rsa互通密钥对生成及互通加解密(c#,java,php)

在数据安全上rsa起着非常大的作用,特别是数据网络通讯的安全上。当异构系统在数据网络通讯上对安全性有所要求时,rsa将作为其中的一种选择,此时rsa的互通性就显得尤为重要了。

本文参考网络资料,提供了rsa互通性的一种可行的解决方案(c#,java,php),而这种互通性是在一定的局限性上达成的,比如密钥是1024位的(更高位没试过,应该也可行),基于PKCS1填充方式。

所编写的代码有一部分使用了硬编码,同时注重了功能的实现,在类结构设计上关注不多,有需要的可自行修改重构。

相关的程序集或包或代码请详见本文末尾附件。

生成互通密钥对

重点参考了Jeffrey Walton(http://www.codeproject.com/Articles/25487/Cryptographic-Interoperability-Keys

1.C#

1
2
3
4
5
6
7
8
9
10
11
12
using Cn.Ubingo.Security.RSA.Core;
using Cn.Ubingo.Security.RSA.Key;
//生成公私钥对
KeyPair keyPair = KeyGenerator.GenerateKeyPair();
//转换成不同的格式
KeyPair asnKeyPair = keyPair.ToASNKeyPair();
KeyPair xmlKeyPair = asnKeyPair.ToXMLKeyPair();
KeyPair pemKeyPair = xmlKeyPair.ToPEMKeyPair();

//获取公私钥,以xml格式的为例
string privateKey = xmlKeyPair.PrivateKey;
string publicKey = xmlKeyPair.PublicKey;

2.Java

1
2
3
4
5
6
7
8
9
10
11
import cn.ubingo.security.rsa.core.*;
import cn.ubingo.security.rsa.key.*;
//生成密钥对
KeyPair keyPair = KeyGenerator.generateKeyPair();
//转换成不同的格式
KeyPair asnKeyPair = keyPair.toASNKeyPair();
KeyPair xmlKeyPair = asnKeyPair.toXMLKeyPair();
KeyPair pemKeyPair = xmlKeyPair.toPEMKeyPair();
//获取公私钥,以asn格式的为例
String publicKey = asnKeyPair.getPublicKey();
String privateKey = asnKeyPair.getPrivateKey();

3.密钥格式

a.XML—c#平台惯用

私钥:

1
2
3
4
5
6
7
8
9
10
11
12
<RSAKeyValue>
<Modulus>
ociVSjfhJzxxAd9msplL4mllmaknjoB4OPgb+hVo1QGcQ9QWLyMHuPyCo3qbp3ahKqqa4nEkRkTHFOJeITXH69LE78Zyizne9N4hamAHZSVmuBa8dtsQmT06yJWxR4siG8JNMR9aQkMN7kGtxsJBN2+CbIomxSMWSQ2pqvFAyks=
</Modulus>
<Exponent>AQAB</Exponent>
<P>0ekRMBZU0GhIYbTZSbnd/TC7QHtE4TCFfg98nOC8EBGBE6U5i3zgRofY0JdWBYmWNE5gtEMINPVfRxJ63Euwkw==</P>
<Q>xU5Vbntok5Emt8aP823gizSpG4h//5NvLBDAYpDmGSeg3Jxi1clJLu8d0bYfDOkk+QADYlpNdmJkH2lC4lXqaQ==</Q>
<DP>FvcKvFoVGV/vW1Ev3JMLKGImel2CIajvUeVb2sXzjUZ8+qs+6nXVC6JdJCUKcjylRD+28MHMqg6WR+5LpsJZJQ==</DP>
<DQ>Ah9ameqn8FZyZS20pLi83qJ9YzdvsQ8j9gRkHWtTnlenylfAVG5q5B9cYcIkyV3EsQ0H/EneZXSfgRr/xjGIaQ==</DQ>
<InverseQ>aCt01Nk4gANWR0JrYvJcsLgwB38OOG82hQrBa/quPkCMjGtLZzKh9TFiZjcTWLs1hsBvCPDBO7LM/wbZn1PG6A==</InverseQ>
<D>O+gF2+coMLqxT9O0+AJKtqp3bDhWRO6uVc4NsgN2f5VqkzU4B7lNdULPbxCKDa8gS6fD1YC2wWxbWZDe8SY0gjgOF1EUbbO8G1XvcHFE5rGxmZsFJCY+dhxgknqO08MXHVaZYjkmQO7z7XpvFKBocduj39NEe2mYO25l07TQfBk=</D>
</RSAKeyValue>

公钥:

1
2
3
4
<RSAKeyValue>
<Modulus>ociVSjfhJzxxAd9msplL4mllmaknjoB4OPgb+hVo1QGcQ9QWLyMHuPyCo3qbp3ahKqqa4nEkRkTHFOJeITXH69LE78Zyizne9N4hamAHZSVmuBa8dtsQmT06yJWxR4siG8JNMR9aQkMN7kGtxsJBN2+CbIomxSMWSQ2pqvFAyks=</Modulus>
<Exponent>AQAB</Exponent>
</RSAKeyValue>

b.ASN—java平台惯用

私钥:

1
MIICdQ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RMcU4l4hNcfr0sTv
xnKLOd703iFqYAdlJWa4Frx22xCZPTrIlbFHiyIbwk0xH1pCQw3uQa3GwkE3b4Js
iibFIxZJDamq8UDKSwIDAQAB
-----END PUBLIC KEY-----

4.密钥互通性

各种格式的密钥都可以通过KeyPair类的c#:To*KeyPair()或java:to*KeyPair()方法相互进行转换,无门槛,无障碍。

C#

1
2
3
4
//转换成不同的格式
KeyPair asnKeyPair = keyPair.ToASNKeyPair();
KeyPair xmlKeyPair = asnKeyPair.ToXMLKeyPair();
KeyPair pemKeyPair = xmlKeyPair.ToPEMKeyPair();

Java

1
2
3
4
//转换成不同的格式
KeyPair asnKeyPair = keyPair.toASNKeyPair();
KeyPair xmlKeyPair = asnKeyPair.toXMLKeyPair();
KeyPair pemKeyPair = xmlKeyPair.toPEMKeyPair();

互通加解密

基于三种语言环境都支持的RSA PKCS1填充方式的算法,实现加解密互通

KeyWorker的c#:Encrypt,Decrypt或java:encrypt,decrypt或php:encrypt,decrypt方法实现加解密的功能

1.C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using Cn.Ubingo.Security.RSA.Core;
using Cn.Ubingo.Security.RSA.Data;
//加解密

//XML
KeyWorker privateWorker = new KeyWorker(privateKey, KeyFormat.XML);
KeyWorker publicWorker = new KeyWorker(publicKey, KeyFormat.XML);
Console.WriteLine(privateWorker.Decrypt(publicWorker.Encrypt("你好!世界")));
Console.WriteLine(publicWorker.Decrypt(privateWorker.Encrypt("你好!中国")));
//ASN
privateWorker = new KeyWorker(asnKeyPair.PrivateKey, KeyFormat.ASN);
publicWorker = new KeyWorker(asnKeyPair.PublicKey, KeyFormat.ASN);
Console.WriteLine(privateWorker.Decrypt(publicWorker.Encrypt("你好!世界")));
Console.WriteLine(publicWorker.Decrypt(privateWorker.Encrypt("你好!中国")));
//PEM
privateWorker = new KeyWorker(pemKeyPair.PrivateKey, KeyFormat.PEM);
publicWorker = new KeyWorker(pemKeyPair.PublicKey, KeyFormat.PEM);
Console.WriteLine(privateWorker.Decrypt(publicWorker.Encrypt("你好!世界")));
Console.WriteLine(publicWorker.Decrypt(privateWorker.Encrypt("你好!中国")));

2.Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import cn.ubingo.security.rsa.core.*;
import cn.ubingo.security.rsa.data.*;
//ASN
KeyWorker privateWorker = new KeyWorker(privateKey, KeyFormat.ASN);
KeyWorker publicWorker = new KeyWorker(publicKey, KeyFormat.ASN);
System.out.print(privateWorker.decrypt(publicWorker.encrypt("你好!世界")));
System.out.print(publicWorker.decrypt(privateWorker.encrypt("你好!中国")));
//XML
privateWorker = new KeyWorker(xmlKeyPair.getPrivateKey(), KeyFormat.XML);
publicWorker = new KeyWorker(xmlKeyPair.getPublicKey(), KeyFormat.XML);
System.out.print(privateWorker.decrypt(publicWorker.encrypt("你好!世界")));
System.out.print(publicWorker.decrypt(privateWorker.encrypt("你好!中国")));
//PEM
privateWorker = new KeyWorker(pemKeyPair.getPrivateKey(), KeyFormat.PEM);
publicWorker = new KeyWorker(pemKeyPair.getPublicKey(), KeyFormat.PEM);
System.out.print(privateWorker.decrypt(publicWorker.encrypt("你好!世界")));
System.out.print(publicWorker.decrypt(privateWorker.encrypt("你好!中国")));

3.php
请保证php文件为utf-8的格式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
include "KeyWorker.php";
use Cn\Ubingo\Security\RSA\Core as core;
use Cn\Ubingo\Security\RSA\Data as data;
/*PEM TEST*/
$publicWorker = new data\KeyWorker("-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCZw5tyBNJsjgVrPkLUIWF9el5E
OXL1AydVBnJ/WWPKTZJBWYxudW+1jI5ifML+1DkCxw/2QLowzViV1OLnpNAWr7zv
LpN6i7OZtq0o2Yfc+vv6vCctTpMvFartRRwDeXTRBgoGd71UwWgdMOu8Gmr9sv5u
3C9Kj5fhj0I4WgB94wIDAQAB
-----END PUBLIC KEY-----",core\KeyFormat::PEM);
$privateWorker = new data\KeyWorker("-----BEGIN PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJnDm3IE0myOBWs+
QtQhYX16XkQ5cvUDJ1UGcn9ZY8pNkkFZjG51b7WMjmJ8wv7UOQLHD/ZAujDNWJXU
4uek0BavvO8uk3qLs5m2rSjZh9z6+/q8Jy1Oky8Vqu1FHAN5dNEGCgZ3vVTBaB0w
67waav2y/m7cL0qPl+GPQjhaAH3jAgMBAAECgYA6ANHYlv0RuhlNNTVcdCMkhE6f
CdTVnBTwO/hhFcVRASYUxMT2vhIfuB/WAx5DpqbC53ib+hrCYhPyoVXe8AsWjgBJ
cj3RHxjLMou0Jbu6mTjcLgKzMYM85yHscmmDcc8l/ep9BhbiJJsTVM8RqqXfdPmY
5CAgfwsNXruC7Zb5wQJBAMxVZhR+IzoeYLsmEciy2oOaTq5u8O6yjx7RapIggIKT
dRU5G2GeCyLRHyZH/+U2/nL3undiP74fxizatzgTE3ECQQDApNKzaMobPW23wRes
E+dDjJxYnqVqOmZS22D6s+BKwDmHpOt+oJmrirY4LWmOUwUfgAboDWH5y/q5+qJH
P5STAkEAux+F4UR2nDXPnfPKG4L3K8f3QDUm/WGWQcHEF9gd9/Z0JaBrm+TxC8x4
+0S6ar4HHWASalwWRdWxVchiO770cQJAGaEUAxhq4wreIPdIffU77Em1tziMC0Dv
whA7q77olSlTvg8b4YHeT+spaPnptCypXtJ6mL7HDSOtHLcSheYYjwJBAJFdK3vg
ZS0Sk4oy6iOaOcux+5uBkpF9ran0GIpehyGzia+F1Xf/+NnS22qER/ADvRMNnyHf
QH24qWxQuST39UQ=
-----END PRIVATE KEY-----",core\KeyFormat::PEM);

echo $publicWorker->decrypt($privateWorker->encrypt("你好!世界"));
echo $privateWorker->decrypt($publicWorker->encrypt("你好!中国"));

/*ASN TEST*/
$publicWorker = new data\KeyWorker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yHfQH24qWxQuST39UQ=",core\KeyFormat::ASN);

echo $publicWorker->decrypt($privateWorker->encrypt("你好!世界"));
echo $privateWorker->decrypt($publicWorker->encrypt("你好!中国"));

4.加解密互通性

任何语言(指的是c#,java,php)使用KeyWorker加密得到的密文,用任何语言的KeyWorker都可以进行解密,只要公私钥配对即可。

原文链接:http://www.cnblogs.com/FoChen/p/4740814.html