【急】rsa加密:C#怎么调用java 产生的 rsa 公钥,密钥进行加密。

.Net技术 码拜 9年前 (2016-02-01) 1199次浏览
这是公钥:–BEGIN RSA PUBLIC KEY–
MEgCQQCh0U1FB617S9gaaI8hsD1o2v88F4vNlK2ggJA6vaRAAFAznxU8atAOSNyscOmtiXqDEymq5wlS0rg30zczONk7AgMBAAE=
–END RSA PUBLIC KEY–
这是密钥:–BEGIN RSA PRIVATE KEY–
MIIBOgIBAAJBAK/12y7RfdtfNx5enNa48sq2ZKE0Gz1huwAOwmPL4+A3Z4+w/KIz
iOdBKOEc8vxURKrNX8TeIImzOpzabyStN/cCAwEAAQJADAzCHcqq28MBe0rvYqT3
u3oSGhf/ZYyzyaU5Gqxzxn7ntBiBRwhjs4bHDHQsVSU71ItWp9DTA7jqh9DaZd0h
gQIhAOAJdRSGs4gGZFuQbJVe8DFxQDGy1VxGrndUBFsbbD/pAiEAyRB9T7hhutY5
vvwVoCnZwIcstlUk5cVN3/3OMcVJrN8CIFYLC85QVY8+dTi9ZRaRGcrDZhYwSsVf
2A24VNvw0khRAiBaidFTv5xKbMLuDEeUqqPCPVxV8ns9+B28GmbqJIOrkwIhAJlG
bEcJpFEZGFTb2fPoqVI8/PH+thjPTtf0bBgYD++C
–END RSA PRIVATE KEY–
貌似还不是配对的。但是要求是用这俩串密钥进行加密。求给方法。
本人知道C# rsa 加密是XML格式。这种格式怎么用啊。求指导…
解决方案:70分
    public class RSACryption2
    {
        const int MaxLeng =1024;
        /// <summary>
        /// 生成公私钥
        /// </summary>
        /// <param name="PrivateKey"></param>
        /// <param name="PublicKey"></param>
        public void CreateRSAKey(out string PrivateKey, out string PublicKey)
        {
               CspParameters RSAParams = new CspParameters();  
               RSAParams.Flags = CspProviderFlags.UseMachineKeyStore;  
           //System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024, RSAParams);  
               using (RSACryptoServiceProvider provider = new RSACryptoServiceProvider(MaxLeng, RSAParams))
               {
                   PrivateKey = provider.ToXmlString(true);
                   PublicKey = provider.ToXmlString(false);
                   provider.Clear();
               }
           
        }
        /// <summary>
        /// 对原始数据进行MD5加密
        /// </summary>
        /// <param name="m_strSource">待加密数据</param>
        /// <returns>返回机密后的数据</returns>
        public string GetHash(string m_strSource)
        {
            HashAlgorithm algorithm = HashAlgorithm.Create("MD5");
            byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(m_strSource);
            byte[] inArray = algorithm.ComputeHash(bytes);
            return Convert.ToBase64String(inArray);
        }
        /// <summary>
        /// RSA加密
        /// </summary>
        /// <param name="xmlPublicKey">公钥</param>
        /// <param name="m_strEncryptString">MD5加密后的数据</param>
        /// <returns>RSA公钥加密后的数据</returns>
        public string RSAEncrypt(string xmlPublicKey, string m_strEncryptString)
        {
            string str2;
            try
            {
                CspParameters RSAParams = new CspParameters();
                RSAParams.Flags = CspProviderFlags.UseMachineKeyStore;
                using (RSACryptoServiceProvider provider = new RSACryptoServiceProvider(MaxLeng, RSAParams))
                {
                    provider.FromXmlString(xmlPublicKey);
                    byte[] bytes = new UnicodeEncoding().GetBytes(m_strEncryptString);
                    str2 = Convert.ToBase64String(provider.Encrypt(bytes, false));
                    provider.Clear();
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return str2;
        }
        /// <summary>
        /// RSA解密
        /// </summary>
        /// <param name="xmlPrivateKey">私钥</param>
        /// <param name="m_strDecryptString">待解密的数据</param>
        /// <returns>解密后的结果</returns>
        public string RSADecrypt(string xmlPrivateKey, string m_strDecryptString)
        {
            string str2;
            try
            {
                CspParameters RSAParams = new CspParameters();
                RSAParams.Flags = CspProviderFlags.UseMachineKeyStore;
                using (RSACryptoServiceProvider provider = new RSACryptoServiceProvider(MaxLeng, RSAParams))
                {
                    provider.FromXmlString(xmlPrivateKey);
                    byte[] rgb = Convert.FromBase64String(m_strDecryptString);
                    byte[] buffer2 = provider.Decrypt(rgb, false);
                    str2 = new UnicodeEncoding().GetString(buffer2);
                    provider.Clear();
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return str2;
        }
        /// <summary>
        /// 对MD5加密后的密文进行签名
        /// </summary>
        /// <param name="p_strKeyPrivate">私钥</param>
        /// <param name="m_strHashbyteSignature">MD5加密后的密文</param>
        /// <returns></returns>
        public string SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature)
        {
            byte[] rgbHash = Convert.FromBase64String(m_strHashbyteSignature);
            CspParameters RSAParams = new CspParameters();
            RSAParams.Flags = CspProviderFlags.UseMachineKeyStore;
            using (RSACryptoServiceProvider key = new RSACryptoServiceProvider(MaxLeng, RSAParams))
            {
                key.FromXmlString(p_strKeyPrivate);
                RSAPKCS1SignatureFormatter formatter = new RSAPKCS1SignatureFormatter(key);
                formatter.SetHashAlgorithm("MD5");
                byte[] inArray = formatter.CreateSignature(rgbHash);
                key.Clear();
                return Convert.ToBase64String(inArray);
            }
        }
        /// <summary>
        /// 签名验证
        /// </summary>
        /// <param name="p_strKeyPublic">公钥</param>
        /// <param name="p_strHashbyteDeformatter">待验证的用户名</param>
        /// <param name="p_strDeformatterData">注册码</param>
        /// <returns></returns>
        public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, string p_strDeformatterData)
        {
            try
            {
                byte[] rgbHash = Convert.FromBase64String(p_strHashbyteDeformatter);
                RSACryptoServiceProvider key = new RSACryptoServiceProvider();
                key.FromXmlString(p_strKeyPublic);
                RSAPKCS1SignatureDeformatter deformatter = new RSAPKCS1SignatureDeformatter(key);
                deformatter.SetHashAlgorithm("MD5");
                byte[] rgbSignature = Convert.FromBase64String(p_strDeformatterData);
                if (deformatter.VerifySignature(rgbHash, rgbSignature))
                {
                    return true;
                }
                return false;
            }
            catch
            {
                return false;
            }
        }
    }

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明【急】rsa加密:C#怎么调用java 产生的 rsa 公钥,密钥进行加密。
喜欢 (0)
[1034331897@qq.com]
分享 (0)