您的位置:首页 > 其它

再来个封装得更好的RSAHelper

2016-04-12 00:09 501 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cn.Ubingo.Security.RSA.Core;
using Cn.Ubingo.Security.RSA.Data;
using Cn.Ubingo.Security.RSA.Key;

/// <summary>
/// 类说明:RSA类库,主要解决Net(c#)、ASN(Java)、PEM(PHP)三种格式相互转换使用的问题
/// 编码日期:2016-04-10
/// 编 码 人:TheLuther
/// 引用网址:http://www.cnblogs.com/FoChen/p/4740814.html
/// 修改日期:2016-04-10
/// </summary>
class RSAHelper
{
public static KeyPair keypair = null;

public static KeyPair PEM2XML(KeyPair PEMKeyPair, KeyPair XMLKeyPair) {
return PEMKeyPair.ToXMLKeyPair();
}

public static KeyPair ASN2XML(KeyPair ASNKeyPair, KeyPair XMLPair) {
return ASNKeyPair.ToXMLKeyPair();
}

public static KeyPair XML2PEM(KeyPair XMLKeyPair, KeyPair PEMKeyPair) {
return XMLKeyPair.ToPEMKeyPair();
}

public static KeyPair XML2ASN(KeyPair XMLPair, KeyPair ASNKeyPair)
{
return XMLPair.ToASNKeyPair();
}

public static KeyPair CreatePair() {
return KeyGenerator.GenerateKeyPair();
}

public static string Encrypt(string data,string key,KeyFormat format) {
KeyWorker worker = new KeyWorker(key, format);
return worker.Encrypt(data);
}

public static string DeEncrypt(string data, string key, KeyFormat format) {
KeyWorker worker = new KeyWorker(key, format);
return worker.Decrypt(data);
}

public static void PEMDemo() {
KeyPair pair = CreatePair().ToPEMKeyPair();
string data = "test";
KeyWorker worker = new KeyWorker(pair.PrivateKey,KeyFormat.PEM);
string encryptdata = worker.Encrypt(data);

worker = new KeyWorker(pair.PublicKey,KeyFormat.PEM);
string deencryptdata = worker.Decrypt(encryptdata);

}

}


刚开始研究RSA的时候没注意,看到的大部分资料都是讲Cipher怎么用的,就忽视了这么好的一个东西了。这是自己对原博客内容的简单整理,用起来真是超爽,太方便了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: