gpt4 book ai didi

C# RSA 公共(public)模数/指数? (坏数据)

转载 作者:太空狗 更新时间:2023-10-29 21:52:15 25 4
gpt4 key购买 nike

我试了又试,但我仍然收到“错误数据”。如何使用带有公钥指数/模数的 RSACryptoServiceProvider 解密数据?

public static byte[] Encrypt(byte[] b, byte[] mod, byte[] exp)
{
CspParameters csp = new CspParameters();
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(csp);
RSACryptoServiceProvider.UseMachineKeyStore = false;

RSAParameters par = new RSAParameters();
par.Exponent = exp;
par.Modulus = mod;
rsa.ImportParameters(par);

return rsa.Encrypt(b, false);
}
public static byte[] Decrypt(byte[] b, byte[] pubexp, byte[] mod, byte[] priexp)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
RSACryptoServiceProvider.UseMachineKeyStore = false;
RSAParameters rp = new RSAParameters();

rp.Exponent = pubexp;
rp.D = priexp;

rp.Modulus = mod;
rsa.ImportParameters(rp);
return rsa.Decrypt(b, false);
}

static List<byte[]> Base2Array(string str)
{
byte[] b = Convert.FromBase64String(str);

List<byte[]> Bytes = new List<byte[]>();

int i = 0;
while (i < b.Length)
{
int size = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(b, i));
i += 4;
byte[] b2 = new byte[size];
Array.Copy(b, i, b2, 0, size);
Bytes.Add(b2);
i += size;
}

return Bytes;
}


static void Main(string[] args)
{
List<byte[]> pub = Base2Array("AAAAB3NzaC1yc2EAAAABJQAAAIBMW4HxU1glv+CcZpJnvUKEyeNfFoKkyLOVLOOb/vNXQkrkGsNdpYAZkKKizij8fD3u3/iYT8UI+xkFoyonRYVipgCslirJB1VdvLivXs69Ht4vf7VAv2yJSUni3XsIHauMlfOkjJ7DpUW75ZkrxsGieICFWlXvRnAyDdqQrkZRZQ==");
List<byte[]> pri = Base2Array("AAAAgBSjHDNiojO3UXZg6Ux4VyrOx9SCn9mCWgykWTEUeR6Anp6DxhlPUw3UEEetVy97hlw8iGCEQxcvG4T7qocni9UtUTLdpuQzvr6718y2CP0ouKt/1hVKD9QssT08XUvJEBQnnl2yVZAbIqT/DGnUH36L0BnQE/2ombPakwHscfFFAAAAQQCSfQy2cP8Oa0IR0u0whxqGmuuXY/3tCD8NaaSCYm31ly0QBdxFdf2WkC51DNVaf5/1ErHceMapFN9Z3j+/6lA7AAAAQQCFcMoSA32f240nFBmMv/nn/mL1uSdAXOxjUHViJc6M8ntZvW2ZuP2qTvfA3mh1AK5K69piX/4T72xxqTA2tmrfAAAAQFxX1JunFI+fpobdienVCZcjibwbpDPf1MVTbwQhAXHqVBL3XXgkysS/67X/aWnv/+SdBDaXa1SnDpphSWOkxAQ=");

//pub[0] 7
//pub[1] 1
//pub[2] 128

//pri[0] 128
//pri[1] 65
//pri[2] 65
//pri[3] 64

byte[] pubmod = null;
byte[] primod = null;
byte[] pubexp = null;
byte[] priexp = null;

pubexp = pub[0];
pubmod = pub[2];

priexp = pri[0];
primod = pri[2];


byte[] bstr = Encoding.ASCII.GetBytes("Test");

bstr = Encrypt(bstr, pubmod, pubexp);
bstr = Decrypt(bstr, pubexp, pubmod, null);


string str = Encoding.ASCII.GetString(bstr);
}

最佳答案

我做了类似的事情:

public byte[] PublicDecryption(byte[] encryptedData)
{
var encData = new BigInteger(encryptedData);
BigInteger bnData = encData.modPow(_exponent, _modulus);
return bnData.getBytes();
}

public byte[] PrivateDecryption(byte[] encryptedData)
{
var encData = new BigInteger(encryptedData);
d = new BigInteger(rsaParams.D);
BigInteger bnData = encData.modPow(d, _modulus);
return bnData.getBytes();
}

BigInteger 在哪里:

http://www.codeproject.com/KB/cs/biginteger.aspx

因为微软的实现是部分的和错误的。

我从来没有遇到过这个问题。

希望对你有帮助

关于C# RSA 公共(public)模数/指数? (坏数据),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2909899/

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