zoukankan      html  css  js  c++  java
  • RSA加解密

    using System;
    using System.Text;
    using System.Security.Cryptography;
    using System.IO;
    using System.Security.Cryptography.X509Certificates;
    using System.Xml;
    using Org.BouncyCastle.Math;
    using Org.BouncyCastle.Crypto.Parameters;
    using Org.BouncyCastle.Asn1.X509;
    using Org.BouncyCastle.X509;
    using Org.BouncyCastle.Security;
    using Org.BouncyCastle.Asn1.Pkcs;
    using Org.BouncyCastle.Pkcs;
    
    namespace Cn.Vcredit.PayPlatform.Platforms.Support.MSBank.Common
    {
        /// <summary> 
        /// RSA加密解密及RSA签名和验证
        /// </summary> 
        public static class RSACryption
        {
            //RSA 的密钥产生 产生私钥 和公钥 
            public static void RSAKey(out string xmlKeys, out string xmlPublicKey)
            {
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                xmlKeys = rsa.ToXmlString(true);
                xmlPublicKey = rsa.ToXmlString(false);
            }
    
            //RSA的加密函数
            public static byte[] RSAEncrypt(string xmlPublicKey, string m_strEncryptString)
            {
                byte[] PlainTextBArray = Encoding.UTF8.GetBytes(m_strEncryptString);
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                rsa.FromXmlString(xmlPublicKey);
    
                int bufferSize = (rsa.KeySize / 8) - 11;//单块最大长度
                var buffer = new byte[bufferSize];
                using (MemoryStream inputStream = new MemoryStream(PlainTextBArray), outputStream = new MemoryStream())
                {
                    while (true)
                    {
                        //分段加密
                        int readSize = inputStream.Read(buffer, 0, bufferSize);
                        if (readSize <= 0)
                        {
                            break;
                        }
                        var temp = new byte[readSize];
                        Array.Copy(buffer, 0, temp, 0, readSize);
                        var encryptedBytes = rsa.Encrypt(temp, false);
                        outputStream.Write(encryptedBytes, 0, encryptedBytes.Length);
                    }
                    rsa.Dispose();
                    return outputStream.ToArray();//转化为字节流方便传输
                }
            }
    
            //RSA的加密函数
            public static string RSAEncrypt(string xmlPublicKey, byte[] EncryptString)
            {
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                rsa.FromXmlString(xmlPublicKey);
    
                int bufferSize = (rsa.KeySize / 8) - 11;//单块最大长度
                var buffer = new byte[bufferSize];
                using (MemoryStream inputStream = new MemoryStream(EncryptString), outputStream = new MemoryStream())
                {
                    while (true)
                    {
                        //分段加密
                        int readSize = inputStream.Read(buffer, 0, bufferSize);
                        if (readSize <= 0)
                        {
                            break;
                        }
                        var temp = new byte[readSize];
                        Array.Copy(buffer, 0, temp, 0, readSize);
                        var encryptedBytes = rsa.Encrypt(temp, false);
                        outputStream.Write(encryptedBytes, 0, encryptedBytes.Length);
                    }
                    rsa.Dispose();
                    return Convert.ToBase64String(outputStream.ToArray());//转化为字节流方便传输
                }
            }
    
            //RSA的解密函数
            public static string RSADecrypt(string xmlPrivateKey, string m_strDecryptString)
            {
                byte[] PlainTextBArray = Convert.FromBase64String(m_strDecryptString);
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                rsa.FromXmlString(xmlPrivateKey);
    
                int bufferSize = rsa.KeySize / 8;
                var buffer = new byte[bufferSize];
                using (MemoryStream inputStream = new MemoryStream(PlainTextBArray), outputStream = new MemoryStream())
                {
                    while (true)
                    {
                        int readSize = inputStream.Read(buffer, 0, bufferSize);
                        if (readSize <= 0)
                        {
                            break;
                        }
                        var temp = new byte[readSize];
                        Array.Copy(buffer, 0, temp, 0, readSize);
                        var rawBytes = rsa.Decrypt(temp, false);
                        outputStream.Write(rawBytes, 0, rawBytes.Length);
                    }
                    rsa.Dispose();
                    return Encoding.UTF8.GetString(outputStream.ToArray());
                }
            }
    
            //RSA的解密函数
            public static string RSADecrypt(string xmlPrivateKey, byte[] DecryptString)
            {
                //byte[] PlainTextBArray = Convert.FromBase64String(m_strDecryptString);
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                rsa.FromXmlString(xmlPrivateKey);
    
                int bufferSize = rsa.KeySize / 8;
                var buffer = new byte[bufferSize];
                using (MemoryStream inputStream = new MemoryStream(DecryptString), outputStream = new MemoryStream())
                {
                    while (true)
                    {
                        int readSize = inputStream.Read(buffer, 0, bufferSize);
                        if (readSize <= 0)
                        {
                            break;
                        }
                        var temp = new byte[readSize];
                        Array.Copy(buffer, 0, temp, 0, readSize);
                        var rawBytes = rsa.Decrypt(temp, false);
                        outputStream.Write(rawBytes, 0, rawBytes.Length);
                    }
                    rsa.Dispose();
                    return Encoding.UTF8.GetString(outputStream.ToArray());
                }
            }
    
            //获取Hash描述表
            public static byte[] GetMD5Hash(string m_strSource)
            {
                //从字符串中取得Hash描述 
                byte[] Buffer;
                HashAlgorithm MD5 = HashAlgorithm.Create("MD5");
                Buffer = Encoding.UTF8.GetBytes(m_strSource);
                return MD5.ComputeHash(Buffer);
            }
    
            //SHA1签名和java中的SHA1withRSA加密是一样的
            public static byte[] GetSHA1Hash(string PlainText)
            {
                SHA1 sha1Hasher = SHA1.Create();
                byte[] data = sha1Hasher.ComputeHash(Encoding.UTF8.GetBytes(PlainText));
                return data;
            }
    
            //RSA签名
            public static byte[] RSASignData(string p_strKeyPrivate, byte[] HashbyteSignature)
            {
                RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
                RSA.FromXmlString(p_strKeyPrivate);
                RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(RSA);
                //设置签名的算法为MD5 
                //RSAFormatter.SetHashAlgorithm("MD5");
                RSAFormatter.SetHashAlgorithm("SHA1");
                //执行签名 
                byte[] EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
                return EncryptedSignatureData;
            }
    
            //签名验证
            public static bool RSAVerifySign(string p_strKeyPublic, byte[] HashbyteDeformatter, byte[] DeformatterData)
            {
                RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
                RSA.FromXmlString(p_strKeyPublic);
                RSAPKCS1SignatureDeformatter RSADeformatter = new RSAPKCS1SignatureDeformatter(RSA);
                //指定解密的时候HASH算法为MD5 
                //RSADeformatter.SetHashAlgorithm("MD5");
                RSADeformatter.SetHashAlgorithm("SHA1");
                if (RSADeformatter.VerifySignature(HashbyteDeformatter, DeformatterData))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
    
            //先记下了,将来可能有用
            //https://social.msdn.microsoft.com/Forums/zh-CN/c39f2298-ffe3-48d9-ad1e-ababa122d229/sha1-with-rsa-in-c?forum=netfxbcl
            private static string GetRSAHash(string PlainText)
            {
                string ResultString = "";
                string DigitalCertificateName = "C=IE, O=ad, OU=1234567, CN=f";
                X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
                store.Open(OpenFlags.ReadOnly);
                X509CertificateCollection collection = store.Certificates;
                foreach (System.Security.Cryptography.X509Certificates.X509Certificate cert in collection)
                {
                    if (cert.Subject == DigitalCertificateName)
                    {
                        CspParameters CspParam;
                        string publicXmlString = string.Empty;
                        string privateXmlString = string.Empty;
                        RSACryptoServiceProvider RsaCsp;
                        RSACryptoServiceProvider RsaCsp2;
                        UnicodeEncoding ByteConverter = new UnicodeEncoding();
                        CspParam = new CspParameters();
                        CspParam.KeyContainerName = cert.Subject; ;
                        CspParam.Flags = CspProviderFlags.UseMachineKeyStore;
                        byte[] encryptedString = ByteConverter.GetBytes(PlainText);
                        RsaCsp = new RSACryptoServiceProvider(CspParam);
                        //Get private key
                        privateXmlString = RsaCsp.ToXmlString(true);
                        RsaCsp2 = new RSACryptoServiceProvider();
                        RsaCsp2.FromXmlString(privateXmlString);
                        encryptedString = RsaCsp2.Encrypt(System.Text.Encoding.Unicode.GetBytes(PlainText), false);
                        ResultString = Convert.ToBase64String(encryptedString);
                    }
                }
                store.Close();
                return ResultString;
            }
    
        }
    
        /// <summary>
        /// java c#密钥互转
        /// </summary>
        public static class RSAKeyConvert
        {
            /// <summary>
            /// RSA私钥格式转换,java->.net
            /// </summary>
            /// <param name="privateKey">java生成的RSA私钥</param>
            /// <returns></returns>
            public static string RSAPrivateKeyJava2DotNet(string privateKey)
            {
                RsaPrivateCrtKeyParameters privateKeyParam = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateKey));
                return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent><P>{2}</P><Q>{3}</Q><DP>{4}</DP><DQ>{5}</DQ><InverseQ>{6}</InverseQ><D>{7}</D></RSAKeyValue>",
                    Convert.ToBase64String(privateKeyParam.Modulus.ToByteArrayUnsigned()),
                    Convert.ToBase64String(privateKeyParam.PublicExponent.ToByteArrayUnsigned()),
                    Convert.ToBase64String(privateKeyParam.P.ToByteArrayUnsigned()),
                    Convert.ToBase64String(privateKeyParam.Q.ToByteArrayUnsigned()),
                    Convert.ToBase64String(privateKeyParam.DP.ToByteArrayUnsigned()),
                    Convert.ToBase64String(privateKeyParam.DQ.ToByteArrayUnsigned()),
                    Convert.ToBase64String(privateKeyParam.QInv.ToByteArrayUnsigned()),
                    Convert.ToBase64String(privateKeyParam.Exponent.ToByteArrayUnsigned()));
            }
    
            /// <summary>
            /// RSA私钥格式转换,.net->java
            /// </summary>
            /// <param name="privateKey">.net生成的私钥</param>
            /// <returns></returns>
            public static string RSAPrivateKeyDotNet2Java(string privateKey)
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(privateKey);
                BigInteger m = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Modulus")[0].InnerText));
                BigInteger exp = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Exponent")[0].InnerText));
                BigInteger d = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("D")[0].InnerText));
                BigInteger p = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("P")[0].InnerText));
                BigInteger q = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Q")[0].InnerText));
                BigInteger dp = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("DP")[0].InnerText));
                BigInteger dq = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("DQ")[0].InnerText));
                BigInteger qinv = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("InverseQ")[0].InnerText));
    
                RsaPrivateCrtKeyParameters privateKeyParam = new RsaPrivateCrtKeyParameters(m, exp, d, p, q, dp, dq, qinv);
                PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKeyParam);
                byte[] serializedPrivateBytes = privateKeyInfo.ToAsn1Object().GetEncoded();
                return Convert.ToBase64String(serializedPrivateBytes);
            }
    
            /// <summary>
            /// RSA公钥格式转换,java->.net
            /// </summary>
            /// <param name="publicKey">java生成的公钥</param>
            /// <returns></returns>
            public static string RSAPublicKeyJava2DotNet(string publicKey)
            {
                RsaKeyParameters publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey));
                return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent></RSAKeyValue>",
                    Convert.ToBase64String(publicKeyParam.Modulus.ToByteArrayUnsigned()),
                    Convert.ToBase64String(publicKeyParam.Exponent.ToByteArrayUnsigned()));
            }
    
            /// <summary>
            /// RSA公钥格式转换,.net->java
            /// </summary>
            /// <param name="publicKey">.net生成的公钥</param>
            /// <returns></returns>
            public static string RSAPublicKeyDotNet2Java(string publicKey)
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(publicKey);
                BigInteger m = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Modulus")[0].InnerText));
                BigInteger p = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Exponent")[0].InnerText));
                RsaKeyParameters pub = new RsaKeyParameters(false, m, p);
    
                SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pub);
                byte[] serializedPublicBytes = publicKeyInfo.ToAsn1Object().GetDerEncoded();
                return Convert.ToBase64String(serializedPublicBytes);
            }
    
        }
    }
  • 相关阅读:
    Mybatis中selectKey源码分析
    Mybatis 基于注解Mapper源码分析
    MyBatis的二级缓存以及装饰器模式运用
    ConcurrentSkipListMap源码分析
    CopyOnWriteArrayList源码分析
    InnoDB 单列索引与多列索引
    Java 线程池源码分析
    JDK8 ReentrantReadWriteLock源码分析
    关于lombok的坑
    on duplicate key update 的用法说明(解决批量操作数据,有就更新,没有就新增)mybatis批量操作数据更新和添加
  • 原文地址:https://www.cnblogs.com/Jeely/p/11718708.html
Copyright © 2011-2022 走看看