再来个封装得更好的RSAHelper

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怎么用的,就忽视了这么好的一个东西了。这是自己对原博客内容的简单整理,用起来真是超爽,太方便了

posted @ 2016-04-12 00:09  TheLuther  阅读(1476)  评论(0编辑  收藏  举报