首页 > 代码库 > C# 与 Java Rsa加密与解密互通
C# 与 Java Rsa加密与解密互通
Rsa 加密标准的制定已经过去了十多年了. 这两天在看rsa 加密的文章,基本上都是在说 .net 与 java 之间的 rsa加密是不能互通的.因为项目有用到,所以花了点时间对rsa加密做了一点点了解,发现,不管是java 还是 C# 都对 rsa 的标准加密进行了实现, 是 对于标准是实现,不能互通就讲不过去了. 今天特意写了一段java 代码试了一下,发现是完全可以的.
密钥的描述: C#(.net) 中有三种方式导出密钥,一种是blob,一种是 xml 另一种是参数,其中xml 的方式是把 参数进行了 xml序列化.blob 的形式我没看懂是什么意思,只知道文档上说是给微软的什么api用的,下面给出其参数的形式.
RSAParameters 字段 | Contains | 对应的 PKCS #1 字段 |
---|---|---|
D | d,私钥指数 | privateExponent |
DP | d mod (p - 1) | exponent1 |
DQ | d mod (q - 1) | exponent2 |
Exponent | e,公钥指数 | publicExponent |
InverseQ | (InverseQ)(q) = 1 mod p | coefficient |
Modulus | n | modulus |
P | p | prime1 |
Q | q | prime2 |
RSA 算法
若要生成密钥对,可以从创建名为 p 和 q 的两个大的质数开始; 这两个数相乘,结果称为 n; 因为 p 和 q 都是质数,所以 n 的全部因数为 1、p、q 和 n;
如果仅考虑小于 n 的数,则与 n 为互质数(即与 n 没有公因数)的数的个数等于 (p - 1)(q - 1);
现在,选择一个数 e,它与计算的值为互质数; 则公钥表示为 {e, n};
若要创建私钥,则必须计算 d,它是满足 (d)(e) mod n = 1 的一个数; 根据 Euclidean 算法,私钥为 {d, n};
纯文本 m 到密码文本 c 的加密定义为 c = (m ^ e) mod n; 解密则定义为 m = (c ^ d) mod n;
总之,我们可以从 .net 的rsa 中拿 到合适的密钥描述就是了,至于 java的,我想也是可以做到的,而且通常密钥是应该放在密钥容器的.
接下来我们用.net 生成一个 rsa 的密钥,以十六进制的方式输出的(new 一个RSACryptoServiceProvider,把密钥导出就可以了)
D: 2FE7479CF4CFEE63218C44D763C3E552DC5FBC94A31F944B88AE8E58F0ED16874B8BED35307B143F413761B2ECFFC95F48DF0D0A29FC155C0B968EFE9FFF36E7DP: 6777B761BC29637622FC63682243BB2E05CCFC6FF710ADE1DCE6B0C843B17C4FDQ: 68771CCDA40F0DA0B504C438BB03F7DF30F77364094D475E70270D148260D247Exponent: 010001InverseQ: 5665AB47697008CC2CECB544B582B9C50628281C400846C1E736629B03FE5C85Modulus: B3F276C8EDF515FD3248CCF4163480B9F77443A666522D66B89411EC6DFE11DEA917A97C977750EE777DACBD4D2C11BC363FDC110E5CCA0A1361D51AFA4A7ADDP: ECC60A01B1BDCBA1C5422D8A0A34FC0E46727DB4ED5089E54C356F052E0AB573Q: C28F233948483D0CD0E3FA7B5D2955F2B15E831B38876FB0E7180D873EDF7A6F
为了方便写.net 代码,这里贴一下blob的密钥
0702000000A40000525341320002000001000100DD7A4AFA1AD561130ACA5C0E11DC3F36BC112C4DBDAC7D77EE5077977CA917A9DE11FE6DEC1194B8662D5266A64374F7B9803416F4CC4832FD15F5EDC876F2B373B50A2E056F354CE58950EDB47D72460EFC340A8A2D42C5A1CBBDB1010AC6EC6F7ADF3E870D18E7B06F87381B835EB1F255295D7BFAE3D00C3D484839238FC24F7CB143C8B0E6DCE1AD10F76FFCCC052EBB43226863FC22766329BC61B7776747D26082140D27705E474D096473F730DFF703BB38C404B5A00D0FA4CD1C7768855CFE039B6236E7C14608401C282806C5B982B544B5EC2CCC08706947AB6556E736FF9FFE8E960B5C15FC290A0DDF485FC9FFECB26137413F147B3035ED8B4B8716EDF0588EAE884B941FA394BC5FDC52E5C363D7448C2163EECFF49C47E72F
因为是私钥,所以同时可以加密和解密.下面上 C# 代码,比较简单
using System; using System.Security.Cryptography; class Program { public static void Main(string[] args) { byte[] plainText = new byte[]{0,1,2,3,4,5}; byte[] cipherText; byte[] key = String2Bytes("0702000000A40000525341328001000001000100D3D10816051881319774576B67B1D24F3AA303471A4402AB625208EC1CB04D508AF2098227C5EE185890ECB83E6971C12BDCF4F8AB0FD729167C815D3404C1AD1C0628E3544C89E9F9044A6869447310C72D7CDEB5E3582AB28BCC5069D60CD4AB5A1BB8C754AEB544FA65FB990ADB68F5AA37D7AC2CCDBF19058A2A4CF18FC29577D184E75EFD0ABEC1263893262F40C89AB2831B20B52B477770BA95FA02A71339911EBBF8630AD1AEECC205888440037D580B18AEF11FC5F35EB74E434E5A9302823BF795A34DADDFE0C6D55BC7997E667ADBE511BA06"); using(var rsa = new RSACryptoServiceProvider()) { rsa.ImportCspBlob(key); Console.WriteLine("OAEP:"); Console.WriteLine(Bytes2String(rsa.Encrypt(plainText, true))); Console.WriteLine("PCSK1-v1_5:"); Console.WriteLine(Bytes2String(rsa.Encrypt(plainText, false))); } Console.Write("Press any key to continue . . . "); Console.ReadKey(true); } const string pattern = @"[^0-9a-fA-F]+"; static byte[] String2Bytes(string str) { str = System.Text.RegularExpressions.Regex.Replace(str, pattern, ""); if (str == string.Empty) return null; byte[] data = http://www.mamicode.com/new byte[str.Length / 2]; for (int i = 0; i < data.Length; ++i) data[i] = byte.Parse(str.Substring(2 * i, 2), System.Globalization.NumberStyles.HexNumber); return data; } static string Bytes2String(byte[] data) { System.Text.StringBuilder builder = new System.Text.StringBuilder(); foreach (var element in data) { builder.AppendFormat("{0:X2}",element); } return builder.ToString(); } }
运行后输出(rsa加密,密文每次都不一定一样):
OAEP:
87F04B0F28B81D23E63DA71C8278E0B7E357F40583BDDCAB493D44A58080EB178EC8E0DB0DCD4BE5427FDB8190229B8DF2511BDA1082607C92BD03B0615D5AD3
PCSK1-v1_5:
358AB4D336D0C35DAE3895E8A125F4F5AD0FB58117A4100FAF15DE95FF8615F01FFB1A59C9B579792B7C14E93E54A3E7E236D464DDB93D8DF9D96F63F46BACD7
现在我们有密文了,至于.net 的解密 .net 加密后的密文,我没兴趣去看,反正铁定可以的就是了.
下面我们写java 的解密部分
package rsatest;import java.math.BigInteger;import java.security.KeyFactory;import java.security.PrivateKey;import java.security.PublicKey;import java.security.spec.RSAPrivateKeySpec;import java.security.spec.RSAPublicKeySpec;import javax.crypto.Cipher;public class RsaTest { public static void main(String[] args) throws Exception { //前面补了个0,符号位为0,表示非负 BigInteger n = new BigInteger("B3F276C8EDF515FD3248CCF4163480B9F77443A666522D66B89411EC6DFE11DEA917A97C977750EE777DACBD4D2C11BC363FDC110E5CCA0A1361D51AFA4A7ADD", 16); BigInteger e = new BigInteger("010001", 16); BigInteger d = new BigInteger("2FE7479CF4CFEE63218C44D763C3E552DC5FBC94A31F944B88AE8E58F0ED16874B8BED35307B143F413761B2ECFFC95F48DF0D0A29FC155C0B968EFE9FFF36E7", 16); RSAPublicKeySpec keySpec = new RSAPublicKeySpec(n, e); PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(keySpec); RSAPrivateKeySpec prvKeySpec = new RSAPrivateKeySpec(n, d); PrivateKey privateKey = KeyFactory.getInstance("RSA").generatePrivate(prvKeySpec); //现在key 准备好了,把前面的密文放这里解密 byte[] oaepCiphertext = Hex2Bytes("87F04B0F28B81D23E63DA71C8278E0B7E357F40583BDDCAB493D44A58080EB178EC8E0DB0DCD4BE5427FDB8190229B8DF2511BDA1082607C92BD03B0615D5AD3"); //解密OAEP 加密的数据 Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPPADDING"); cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] plaintext = cipher.doFinal(oaepCiphertext); System.out.println(Bytes2Hex(plaintext)); //解密PKCS1-v1_5 加密的数据 byte[] ciphertext = Hex2Bytes("358AB4D336D0C35DAE3895E8A125F4F5AD0FB58117A4100FAF15DE95FF8615F01FFB1A59C9B579792B7C14E93E54A3E7E236D464DDB93D8DF9D96F63F46BACD7"); cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, privateKey); plaintext = cipher.doFinal(ciphertext); System.out.println(Bytes2Hex(plaintext)); } public static byte[] Hex2Bytes(String hexStr) { if (hexStr.length() % 2 != 0) { hexStr = "0" + hexStr; } byte[] bytes = new byte[hexStr.length() / 2]; for (int i = 0; i < bytes.length; ++i) { bytes[i] = (byte) Integer.parseUnsignedInt(hexStr.substring(i * 2, i * 2 + 2), 16); } return bytes; } public static String Bytes2Hex(byte[] bytes) { StringBuilder builder = new StringBuilder(); for (byte b : bytes) { builder.append(String.format("%02X", b)); } return builder.toString(); }}
程序运行后输出:
000102030405
000102030405
跟我们预期的是一样的.
我们再用java 对明文加密,代码片段
// plaintext {0,1,2,3,4,5} cipher = Cipher.getInstance("RSA/ECB/OAEPPADDING"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); oaepCiphertext = cipher.doFinal(plaintext); System.out.println("OAEP:"); System.out.println(Bytes2Hex(oaepCiphertext)); cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); ciphertext = cipher.doFinal(plaintext); System.out.println("PCSK1-v1_5:"); System.out.println(Bytes2Hex(ciphertext));
对应输出(rsa加密,密文每次都不一定一样):
OAEP:
3144C4CB06C7F49D31E65D09C840069F7CCF602487908CCEAB33D473B949199E1795530B69E1FA20EB59E392B2B934024D46E979DEA1682BDFA61D6FDD980F9C
PCSK1-v1_5:
699A694BEB75616879C6B8D311CC10D987EA109D494EE6C9380CD2C02A124613F130C440CB1CA6D3405E50B62CF96A79EB43C3370253E5D8C1A9132CFE01D686
接下来用C# 对数据解密,代码片段
//还是用之前那个rsa对象//OAEPcipherText = String2Bytes("3144C4CB06C7F49D31E65D09C840069F7CCF602487908CCEAB33D473B949199E1795530B69E1FA20EB59E392B2B934024D46E979DEA1682BDFA61D6FDD980F9C");Console.WriteLine(Bytes2String(rsa.Decrypt(cipherText, true)));//PCSK1-v1_5cipherText = String2Bytes("699A694BEB75616879C6B8D311CC10D987EA109D494EE6C9380CD2C02A124613F130C440CB1CA6D3405E50B62CF96A79EB43C3370253E5D8C1A9132CFE01D686");Console.WriteLine(Bytes2String(rsa.Decrypt(cipherText, false)));
对应的输出:
000102030405
000102030405
解密也成功了.
或许该头疼的问题是,已知 {e,n} 或者 {d,n} 怎么生成.net 使用的密钥.
C# 与 Java Rsa加密与解密互通