C#的RSA2加解密算法

写注册机的时候研究了一下加密解密,经过修改和测试后弄出来了算法,然后把它用在了DKProCloudMusic上。

安装NuGut包

需要安装 BouncyCastle 包

20200427082721

私钥加密

using System;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Encodings;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Security;

namespace cn.liudank.robot.code.DoClasss
{
    /// 类 名 称 :RSAEncrypt
    /// 类 描 述 :私钥加密
    /// 作    者 :LiuDanK
    /// 创建时间 :2019-11-28 17:10:31
    /// 更新时间 :2019-11-28 17:10:31
    /// 版 本 号 :v1.0
    public class RSAEncrypt
    {
        private static AsymmetricKeyParameter GetPrivateKeyParameter(string s)
        {
            s = s.Replace("\r", "").Replace("\n", "").Replace(" ", "");
            byte[] privateInfoByte = Convert.FromBase64String(s);
            AsymmetricKeyParameter priKey = PrivateKeyFactory.CreateKey(privateInfoByte);
            return priKey;
        }

        private static string EncryptByPrivateKey(string s, string key)
        {
            //非对称加密算法,加解密用
            IAsymmetricBlockCipher engine = new Pkcs1Encoding(new RsaEngine());
            engine.Init(true, GetPrivateKeyParameter(key));
            byte[] byteData = System.Text.Encoding.UTF8.GetBytes(s);
            var ResultData = engine.ProcessBlock(byteData, 0, byteData.Length);
            return Convert.ToBase64String(ResultData);
        }

        /// <summary>
        /// 生成激活码
        /// </summary>
        /// <param name="machCode">机器码</param>
        /// <returns></returns>
        public static string CreateUseCode(string machCode)
        {
            try
            {
                if (string.IsNullOrEmpty(machCode))
                {
                    return "请输入正确的机器码!";
                }
                return EncryptByPrivateKey(machCode, RSAKey.CloudMusicPrivateKey); // RSAKey.CloudMusicPrivateKey 为私钥
            }
            catch
            {
                return "生成失败。";
            }
        }

        /// <summary>
        /// 将字符串进行base64转换
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static string ToBase64String(string str)
        {
            return Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(str));
        }
    }
}

公钥解密

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Encodings;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Security;

namespace cn.liudank.robot.code.DoClasss
{
    /// 类 名 称 :RSADecrypt
    /// 类 描 述 :公钥解码
    /// 作    者 :LiuDanK
    /// 创建时间 :2019-11-28 17:10:49
    /// 更新时间 :2019-11-28 17:10:49
    /// 版 本 号 :v1.0
    public class RSADecrypt
    {
        #region 公钥

        private const string PublicKey = "";

        #endregion

        private static AsymmetricKeyParameter GetPublicKeyParameter(string publicKdy)
        {
            publicKdy = publicKdy.Replace("\r", "").Replace("\n", "").Replace(" ", "");
            byte[] publicInfoByte = Convert.FromBase64String(publicKdy);
            Asn1Object pubKeyObj = Asn1Object.FromByteArray(publicInfoByte);//这里也可以从流中读取,从本地导入   
            AsymmetricKeyParameter pubKey = PublicKeyFactory.CreateKey(publicInfoByte);
            return pubKey;
        }

        private static string DecryptByPublicKey(string s, string publicKdy)
        {
            s = s.Replace("\r", "").Replace("\n", "").Replace(" ", "");
            //非对称加密算法,加解密用  
            IAsymmetricBlockCipher engine = new Pkcs1Encoding(new RsaEngine());
            //解密  
            try
            {
                engine.Init(false, GetPublicKeyParameter(publicKdy));
                byte[] byteData = Convert.FromBase64String(s);
                var ResultData = engine.ProcessBlock(byteData, 0, byteData.Length);
                return System.Text.Encoding.UTF8.GetString(ResultData);

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        /// <summary>
        /// 验证激活码
        /// </summary>
        /// <param name="machCode">机器码</param>
        /// <param name="useCode">激活码</param>
        /// <returns></returns>
        public static bool Verifty(string machCode,string useCode)
        {
            try
            {
                if (string.IsNullOrEmpty(machCode))
                {
                    return false;
                }
                string enCode = DecryptByPublicKey(useCode, PublicKey);
                if (string.IsNullOrEmpty(enCode))
                {
                    return false;
                }
                if (machCode.Equals(enCode))
                {
                    return true;
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 将字符串进行md5加密
        /// </summary>
        /// <param name="str">返回16位字符串</param>
        /// <returns></returns>
        public static string GetMD5Lenth16(string str) 
        {
            System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(str)), 4, 8);
            t2 = t2.Replace("-", "");
            md5.Dispose();
            return t2;
        }
    }
}


遇到你之后,我才知道,原来这世间是如此的美好。