gpt4 book ai didi

c# - 如何使用公钥密码术加密字符串

转载 作者:行者123 更新时间:2023-11-30 22:05:38 24 4
gpt4 key购买 nike

我正在尝试实现我自己的 RSA 加密引擎。鉴于这些 RSA algorithm值(value)观:

p = 61. // A prime number.
q = 53. // Also a prime number.
n = 3233. // p * q.
totient = 3120. // (p - 1) * (q - 1)
e = 991. // Co-prime to the totient (co-prime to 3120).
d = 1231. // d * e = 1219921, which is equal to the relation where 1 + k * totient = 1219921 when k = 391.

我正在尝试编写一种方法来加密字符串中的每个字节并返回加密的字符串:

public string Encrypt(string m, Encoding encoding)
{
byte[] bytes = encoding.GetBytes(m);
for (int i = 0; i < bytes.Length; i++)
{
bytes[i] = (byte)BigInteger.ModPow(bytes[i], e, n);
}
string encryptedString = encoding.GetString(bytes);
Console.WriteLine("Encrypted {0} as {1}.", m, encryptedString);
return encryptedString;
}

这里明显的问题是 BigInteger.ModPow(bytes[i], e, n) 可能太大而无法放入字节空间;它可能导致值的大小超过 8 位。您如何解决这个问题,同时仍然能够将加密的字节字符串解密回常规字符串?

更新:即使从 byte[] 加密到 byte[],您也会遇到使用 RSA 算法加密该字节超出字节大小限制的情况:

public byte[] Encrypt(string m, Encoding encoding)
{
byte[] bytes = encoding.GetBytes(m);
for (int i = 0; i < bytes.Length; i++)
{
bytes[i] = (byte)BigInteger.ModPow(bytes[i], e, n);
}
return bytes;
}

更新:我的问题是加密会产生比初始输入字符串更多的字节数:

public byte[] Encrypt(string m, Encoding encoding)
{
byte[] bytes = encoding.GetBytes(m);
byte[] returnBytes = new byte[0];
for (int i = 0; i < bytes.Length; i++)
{
byte[] result = BigInteger.ModPow(bytes[i], (BigInteger)e, n).ToByteArray();
int preSize = returnBytes.Length;
Array.Resize(ref returnBytes, returnBytes.Length + result.Length);
result.CopyTo(returnBytes, preSize);
}
return returnBytes;
}

public string Decrypt(byte[] c, Encoding encoding)
{
byte[] returnBytes = new byte[0];
for (int i = 0; i < c.Length; i++)
{
byte[] result = BigInteger.ModPow(c[i], d, n).ToByteArray();
int preSize = returnBytes.Length;
Array.Resize(ref returnBytes, returnBytes.Length + result.Length);
result.CopyTo(returnBytes, preSize);
}
string decryptedString = encoding.GetString(returnBytes);
return decryptedString;
}

如果你像这样运行这段代码:

byte[] encryptedBytes = engine.Encrypt("Hello, world.", Encoding.UTF8);
Console.WriteLine(engine.Decrypt(encryptedBytes, Encoding.UTF8));

输出是这样的:

?♥D
?♥→☻►♦→☻►♦oD♦8? ?♠oj?♠→☻►♦;♂?♠♂♠?♠

显然,输出不是原始字符串,因为我不能一次只尝试解密每个字节,因为有时密文的两个或更多字节代表我需要解密回的一个整数的值原始字符串的一个字节...所以我想知道处理它的标准机制是什么。

最佳答案

您用于加密和解密每个字节的基本代码 - 对 ModPow 的调用 - 正在运行,但您将不恰本地“拆分消息并加密每个部分”。

为了证明 ModPow 部分(即数学)没问题,这里是基于您的代码,它将 string 加密为 BigInteger[] 并返回:

using System;
using System.Linq;
using System.Numerics;
using System.Text;

class Test
{
const int p = 61;
const int q = 53;
const int n = 3233;
const int totient = 3120;
const int e = 991;
const int d = 1231;

static void Main()
{
var encrypted = Encrypt("Hello, world.", Encoding.UTF8);
var decrypted = Decrypt(encrypted, Encoding.UTF8);
Console.WriteLine(decrypted);
}

static BigInteger[] Encrypt(string text, Encoding encoding)
{
byte[] bytes = encoding.GetBytes(text);
return bytes.Select(b => BigInteger.ModPow(b, (BigInteger)e, n))
.ToArray();
}

static string Decrypt(BigInteger[] encrypted, Encoding encoding)
{
byte[] bytes = encrypted.Select(bi => (byte) BigInteger.ModPow(bi, d, n))
.ToArray();
return encoding.GetString(bytes);
}
}

接下来您需要阅读更多有关如何使用 RSA 将 byte[] 加密为另一个 byte[] 的信息,包括所有不同的填充方案等。有很多不仅仅是在每个字节上调用 ModPow

但重申一下,您这样做是为了最终实现生产 RSA。您在没有任何安全漏洞的情况下这样做的机会确实非常渺茫。出于学术兴趣这样做很好,可以了解更多有关密码学原理的信息,但将真正的实现留给专家。 (我远不是这个领域的专家——我不可能开始实现自己的加密……)

关于c# - 如何使用公钥密码术加密字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24267332/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com