• 再来个封装得更好的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++ Primer注意事项11_运算符重载_算术/关系运算符_下标运算符
    android最新的工具DateHelper
    ssh否password登陆server
    atitit.设计模式(2) -----查询方式/ command 总结
    采用Eclipse中间Maven构建Web项目错误(一)
    dm8148 jpeg编解码器测试
    C++ 结构体和类的区别
    C++ const
    C++中的inline函数
    C++ 模板类demo
  • 原文地址:https://www.cnblogs.com/theluther/p/5380813.html
Copyright © 2020-2023  润新知