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#
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,[email protected])
    /// 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,[email protected])
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)

时间: 2024-10-09 21:50:41

rsa互通密钥对生成及互通加解密(c#,java,php)的相关文章

python实现RSA加密和签名以及分段加解密的方案

python实现RSA加解密和签名加解签 1.生成秘钥对 在这边为了方面演示,手动生成一个密钥对(项目中的秘钥对由开发来生成,会直接给到我们) 生成秘钥对的时候,可以指定生成秘钥的长度,一般推荐使用1024bit, 1024bit的rsa公钥,加密数据时,最多只能加密117byte的数据),数据量超过这个数,则需要对数据进行分段加密,但是目前1024bit长度的秘钥已经被证明了不够安全,尽量使用2048bit长度的秘钥.2048bit长度的秘钥,最多245byte长度的数据 计算公式如下: 秘钥

微信公众平台消息体签名及加解密实例(Java)

前言: 最近在研究微信公众平台的开发,玩得不亦乐乎.基本的回复功能已经实现了,而且回复用到了图灵机器人的接口.其实图灵机器人已经有微信接口可以直接调用.如果项目的需要,想要做个性化需求的话,用这种方式是行不通的.我现在的解决方案是,我开发的应用A作为中间层,连接微信接口与图灵机器人接口.有点扯远了,如题,微信官方为了更高的安全性,10月份推出了消息体签名验证.网上关于此项的实例不多,其实根据官方的例子,重新封装一下,就可以了. 一.准备工作: 1.已申请了相关的订阅号或者服务号(可以用虚拟器,具

java 使用pem密钥进行RSA加解密

1.使用openssl生成私钥和公钥 openssl下载地址:http://www.openssl.org/source openssl生成私钥命令:  genrsa -out rsa_private_key.pem 1024 openssl生成公钥命令:  rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem 2.此时在openssl安装目录下的bin文件夹可以看到 rsa_private_key.pem 和 rsa_publi

Java 进行 RSA 加解密的例子

加密是保证数据安全的手段之一.加密是将纯文本数据转换为难以理解的密文:解密是将密文转换回纯文本.数据的加解密属于密码学的范畴.通常,加密和解密都需要使用一些秘密信息,这些秘密信息叫做密钥,将纯文本转为密文或者转回的时候都要用到这些密钥.对称加密指的是发送者和接收者共用同一个密钥的加解密方法.非对称加密(又称公钥加密)指的是需要一个私有密钥一个公开密钥,两个不同的密钥的加解密体系.尽管不同,这个密钥对的这两个部分在算法上是有关联的.一个密钥将纯文本加密,另一个将密文解密.没有一个密钥能够把加密和加

各种加解密算法比較

二.          加密算法介绍 对称加密算法 对称加密算法用来对敏感数据等信息进行加密,经常使用的算法包含: DES(Data Encryption Standard):数据加密标准,速度较快,适用于加密大量数据的场合. 3DES(Triple DES):是基于DES,对一块数据用三个不同的密钥进行三次加密,强度更高. AES(Advanced Encryption Standard):高级加密标准,是下一代的加密算法标准,速度快,安全级别高: AES与3DES的比較 算法名称 算法类型

【转】各种加解密算法比较

转自: http://blog.csdn.net/pengzp/article/details/6556674 二.          加密算法介绍 对称加密算法 对称加密算法用来对敏感数据等信息进行加密,常用的算法包括: DES(Data Encryption Standard):数据加密标准,速度较快,适用于加密大量数据的场合. 3DES(Triple DES):是基于DES,对一块数据用三个不同的密钥进行三次加密,强度更高. AES(Advanced Encryption Standard

aes加解密前后端-后台

一.web.xml: <filter> <filter-name>fastLoginFilter</filter-name> <filter-class>com.shencai.xf.common.util.FastLoginFilter</filter-class> </filter> <filter-mapping> <filter-name>fastLoginFilter</filter-name&

RSA在Android、vc、java下加解密实现互通。

最近公司项目需求要对上传/下载的数据进行AES+RSA的加解密需求,客户有vc的RSA加解密接口,而且说要与他们的系统兼容,也就是说vc下要用他们的模块实现加解密.期间过程有几个坑,原因都是自己的对这些加密的协议/准则/规范不熟,以此文记录备案. 坑1: 首先先是百度java的RSAUtils,网上很多现成的,例如这个:http://www.2cto.com/kf/201408/328112.html,公钥使用X509的解析类,私钥使用PKCS#8的解析类.编写测试程序一切正常... 然后就是调

全面解决.Net与Java互通时的RSA加解密问题,使用PEM格式的密钥文件

一.缘由 RSA是一种常用的非对称加密算法.所以有时需要在不用编程语言中分别使用RSA的加密.解密.例如用Java做后台服务端,用C#开发桌面的客户端软件时.由于 .Net.Java 的RSA类库存在很多细节区别,尤其是它们支持的密钥格式不同.导致容易出现“我加密的数据对方不能解密,对方加密的数据我不能解密,但是自身是可以正常加密解密”等情况.虽然网上已经有很多文章讨论 .Net与Java互通的RSA加解密,但是存在不够全面.需要第三方dll.方案复杂 等问题.于是我仔细研究了这一课题,得到了一