/// <summary>
    /// RSA加密解密及RSA签名和验证
    /// </summary>
    public class RSADE
    {         
        public RSADE()
        {             
        }

#region RSA 加密解密

#region RSA 的密钥产生
    
        /// <summary>
        /// RSA 的密钥产生 产生私钥 和公钥
        /// </summary>
        /// <param name="xmlKeys"></param>
        /// <param name="xmlPublicKey"></param>
        public void RSAKey(out string xmlKeys,out string xmlPublicKey)
        {             
                System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
                xmlKeys=rsa.ToXmlString(true);
                xmlPublicKey = rsa.ToXmlString(false);             
        }
        #endregion

#region RSA的加密函数
        //##############################################################################
        //RSA 方式加密
        //说明KEY必须是XML的行式,返回的是字符串
        //在有一点需要说明!!该加密方式有 长度 限制的!!
        //##############################################################################

//RSA的加密函数  string
        public string RSAEncrypt(string xmlPublicKey,string m_strEncryptString )
        {
            
            byte[] PlainTextBArray;
            byte[] CypherTextBArray;
            string Result;
            RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
            rsa.FromXmlString(xmlPublicKey);
            PlainTextBArray = (new UnicodeEncoding()).GetBytes(m_strEncryptString);
            CypherTextBArray = rsa.Encrypt(PlainTextBArray, false);
            Result=Convert.ToBase64String(CypherTextBArray);
            return Result;
            
        }
        //RSA的加密函数 byte[]
        public string RSAEncrypt(string xmlPublicKey,byte[] EncryptString )
        {
            
            byte[] CypherTextBArray;
            string Result;
            RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
            rsa.FromXmlString(xmlPublicKey);
            CypherTextBArray = rsa.Encrypt(EncryptString, false);
            Result=Convert.ToBase64String(CypherTextBArray);
            return Result;
            
        }
        #endregion

#region RSA的解密函数
        //RSA的解密函数  string
        public string RSADecrypt(string xmlPrivateKey, string m_strDecryptString )
        {            
            byte[] PlainTextBArray;
            byte[] DypherTextBArray;
            string Result;
            System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
            rsa.FromXmlString(xmlPrivateKey);
            PlainTextBArray =Convert.FromBase64String(m_strDecryptString);
            DypherTextBArray=rsa.Decrypt(PlainTextBArray, false);
            Result=(new UnicodeEncoding()).GetString(DypherTextBArray);
            return Result;
            
        }

//RSA的解密函数  byte
        public string RSADecrypt(string xmlPrivateKey, byte[] DecryptString )
        {            
            byte[] DypherTextBArray;
            string Result;
            System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
            rsa.FromXmlString(xmlPrivateKey);
            DypherTextBArray=rsa.Decrypt(DecryptString, false);
            Result=(new UnicodeEncoding()).GetString(DypherTextBArray);
            return Result;
            
        }
        #endregion

#endregion

#region RSA数字签名

#region 获取Hash描述表
        //获取Hash描述表
        public bool GetHash(string m_strSource, ref byte[] HashData)
        {             
            //从字符串中取得Hash描述
            byte[] Buffer;
            System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
            Buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(m_strSource);
            HashData = MD5.ComputeHash(Buffer);

return true;             
        }

//获取Hash描述表
        public bool GetHash(string m_strSource, ref string strHashData)
        {
            
            //从字符串中取得Hash描述
            byte[] Buffer;
            byte[] HashData;
            System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
            Buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(m_strSource);
            HashData = MD5.ComputeHash(Buffer);

strHashData = Convert.ToBase64String(HashData);
            return true;
            
        }

//获取Hash描述表
        public bool GetHash(System.IO.FileStream objFile, ref byte[] HashData)
        {
            
            //从文件中取得Hash描述
            System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
            HashData = MD5.ComputeHash(objFile);
            objFile.Close();

return true;
            
        }

//获取Hash描述表
        public bool GetHash(System.IO.FileStream objFile, ref string strHashData)
        {
            
            //从文件中取得Hash描述
            byte[] HashData;
            System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
            HashData = MD5.ComputeHash(objFile);
            objFile.Close();

strHashData = Convert.ToBase64String(HashData);

return true;
            
        }
        #endregion

#region RSA签名
        //RSA签名
        public bool SignatureFormatter(string p_strKeyPrivate, byte[] HashbyteSignature, ref byte[] EncryptedSignatureData)
        {
            
                System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();

RSA.FromXmlString(p_strKeyPrivate);
                System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
                //设置签名的算法为MD5
                RSAFormatter.SetHashAlgorithm("MD5");
                //执行签名
                EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);

return true;
            
        }

//RSA签名
        public bool SignatureFormatter(string p_strKeyPrivate, byte[] HashbyteSignature, ref string m_strEncryptedSignatureData)
        {
            
                byte[] EncryptedSignatureData;

System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();

RSA.FromXmlString(p_strKeyPrivate);
                System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
                //设置签名的算法为MD5
                RSAFormatter.SetHashAlgorithm("MD5");
                //执行签名
                EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);

m_strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData);

return true;
            
        }

//RSA签名
        public bool SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature, ref byte[] EncryptedSignatureData)
        {
            
                byte[] HashbyteSignature;

HashbyteSignature = Convert.FromBase64String(m_strHashbyteSignature);
                System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();

RSA.FromXmlString(p_strKeyPrivate);
                System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
                //设置签名的算法为MD5
                RSAFormatter.SetHashAlgorithm("MD5");
                //执行签名
                EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);

return true;
            
        }

//RSA签名
        public bool SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature, ref string m_strEncryptedSignatureData)
        {
            
                byte[] HashbyteSignature;
                byte[] EncryptedSignatureData;

HashbyteSignature = Convert.FromBase64String(m_strHashbyteSignature);
                System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();

RSA.FromXmlString(p_strKeyPrivate);
                System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
                //设置签名的算法为MD5
                RSAFormatter.SetHashAlgorithm("MD5");
                //执行签名
                EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);

m_strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData);

return true;
            
        }
        #endregion

#region RSA 签名验证

public bool SignatureDeformatter(string p_strKeyPublic, byte[] HashbyteDeformatter, byte[] DeformatterData)
        {
            
                System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();

RSA.FromXmlString(p_strKeyPublic);
                System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
                //指定解密的时候HASH算法为MD5
                RSADeformatter.SetHashAlgorithm("MD5");

if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            
        }

public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, byte[] DeformatterData)
        {
            
                byte[] HashbyteDeformatter;

HashbyteDeformatter = Convert.FromBase64String(p_strHashbyteDeformatter);

System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();

RSA.FromXmlString(p_strKeyPublic);
                System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
                //指定解密的时候HASH算法为MD5
                RSADeformatter.SetHashAlgorithm("MD5");

if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            
        }

public bool SignatureDeformatter(string p_strKeyPublic, byte[] HashbyteDeformatter, string p_strDeformatterData)
        {
            
                byte[] DeformatterData;

System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();

RSA.FromXmlString(p_strKeyPublic);
                System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
                //指定解密的时候HASH算法为MD5
                RSADeformatter.SetHashAlgorithm("MD5");

DeformatterData =Convert.FromBase64String(p_strDeformatterData);

if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            
        }

public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, string p_strDeformatterData)
        {
            
                byte[] DeformatterData;
                byte[] HashbyteDeformatter;

HashbyteDeformatter = Convert.FromBase64String(p_strHashbyteDeformatter);
                System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();

RSA.FromXmlString(p_strKeyPublic);
                System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
                //指定解密的时候HASH算法为MD5
                RSADeformatter.SetHashAlgorithm("MD5");

DeformatterData =Convert.FromBase64String(p_strDeformatterData);

if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            
        }

#endregion

#endregion

#region 获取加密解密证书
        /// <summary>
        /// 取得证书私钥
        /// </summary>
        /// <param name="pfxPath">证书的绝对路径</param>
        /// <param name="password">访问证书的密码</param>
        /// <returns></returns>
        public static String GetPrivateKey( string pfxPath, string password )
        {
            X509Certificate2 pfx = new X509Certificate2( pfxPath, password, X509KeyStorageFlags.Exportable );
            string privateKey = pfx.PrivateKey.ToXmlString( true );
            return privateKey;
        }

/// <summary>
        /// 取得证书的公钥
        /// </summary>
        /// <param name="cerPath">证书的绝对路径</param>
        /// <returns></returns>
        public static String GetPublicKey( string cerPath )
        {
            X509Certificate2 cer = new X509Certificate2( cerPath );
            string publicKey = cer.PublicKey.Key.ToXmlString( false );
            return publicKey;
        }
        #endregion
    }

RSA加密解密和读取公钥、私钥的更多相关文章

  1. c# RSA 加密解密 java.net公钥私钥转换 要解密的模块大于128字节

    有一个和接口对接的任务,对方使用的是java,我方使用的是c#,接口加密类型为RSA,公钥加密私钥解密. 然后就是解决各种问题. 1.转换对方的密钥字符串 由于c#里面需要使用的是xml各式的密钥字符 ...

  2. WP8.1 RSA 加解密实例(导入公钥私钥)

    因项目上需要用到,之前在WP8.0的环境上调试通过,现在在开发8.1时发现已不支持原来的加密库,所以无法使用以前的方法,不得已,去寻找windows命名空间下RSA的加解密方法,经过几天的尝试,将解决 ...

  3. C# 与JAVA 的RSA 加密解密交互,互通,C#使用BouncyCastle来实现私钥加密,公钥解密的方法

    因为C#的RSA加密解密只有公钥加密,私钥解密,没有私钥加密,公钥解密.在网上查了很久也没有很好的实现.BouncyCastle的文档少之又少.很多人可能会说,C#也是可以的,通过Biginteger ...

  4. RSA 加密 解密 公钥 私钥 签名 加签 验签

    http://blog.csdn.net/21aspnet/article/details/7249401# http://www.ruanyifeng.com/blog/2013/06/rsa_al ...

  5. C# Java间进行RSA加密解密交互(二)

    原文:C# Java间进行RSA加密解密交互(二) 接着前面一篇文章C# Java间进行RSA加密解密交互,继续探讨这个问题. 在前面,虽然已经实现了C# Java间进行RSA加密解密交互,但是还是与 ...

  6. C# Java间进行RSA加密解密交互(三)

    原文:C# Java间进行RSA加密解密交互(三) 接着前面一篇C# Java间进行RSA加密解密交互(二)说吧,在上篇中为了实现 /** * RSA加密 * @param text--待加密的明文 ...

  7. python rsa 加密解密 (编解码,base64编解码)

    最近有需求,需要研究一下RSA加密解密安全:在网上百度了一下例子文章,很少有文章介绍怎么保存.传输.打印加密后的文本信息,都是千篇一律的.直接在一个脚本,加密后的文本信息赋于变量,然后立马调用解密.仔 ...

  8. RSA加密解密及RSA加签验签

    RSA安全性应用场景说明 在刚接触RSA的时候,会混淆RSA加密解密和RSA加签验签的概念.简单来说加密解密是公钥加密私钥解密,持有公钥(多人持有)可以对数据加密,但是只有持有私钥(一人持有)才可以解 ...

  9. Java使用RSA加密解密签名及校验

    RSA加密解密类: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 ...

随机推荐

  1. vi命令提示:Terminal too wide

    putty: 在我的电脑上,缺省的设置是这样的: localhost:~ eygle$ stty -aspeed 9600 baud; 51 rows; 171 columns; 在远程编辑文件时,减 ...

  2. Windows NT 技术简介

    Windows NT 技术简介 NT:New Technoly(新技术,因比DOS.WIN9X采用了很多新技术而得名) Windows NT基本介绍 WindowsNT是Microsoft推出的面向工 ...

  3. 幻世(OurDream)2D图形引擎使用教程9——处理操作输入(3)

    声明:本教程版权归Lizcst Software Lab所有,欢迎转载,但是转载必须保留本段声明文字,并注明文章来源:http://blog.csdn.net/kflizcst 谢谢合作! 现在我们该 ...

  4. 基于visual Studio2013解决C语言竞赛题之1091多项式

        题目 解决代码及点评 /************************************************************************/ /* ...

  5. ANTLR4权威參考手冊(一)

    写在前面的话: 此文档是对伟大的Terence Parr的著作<the definitive antlr4 reference>的翻译本.致敬!欢迎转载,请注明原地址,请尊重劳动成果.翻译 ...

  6. 碰撞回避算法(一) Velocity Obstacle

    碰撞回避是机器人导航,游戏AI等领域的基础课题.几十年来,有很多算法被提出.注意这里主要指的是局部的碰撞回避算法.尽管和全局的路径规划算法(A*算法等)有千丝万缕的联系.可是还是有所不同的(局部的碰撞 ...

  7. 最长公共子序列python实现

    最长公共子序列是动态规划基本题目,以下依照动态规划基本步骤解出来. 1.找出最优解的性质,并刻划其结构特征 序列a共同拥有m个元素,序列b共同拥有n个元素,假设a[m-1]==b[n-1],那么a[: ...

  8. AssertValid函数学�

    转自http://tsitao.blog.163.com/blog/static/29795822006914105840496/ VC的调试中,AssertValid和Dump函数的应用 CObje ...

  9. 窗口绘制有关的消息整理 WM_PAINT, WM_NCPAINT, WM_ERASEBKGND

    WM_PAINTWM_PAINT是Windows窗口系统中一条重要的消息,应用程序通过处理该消息实现在窗口上的绘制工作. WM_NCPAINT当窗口客户区以外的部分(如窗口标题栏.菜单栏等)需要需要重 ...

  10. 了解sota字符界面(章节4.1)

    4 SOTA操作 4.1 SOTA字符界面 sotaCC是字符界面管理sota系统程序 . 在/.../sota/bin/目录下,启动sotaCC.在终端的该目录下输入指令“./sotaCC”,启动s ...