zoukankan      html  css  js  c++  java
  • 再来个封装得更好的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怎么用的,就忽视了这么好的一个东西了。这是自己对原博客内容的简单整理,用起来真是超爽,太方便了

  • 相关阅读:
    机器学习 —— 概率图模型(学习:最大似然估计)
    机器学习 —— 概率图模型(学习:综述)
    机器学习 —— 概率图模型(推理:决策)
    机器学习 —— 概率图模型(推理:连续时间模型)
    我的c++学习(3)字符的输入输出
    法国人的浪漫精神从在于他们的灵魂之中(转)
    我的c++学习(2)比较两个数字大小
    我的c++学习(1)hello world!
    获取datable中某行某列的数据
    学习资源asp.net
  • 原文地址:https://www.cnblogs.com/theluther/p/5380813.html
Copyright © 2011-2022 走看看