我使用privKeyOk它工作正常,而privKeyOk是由 openssl 生成的
openssl genrsa -out rsa_1024_priv.pem 1024
但是privKey失败,在 vs2005 和 vs2010 中测试。这是有问题的代码
test()和test2()是相同的错误,为什么?
GetIntegerSize见http://www.jensign.com/opensslkey/opensslkey.cs
BigInteger见http://www.codeproject.com/Articles/2728/C-BigInteger-Class
public void test()
{
//byte[] privKeyOk = Convert.FromBase64String("MIICXAIBAAKBgQC66m7oUZPf02OMzUlUxPIWD09YhjtFiK8HR1jjIeeNwcR7301jdM6bco0xLiMAom1YzyjivCAhBjplWOnkjt/KiaSHt5FpXZZ5s6Vlg99Rro5ClLiBwVttCcqSPVJ0dH79t3x2BQ5mvS5qnso0jOL4mDBjlrxWq0Ki0Me5N+/RuwIDAQABAoGAOJUFE+C2EfiM1y/Bth6UZicmTtb7lFYHHf30I3W9AcWZdCIp7CSM8rOfmDauM9/3l/3GE7T9SOH4xXs/+odhqQq3nXVQ63nfdd8OQA58dSER1ybKHlpORTgyF5v2+4K7UVaDr9ADbZxcLSaF2kjgOJAT5Irp8jbfRO1KMbTVEekCQQDhkOa3ST/+P0Oez47iKHbjXpW1vfHVMJA5hGLAlMbPREhkorVDWeh3vqBWUmaVZY+7kNGcX6/tMTXVntEMawuVAkEA1CKLJJANQe79bTz6Ro3+OI9UL/xd7WyJWGSkIzgXW5A1XveOg0kwgJc+XsdcBCEQVl2SYWfte91s+VAfMRiUDwJAOuHsmGQSxHgXWtmr3vXZMzl5s1vGSXTH8XvRm5w62dhlXX/r+Yz61md4y4/vH4jHoJsJXNzNvAlgmEdBIxC+kQJANxadYapS9wtUHpkWOVpIaXG3IBr+XVixNhY0lbzMPAfwBDBvfuJzyQD9vmkxGRVpA7Sa/daaUm9HDFtlLj++uwJBAJlLr5knlgsQmJNTcZZhdjMcPsoyFdUOVkEDgn5UGWDrwm3kB3YwDeVn3EQrGg0pE1EsbmSIlTb+KAUItClihU4=");
byte[] privKey = Convert.FromBase64String("MIICWwIBAAKBgQDmp99KnS17WAXmDbx+pJ7QSa5FWYammQAnxescDTKWH8p3LbNqxZRaPhYbTqe6fqYj9cIYgjre/EecxfdzrLfoF2yR6uOD+MiahWhLxP7nKYnFs7YYKvIxxjkkSEidF3Fj1oepZHkAAlRDY1viqdqSMuEiuSy7OReWKjJDJX2tXQIDAQABAoGAfGVTHrmQOvJpE/6ahy6fXDQwocQ7w4PGuqyaNSHFA4QXvJjQJ6hxbw4G/Ck6enEthz1WtcMenv4uXRXDdAZVk6xnhx7scc0ebJiJte/OvVGCo3kg9CBOz9s1imufDfcIR+hdY4XdUquTD+y0HgWn/qq8Grj346/VFg+EZ3wMR2ECQQH88l+U5skNqv2IuG6Ipc7d5gRNsW+9Usxbt5kdzCQeYOcddTEn0xrDrSdsxTbKFCFiLDkUqHF/vGWjNCDQWMTlAkB0BQ20j5E5aOIoLaXnTek1JpsMkL2nvXP1qSwSp1bHUYWEtDJvcYwXvRhUOW+FT7LJFOKJbl+dNGlx4FY7XHcZAkEBPbDZwyvQgqyiuE5QCRJfMBpdw1WoJ2jg0buoY3ZDcYcRnUzDE1XuVgjnHDl6uueX8x0hwGho+NBULWzjsspGlQJAcHTFiBLwzRjgvdkhSka2mZJGD7PsYT69u4yy75HMB375UFvp1yPUit37SwgxFKLfxRC9VEDr5ND8tCii7P7/YQJAAN0RIsnTLW81z6F1absoWW8VdGvxYlgcI/biPj4xybHMDlw9m0KfVik8QVcbUdvdAUhWasx5/GcSsrtxoWOifg==");
byte[] MODULUS, E, D, P, Q, DP, DQ, IQ;
MemoryStream mem = new MemoryStream(privKey);
BinaryReader binr = new BinaryReader(mem);
byte bt = 0;
ushort twobytes = 0;
int elems = 0;
twobytes = binr.ReadUInt16();
if (twobytes == 0x8130)
binr.ReadByte();
else if (twobytes == 0x8230)
binr.ReadInt16();
else
return;
twobytes = binr.ReadUInt16();
if (twobytes != 0x0102)
return;
bt = binr.ReadByte();
if (bt != 0x00)
return;
elems = GetIntegerSize(binr);
MODULUS = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
E = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
D = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
P = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
Q = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
DP = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
DQ = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
IQ = binr.ReadBytes(elems);
//Test data is valid, can be ignored
BigInteger biMODULUS = new BigInteger(MODULUS);
BigInteger biE = new BigInteger(E);
BigInteger biD = new BigInteger(D);
BigInteger biP = new BigInteger(P);
BigInteger biQ = new BigInteger(Q);
BigInteger biDP = new BigInteger(DP);
BigInteger biDQ = new BigInteger(DQ);
BigInteger biIQ = new BigInteger(IQ);
BigInteger biPhi = (biP - 1) * (biQ - 1);
if (biMODULUS == biP * biQ &&
biDP == biD % (biP - 1) &&
biDQ == biD % (biQ - 1) &&
biIQ == biQ.modInverse(biP) &&
biD == biE.modInverse(biPhi))
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSAParameters RSAparams = new RSAParameters();
RSAparams.Modulus = MODULUS;
RSAparams.Exponent = E;
RSAparams.D = D;
RSAparams.P = P;
RSAparams.Q = Q;
RSAparams.DP = DP;
RSAparams.DQ = DQ;
RSAparams.InverseQ = IQ;
RSA.ImportParameters(RSAparams); //Error: Throw System.Security.Cryptography.CryptographicException Incorrect data.
}
}
public static byte[] HexStrToBytes(string hexString)
{
hexString = hexString.Replace(" ", "").Replace("\r", "").Replace("\n", "");
byte[] bytesCnt = new byte[hexString.Length / 2];
for (int i = 0; i < bytesCnt.Length; i++)
bytesCnt[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
return bytesCnt;
}
private void test2()
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSAParameters RSAparams = new RSAParameters();
//RSAparams.Modulus = HexStrToBytes("00e6a7df4a9d2d7b5805e60dbc7ea49ed049ae455986a6990027c5eb1c0d32961fca772db36ac5945a3e161b4ea7ba7ea623f5c218823adefc479cc5f773acb7e8176c91eae383f8c89a85684bc4fee72989c5b3b6182af231c6392448489d177163d687a9647900025443635be2a9da9232e122b92cbb3917962a3243257dad5d");
RSAparams.Modulus = HexStrToBytes("e6a7df4a9d2d7b5805e60dbc7ea49ed049ae455986a6990027c5eb1c0d32961fca772db36ac5945a3e161b4ea7ba7ea623f5c218823adefc479cc5f773acb7e8176c91eae383f8c89a85684bc4fee72989c5b3b6182af231c6392448489d177163d687a9647900025443635be2a9da9232e122b92cbb3917962a3243257dad5d");
RSAparams.Exponent = HexStrToBytes("010001");
RSAparams.D = HexStrToBytes("7c65531eb9903af26913fe9a872e9f5c3430a1c43bc383c6baac9a3521c5038417bc98d027a8716f0e06fc293a7a712d873d56b5c31e9efe2e5d15c374065593ac67871eec71cd1e6c9889b5efcebd5182a37920f4204ecfdb358a6b9f0df70847e85d6385dd52ab930fecb41e05a7feaabc1ab8f7e3afd5160f84677c0c4761");
RSAparams.P = HexStrToBytes("01fcf25f94e6c90daafd88b86e88a5cedde6044db16fbd52cc5bb7991dcc241e60e71d753127d31ac3ad276cc536ca1421622c3914a8717fbc65a33420d058c4e5");
RSAparams.Q = HexStrToBytes("74050db48f913968e2282da5e74de935269b0c90bda7bd73f5a92c12a756c7518584b4326f718c17bd1854396f854fb2c914e2896e5f9d346971e0563b5c7719");
RSAparams.DP = HexStrToBytes("013db0d9c32bd082aca2b84e5009125f301a5dc355a82768e0d1bba86376437187119d4cc31355ee5608e71c397abae797f31d21c06868f8d0542d6ce3b2ca4695");
RSAparams.DQ = HexStrToBytes("7074c58812f0cd18e0bdd9214a46b69992460fb3ec613ebdbb8cb2ef91cc077ef9505be9d723d48addfb4b083114a2dfc510bd5440ebe4d0fcb428a2ecfeff61");
//RSAparams.InverseQ = HexStrToBytes("00dd1122c9d32d6f35cfa17569bb28596f15746bf162581c23f6e23e3e31c9b1cc0e5c3d9b429f56293c41571b51dbdd0148566acc79fc6712b2bb71a163a27e");
RSAparams.InverseQ = HexStrToBytes("dd1122c9d32d6f35cfa17569bb28596f15746bf162581c23f6e23e3e31c9b1cc0e5c3d9b429f56293c41571b51dbdd0148566acc79fc6712b2bb71a163a27e");
RSA.ImportParameters(RSAparams); //Also Error: Throw System.Security.Cryptography.CryptographicException Incorrect data.
}
/*
openssl rsa -text -inform DER -in privKey.bin
Private-Key: (1024 bit)
modulus:
00:e6:a7:df:4a:9d:2d:7b:58:05:e6:0d:bc:7e:a4:
9e:d0:49:ae:45:59:86:a6:99:00:27:c5:eb:1c:0d:
32:96:1f:ca:77:2d:b3:6a:c5:94:5a:3e:16:1b:4e:
a7:ba:7e:a6:23:f5:c2:18:82:3a:de:fc:47:9c:c5:
f7:73:ac:b7:e8:17:6c:91:ea:e3:83:f8:c8:9a:85:
68:4b:c4:fe:e7:29:89:c5:b3:b6:18:2a:f2:31:c6:
39:24:48:48:9d:17:71:63:d6:87:a9:64:79:00:02:
54:43:63:5b:e2:a9:da:92:32:e1:22:b9:2c:bb:39:
17:96:2a:32:43:25:7d:ad:5d
publicExponent: 65537 (0x10001)
privateExponent:
7c:65:53:1e:b9:90:3a:f2:69:13:fe:9a:87:2e:9f:
5c:34:30:a1:c4:3b:c3:83:c6:ba:ac:9a:35:21:c5:
03:84:17:bc:98:d0:27:a8:71:6f:0e:06:fc:29:3a:
7a:71:2d:87:3d:56:b5:c3:1e:9e:fe:2e:5d:15:c3:
74:06:55:93:ac:67:87:1e:ec:71:cd:1e:6c:98:89:
b5:ef:ce:bd:51:82:a3:79:20:f4:20:4e:cf:db:35:
8a:6b:9f:0d:f7:08:47:e8:5d:63:85:dd:52:ab:93:
0f:ec:b4:1e:05:a7:fe:aa:bc:1a:b8:f7:e3:af:d5:
16:0f:84:67:7c:0c:47:61
prime1:
01:fc:f2:5f:94:e6:c9:0d:aa:fd:88:b8:6e:88:a5:
ce:dd:e6:04:4d:b1:6f:bd:52:cc:5b:b7:99:1d:cc:
24:1e:60:e7:1d:75:31:27:d3:1a:c3:ad:27:6c:c5:
36:ca:14:21:62:2c:39:14:a8:71:7f:bc:65:a3:34:
20:d0:58:c4:e5
prime2:
74:05:0d:b4:8f:91:39:68:e2:28:2d:a5:e7:4d:e9:
35:26:9b:0c:90:bd:a7:bd:73:f5:a9:2c:12:a7:56:
c7:51:85:84:b4:32:6f:71:8c:17:bd:18:54:39:6f:
85:4f:b2:c9:14:e2:89:6e:5f:9d:34:69:71:e0:56:
3b:5c:77:19
exponent1:
01:3d:b0:d9:c3:2b:d0:82:ac:a2:b8:4e:50:09:12:
5f:30:1a:5d:c3:55:a8:27:68:e0:d1:bb:a8:63:76:
43:71:87:11:9d:4c:c3:13:55:ee:56:08:e7:1c:39:
7a:ba:e7:97:f3:1d:21:c0:68:68:f8:d0:54:2d:6c:
e3:b2:ca:46:95
exponent2:
70:74:c5:88:12:f0:cd:18:e0:bd:d9:21:4a:46:b6:
99:92:46:0f:b3:ec:61:3e:bd:bb:8c:b2:ef:91:cc:
07:7e:f9:50:5b:e9:d7:23:d4:8a:dd:fb:4b:08:31:
14:a2:df:c5:10:bd:54:40:eb:e4:d0:fc:b4:28:a2:
ec:fe:ff:61
coefficient:
00:dd:11:22:c9:d3:2d:6f:35:cf:a1:75:69:bb:28:
59:6f:15:74:6b:f1:62:58:1c:23:f6:e2:3e:3e:31:
c9:b1:cc:0e:5c:3d:9b:42:9f:56:29:3c:41:57:1b:
51:db:dd:01:48:56:6a:cc:79:fc:67:12:b2:bb:71:
a1:63:a2:7e
-----BEGIN RSA PRIVATE KEY-----
MIICWwIBAAKBgQDmp99KnS17WAXmDbx+pJ7QSa5FWYammQAnxescDTKWH8p3LbNq
xZRaPhYbTqe6fqYj9cIYgjre/EecxfdzrLfoF2yR6uOD+MiahWhLxP7nKYnFs7YY
KvIxxjkkSEidF3Fj1oepZHkAAlRDY1viqdqSMuEiuSy7OReWKjJDJX2tXQIDAQAB
AoGAfGVTHrmQOvJpE/6ahy6fXDQwocQ7w4PGuqyaNSHFA4QXvJjQJ6hxbw4G/Ck6
enEthz1WtcMenv4uXRXDdAZVk6xnhx7scc0ebJiJte/OvVGCo3kg9CBOz9s1imuf
DfcIR+hdY4XdUquTD+y0HgWn/qq8Grj346/VFg+EZ3wMR2ECQQH88l+U5skNqv2I
uG6Ipc7d5gRNsW+9Usxbt5kdzCQeYOcddTEn0xrDrSdsxTbKFCFiLDkUqHF/vGWj
NCDQWMTlAkB0BQ20j5E5aOIoLaXnTek1JpsMkL2nvXP1qSwSp1bHUYWEtDJvcYwX
vRhUOW+FT7LJFOKJbl+dNGlx4FY7XHcZAkEBPbDZwyvQgqyiuE5QCRJfMBpdw1Wo
J2jg0buoY3ZDcYcRnUzDE1XuVgjnHDl6uueX8x0hwGho+NBULWzjsspGlQJAcHTF
iBLwzRjgvdkhSka2mZJGD7PsYT69u4yy75HMB375UFvp1yPUit37SwgxFKLfxRC9
VEDr5ND8tCii7P7/YQJAAN0RIsnTLW81z6F1absoWW8VdGvxYlgcI/biPj4xybHM
Dlw9m0KfVik8QVcbUdvdAUhWasx5/GcSsrtxoWOifg==
-----END RSA PRIVATE KEY-----
*/