摘要
在数据安全上rsa起着非常大的作用,特别是数据网络通讯的安全上。当异构系统在数据网络通讯上对安全性有所要求时,rsa将作为其中的一种选择,此时rsa的互通性就显得尤为重要了。
本文参考网络资料,提供了rsa互通性的一种可行的解决方案(c#,java,php),而这种互通性是在一定的局限性上达成的,比如密钥是1024位的(更高位没试过,应该也可行),基于PKCS1填充方式。
所编写的代码有一部分使用了硬编码,同时注重了功能的实现,在类结构设计上关注不多,有需要的可自行修改重构。
相关的程序集或包或代码请详见本文末尾附件。
一、生成互通密钥对
重点参考了Jeffrey Walton(http://www.codeproject.com/Articles/25487/Cryptographic-Interoperability-Keys)
1.c#
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
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#平台惯用
私钥:
<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>
公钥:
<RSAKeyValue><Modulus>ociVSjfhJzxxAd9msplL4mllmaknjoB4OPgb+hVo1QGcQ9QWLyMHuPyCo3qbp3ahKqqa4nEkRkTHFOJeITXH69LE78Zyizne9N4hamAHZSVmuBa8dtsQmT06yJWxR4siG8JNMR9aQkMN7kGtxsJBN2+CbIomxSMWSQ2pqvFAyks=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>
b.ASN---java平台惯用
私钥:
MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAKHIlUo34Sc8cQHfZrKZS+JpZZmpJ46AeDj4G/oVaNUBnEPUFi8jB7j8gqN6m6d2oSqqmuJxJEZExxTiXiE1x+vSxO/Gcos53vTeIWpgB2UlZrgWvHbbEJk9OsiVsUeLIhvCTTEfWkJDDe5BrcbCQTdvgmyKJsUjFkkNqarxQMpLAgMBAAECgYA76AXb5ygwurFP07T4Akq2qndsOFZE7q5Vzg2yA3Z/lWqTNTgHuU11Qs9vEIoNryBLp8PVgLbBbFtZkN7xJjSCOA4XURRts7wbVe9wcUTmsbGZmwUkJj52HGCSeo7TwxcdVpliOSZA7vPtem8UoGhx26Pf00R7aZg7bmXTtNB8GQJBANHpETAWVNBoSGG02Um53f0wu0B7ROEwhX4PfJzgvBARgROlOYt84EaH2NCXVgWJljROYLRDCDT1X0cSetxLsJMCQQDFTlVue2iTkSa3xo/zbeCLNKkbiH//k28sEMBikOYZJ6DcnGLVyUku7x3Rth8M6ST5AANiWk12YmQfaULiVeppAkAW9wq8WhUZX+9bUS/ckwsoYiZ6XYIhqO9R5VvaxfONRnz6qz7qddULol0kJQpyPKVEP7bwwcyqDpZH7kumwlklAkACH1qZ6qfwVnJlLbSkuLzeon1jN2+xDyP2BGQda1OeV6fKV8BUbmrkH1xhwiTJXcSxDQf8Sd5ldJ+BGv/GMYhpAkBoK3TU2TiAA1ZHQmti8lywuDAHfw44bzaFCsFr+q4+QIyMa0tnMqH1MWJmNxNYuzWGwG8I8ME7ssz/BtmfU8bo
公钥:
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQChyJVKN+EnPHEB32aymUviaWWZqSeOgHg4+Bv6FWjVAZxD1BYvIwe4/IKjepundqEqqpricSRGRMcU4l4hNcfr0sTvxnKLOd703iFqYAdlJWa4Frx22xCZPTrIlbFHiyIbwk0xH1pCQw3uQa3GwkE3b4JsiibFIxZJDamq8UDKSwIDAQAB
c.PEM---php平台惯用
私钥:
-----BEGIN PRIVATE KEY-----
MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAKHIlUo34Sc8cQHf
ZrKZS+JpZZmpJ46AeDj4G/oVaNUBnEPUFi8jB7j8gqN6m6d2oSqqmuJxJEZExxTi
XiE1x+vSxO/Gcos53vTeIWpgB2UlZrgWvHbbEJk9OsiVsUeLIhvCTTEfWkJDDe5B
rcbCQTdvgmyKJsUjFkkNqarxQMpLAgMBAAECgYA76AXb5ygwurFP07T4Akq2qnds
OFZE7q5Vzg2yA3Z/lWqTNTgHuU11Qs9vEIoNryBLp8PVgLbBbFtZkN7xJjSCOA4X
URRts7wbVe9wcUTmsbGZmwUkJj52HGCSeo7TwxcdVpliOSZA7vPtem8UoGhx26Pf
00R7aZg7bmXTtNB8GQJBANHpETAWVNBoSGG02Um53f0wu0B7ROEwhX4PfJzgvBAR
gROlOYt84EaH2NCXVgWJljROYLRDCDT1X0cSetxLsJMCQQDFTlVue2iTkSa3xo/z
beCLNKkbiH//k28sEMBikOYZJ6DcnGLVyUku7x3Rth8M6ST5AANiWk12YmQfaULi
VeppAkAW9wq8WhUZX+9bUS/ckwsoYiZ6XYIhqO9R5VvaxfONRnz6qz7qddULol0k
JQpyPKVEP7bwwcyqDpZH7kumwlklAkACH1qZ6qfwVnJlLbSkuLzeon1jN2+xDyP2
BGQda1OeV6fKV8BUbmrkH1xhwiTJXcSxDQf8Sd5ldJ+BGv/GMYhpAkBoK3TU2TiA
A1ZHQmti8lywuDAHfw44bzaFCsFr+q4+QIyMa0tnMqH1MWJmNxNYuzWGwG8I8ME7
ssz/BtmfU8bo
-----END PRIVATE KEY-----
公钥:
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQChyJVKN+EnPHEB32aymUviaWWZ
qSeOgHg4+Bv6FWjVAZxD1BYvIwe4/IKjepundqEqqpricSRGRMcU4l4hNcfr0sTv
xnKLOd703iFqYAdlJWa4Frx22xCZPTrIlbFHiyIbwk0xH1pCQw3uQa3GwkE3b4Js
iibFIxZJDamq8UDKSwIDAQAB
-----END PUBLIC KEY-----
4.密钥互通性
各种格式的密钥都可以通过KeyPair类的c#:To*KeyPair()或java:to*KeyPair()方法相互进行转换,无门槛,无障碍。
c#
//转换成不同的格式
KeyPair asnKeyPair = keyPair.ToASNKeyPair();
KeyPair xmlKeyPair = asnKeyPair.ToXMLKeyPair();
KeyPair pemKeyPair = xmlKeyPair.ToPEMKeyPair();
java
//转换成不同的格式
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#
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
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的格式
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("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCZw5tyBNJsjgVrPkLUIWF9el5EOXL1AydVBnJ/WWPKTZJBWYxudW+1jI5ifML+1DkCxw/2QLowzViV1OLnpNAWr7zvLpN6i7OZtq0o2Yfc+vv6vCctTpMvFartRRwDeXTRBgoGd71UwWgdMOu8Gmr9sv5u3C9Kj5fhj0I4WgB94wIDAQAB",core\KeyFormat::ASN);
$privateWorker = new data\KeyWorker("MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJnDm3IE0myOBWs+QtQhYX16XkQ5cvUDJ1UGcn9ZY8pNkkFZjG51b7WMjmJ8wv7UOQLHD/ZAujDNWJXU4uek0BavvO8uk3qLs5m2rSjZh9z6+/q8Jy1Oky8Vqu1FHAN5dNEGCgZ3vVTBaB0w67waav2y/m7cL0qPl+GPQjhaAH3jAgMBAAECgYA6ANHYlv0RuhlNNTVcdCMkhE6fCdTVnBTwO/hhFcVRASYUxMT2vhIfuB/WAx5DpqbC53ib+hrCYhPyoVXe8AsWjgBJcj3RHxjLMou0Jbu6mTjcLgKzMYM85yHscmmDcc8l/ep9BhbiJJsTVM8RqqXfdPmY5CAgfwsNXruC7Zb5wQJBAMxVZhR+IzoeYLsmEciy2oOaTq5u8O6yjx7RapIggIKTdRU5G2GeCyLRHyZH/+U2/nL3undiP74fxizatzgTE3ECQQDApNKzaMobPW23wResE+dDjJxYnqVqOmZS22D6s+BKwDmHpOt+oJmrirY4LWmOUwUfgAboDWH5y/q5+qJHP5STAkEAux+F4UR2nDXPnfPKG4L3K8f3QDUm/WGWQcHEF9gd9/Z0JaBrm+TxC8x4+0S6ar4HHWASalwWRdWxVchiO770cQJAGaEUAxhq4wreIPdIffU77Em1tziMC0DvwhA7q77olSlTvg8b4YHeT+spaPnptCypXtJ6mL7HDSOtHLcSheYYjwJBAJFdK3vgZS0Sk4oy6iOaOcux+5uBkpF9ran0GIpehyGzia+F1Xf/+NnS22qER/ADvRMNnyHfQH24qWxQuST39UQ=",core\KeyFormat::ASN); echo $publicWorker->decrypt($privateWorker->encrypt("你好!世界"));
echo $privateWorker->decrypt($publicWorker->encrypt("你好!中国"));
4.加解密互通性
任何语言(指的是c#,java,php)使用KeyWorker加密得到的密文,用任何语言的KeyWorker都可以进行解密,只要公私钥配对即可。
三、测试实例
完整的测试实例代码如下
1.c#
using Cn.Ubingo.Security.RSA.Core;
using Cn.Ubingo.Security.RSA.Data;
using Cn.Ubingo.Security.RSA.Key;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Cn.Ubingo.Security.RSA.Test
{
/// <summary>
/// 陈服建(fochen,j@ubingo.cn)
/// 2015-01-23
/// </summary>
class Program
{
static void Main(string[] args)
{
//生成公私钥对
KeyPair keyPair = KeyGenerator.GenerateKeyPair(); //转换成不同的格式
KeyPair asnKeyPair = keyPair.ToASNKeyPair();
KeyPair xmlKeyPair = asnKeyPair.ToXMLKeyPair();
KeyPair pemKeyPair = xmlKeyPair.ToPEMKeyPair(); //获取公私钥
string privateKey = xmlKeyPair.PrivateKey;
string publicKey = xmlKeyPair.PublicKey; //加解密 //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
package cn.ubingo.security.rsa.test; import cn.ubingo.security.rsa.core.*;
import cn.ubingo.security.rsa.data.*;
import cn.ubingo.security.rsa.key.*;
/*
陈服建(fochen,j@ubingo.cn)
2015-01-23
*/
public class Program {
public static void main(String[] args) throws Exception
{
//生成密钥对
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(); //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
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("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCZw5tyBNJsjgVrPkLUIWF9el5EOXL1AydVBnJ/WWPKTZJBWYxudW+1jI5ifML+1DkCxw/2QLowzViV1OLnpNAWr7zvLpN6i7OZtq0o2Yfc+vv6vCctTpMvFartRRwDeXTRBgoGd71UwWgdMOu8Gmr9sv5u3C9Kj5fhj0I4WgB94wIDAQAB",core\KeyFormat::ASN);
$privateWorker = new data\KeyWorker("MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJnDm3IE0myOBWs+QtQhYX16XkQ5cvUDJ1UGcn9ZY8pNkkFZjG51b7WMjmJ8wv7UOQLHD/ZAujDNWJXU4uek0BavvO8uk3qLs5m2rSjZh9z6+/q8Jy1Oky8Vqu1FHAN5dNEGCgZ3vVTBaB0w67waav2y/m7cL0qPl+GPQjhaAH3jAgMBAAECgYA6ANHYlv0RuhlNNTVcdCMkhE6fCdTVnBTwO/hhFcVRASYUxMT2vhIfuB/WAx5DpqbC53ib+hrCYhPyoVXe8AsWjgBJcj3RHxjLMou0Jbu6mTjcLgKzMYM85yHscmmDcc8l/ep9BhbiJJsTVM8RqqXfdPmY5CAgfwsNXruC7Zb5wQJBAMxVZhR+IzoeYLsmEciy2oOaTq5u8O6yjx7RapIggIKTdRU5G2GeCyLRHyZH/+U2/nL3undiP74fxizatzgTE3ECQQDApNKzaMobPW23wResE+dDjJxYnqVqOmZS22D6s+BKwDmHpOt+oJmrirY4LWmOUwUfgAboDWH5y/q5+qJHP5STAkEAux+F4UR2nDXPnfPKG4L3K8f3QDUm/WGWQcHEF9gd9/Z0JaBrm+TxC8x4+0S6ar4HHWASalwWRdWxVchiO770cQJAGaEUAxhq4wreIPdIffU77Em1tziMC0DvwhA7q77olSlTvg8b4YHeT+spaPnptCypXtJ6mL7HDSOtHLcSheYYjwJBAJFdK3vgZS0Sk4oy6iOaOcux+5uBkpF9ran0GIpehyGzia+F1Xf/+NnS22qER/ADvRMNnyHfQH24qWxQuST39UQ=",core\KeyFormat::ASN); echo $publicWorker->decrypt($privateWorker->encrypt("你好!世界"));
echo $privateWorker->decrypt($publicWorker->encrypt("你好!中国")); ?>
四、相关附件
编译包:c#(4.0,dll) java(1.6,jar) php(5.3,php)
源代码:c#(4.0,visual studio 2012) java(1.6,eclipse 4.4.1) php(5.3,zend studio 12.0)
rsa互通密钥对生成及互通加解密(c#,java,php)的更多相关文章
-
AES加解密异常java.security.InvalidKeyException: Illegal key size
AES加解密异常 Java后台AES解密,抛出异常如下:java.security.InvalidKeyException: Illegal key size Illegal key size or ...
-
python实现RSA加密和签名以及分段加解密的方案
1.前言 很多朋友在工作中,会遇到一些接口使用RSA加密和签名来处理的请求参数,那么遇到这个问题的时候,第一时间当然是找开发要加解密的方法,但是开发给加解密代码,大多数情况都是java,c++,js等 ...
-
3Des加解密 C#--->;Java
Java的Byte范围为-128至127,c#的Byte范围是0-255.核心是确定Mode和Padding,关于这两个的意思可以搜索3DES算法相关文章. C#采用CBC Mode,PKCS7 Pa ...
-
java 使用pem密钥进行RSA加解密
1.使用openssl生成私钥和公钥 openssl下载地址:http://www.openssl.org/source openssl生成私钥命令: genrsa -out rsa_private ...
-
RSA,AES加解密算法的实现
目录 Python实现RSA公钥加密算法 RSA公钥加密算法原理 RSA算法的Python实现 AES加解密算法实现 AES加解密算法原理 AES加解密算法Python实现 参考文献 Python实现 ...
-
Java拓展教程:文件DES加解密
Java拓展教程:文件加解密 Java中的加密解密技术 加密技术根据一般可以分为对称加密技术和非对称加密技术.对称加密技术属于传统的加密技术,它的加密和解密的密钥是相同的,它的优点是:运算速度快,加密 ...
-
aes加解密前后端-后台
一.web.xml: <filter> <filter-name>fastLoginFilter</filter-name> <filter-class> ...
-
[C#.NET] 使用 X509 數位電子簽章 加解密
摘自: http://www.dotblogs.com.tw/yc421206/archive/2012/06/30/73150.aspx 上篇提到了使用X509的的憑證使用方式,請參考 [C#.NE ...
-
Des加解密(Java端和Js端配套)解析
一.什么是DES加密 des对称加密,对称加密,是一种比较传统的加密方式,其加密运算.解密运算使用的是同样的密钥,信息的发送者和信息的接收者在进行信息的传输与处理时,必须共同持有该密码( ...
随机推荐
-
VTK初学一,动画加AVI录制终于做出来了
#ifndef INITIAL_OPENGL #define INITIAL_OPENGL #include <vtkAutoInit.h> VTK_MODULE_INIT(vtkRe ...
-
flex_高度补全
一个高度为100px, 另外一个高度自动补全当前界面下剩余的高度: <!DOCTYPE html> <html lang="en"> <head> ...
-
SQL 时间处理
1.获取当前时间 GetDate() 2.获取当前年.月.日 DATEPART(yyyy,GetDate()).DATEPART(m,GetDate()).DATEPART(d,GetDate()) ...
-
计算两条直线的交点(C#)
PS:从其他地方看到的源码是有问题的.下面是修正后的 /// <summary> /// 计算两条直线的交点 /// </summary> /// <param name ...
-
UE4 unreliable 同步问题
今天发现了一个问题,标记为unreliable的函数从来不执行,但是官方文档上的说明是只有在网络负载重时才不执行此类函数,哎哎哎.
-
js 检查登录态方法封装(闭包、状态缓存)
前端页面开发时,经常需要异步校验登录态,每次都重新copy之前写的方法,比较繁琐不好维护,固将登录态校验封装成一个js. (function(){ //登录状态 1 登录态有效 2 登录态无效 3 请 ...
-
【CSS小技巧】 用 CSS 实现一个宽高等比自适应容器
不知道怎么起个标题能更加清楚的说清楚我的意图,那就打个比方吧:比如我们在手机端上放一张宽度 100% 的图片,我们如果不设置图片的高度,那么这个图片会根据图片的原始尺寸等比缩放.今天我们要讲的就是实现 ...
-
Ubuntu16.04下Hive的安装与配置
一.系统环境 os : Ubuntu 16.04 LTS 64bit jdk : 1.8.0_161 hadoop : 2.6.4mysql : 5.7.21 hive : 2.1.0 在配置hive ...
-
Android UI-仿微信底部导航栏布局
现在App基本的标配除了侧滑菜单,还有一个就是底部导航栏,常见的聊天工具QQ,微信,购物App都有底部导航栏,用户可以随便切换看不同的内容,说是情怀也好,用户体验也罢.我们开发的主要的还是讲的是如何如 ...
-
Matrix Walk CodeForces - 954C
题意: 就是给出一连串的数字 这些数字是从第一个数字依次走过的 emm..就是这样.. 然后让你判断这个矩阵是否存在 如果存在输出行和列的数量 其中行..开到最大就好了...主要是判断列 在输入 ...