二维码的生成及常用加密解密

二维码的生成使用工具:ZXing.Net

创建项目,并把ZXing.Net安装到项目中

创建二维码:

        /// <summary>
        /// 创建二维码
        /// </summary>
        /// <param name="msg">要生成的信息</param>
        /// <returns></returns>
        public static Bitmap Create(string msg)
        {
            MultiFormatWriter writer = new MultiFormatWriter();
            Dictionary<EncodeHintType, object> hint = new Dictionary<EncodeHintType, object>();
            hint.Add(EncodeHintType.CHARACTER_SET, "utf-8");
            hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);  //设置纠错等级, 高
            BitMatrix bm = writer.encode(msg, BarcodeFormat.QR_CODE, 300, 300, hint);
            BarcodeWriter barcodeWriter = new BarcodeWriter();
            return barcodeWriter.Write(bm);
        }

        string str = "123";
        Bitmap code = Create(str); 
        string path = Environment.CurrentDirectory;
        code.Save(path + ".png", ImageFormat.Png);//当前bin目录下

二维码解码:

        public static string decode(string imgPath)
        {
            BarcodeReader reader = new BarcodeReader();
            Bitmap bp = new Bitmap(imgPath);
            Result result= reader.Decode(bp);
            return result.ToString();
        }

        string path = @"G:\2018\code\code\bin\Debug.png";
        Console.WriteLine(decode(path));
        Console.ReadKey();

常用加密解密

MD5加密(不可逆加密):

/// <summary>
        /// 将字符串进行MD5加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetMD5Str(string str)
        {
            StringBuilder sb = new StringBuilder();

            System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();

            byte[] s;
            s = new byte[0];
            if (str != null)
            {

                s = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
            }
            for (int i = 0; i < s.Length; i++)
            {
                sb.Append(s[i].ToString("X2"));
            }

            //md5Str就是最后得到加密后的字符串
            string md5Str = sb.ToString();

            return md5Str;
        }

 

DES加密:

     /// <summary> 加密字符串
        /// </summary> 
        /// <param name="strText">需被加密的字符串</param> 
        /// <param name="strEncrKey">密钥</param> 
        /// <returns></returns> 
        public static string DesEncrypt(string strText, string strEncrKey)
        {
            try
            {
                byte[] byKey = null;
                byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
                //加密秘钥要大于8位
                byKey = Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                return Convert.ToBase64String(ms.ToArray());
            }
            catch
            {
                return "";
            }
        }

       string str = "123465";
            string key = "goodgoodstudy";
            Console.WriteLine(DesEncrypt(str, key));
            Console.ReadKey();

DES解密:

        /// <summary> 解密字符串
        /// </summary> 
        /// <param name="strText">需被解密的字符串</param> 
        /// <param name="sDecrKey">密钥</param> 
        /// <returns></returns> 
        public static string DesDecrypt(string strText, string sDecrKey)
        {
            try
            {
                byte[] byKey = null;
                byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
                byte[] inputByteArray = new Byte[strText.Length];

                byKey = Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                inputByteArray = Convert.FromBase64String(strText);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                Encoding encoding = new UTF8Encoding();
                return encoding.GetString(ms.ToArray());
            }
            catch
            {
                return null;
            }
        }

            string str = "123465";
            string key = "goodgoodstudy";
            string ss = DesEncrypt(str, key);
            Console.WriteLine(DesDecrypt(ss, key));
            Console.ReadKey();

RSA 秘钥生成:

     /// <summary>生成RSA加密 解密的 密钥
        /// </summary>
        /// <param name="path">要生成的密钥文件的路径(文件夹)</param>
        public static void getRSAKey(string path)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            string datetimestr = System.DateTime.Now.ToString("yyyyMMddHHmmss");
            using (StreamWriter writer = new StreamWriter("RSA解密_PrivateKey_" + datetimestr + ".xml"))
            {
                writer.WriteLine(rsa.ToXmlString(true));
            }
            using (StreamWriter writer = new StreamWriter("RSA加密_PublicKey_" + datetimestr + ".xml"))
            {
                writer.WriteLine(rsa.ToXmlString(false));
            }
        }


            string path = @"G:\2018\code\code\bin";
            getRSAKey(path);        

会在debug文件夹生成公钥和私钥:

RSA加密:

        /// <summary>RSA加密
        /// </summary>
        /// <param name="plaintext">明文</param>
        /// <param name="publicKey">公钥</param>
        /// <returns>密文字符串</returns>
        public static string EncryptByRSA(string plaintext, string publicKey)
        {
            try
            {
                UnicodeEncoding ByteConverter = new UnicodeEncoding();
                byte[] dataToEncrypt = ByteConverter.GetBytes(plaintext);
                using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
                {
                    RSA.FromXmlString(publicKey);
                    byte[] encryptedData = RSA.Encrypt(dataToEncrypt, false);
                    return Convert.ToBase64String(encryptedData);
                }
            }
            catch (Exception)
            {
                return null;
            }

        }

            string str = "123465";
            string publicKey = @"G:\2018\code\code\bin\Debug\RSA    加密_PublicKey_20180420145359.xml";    
            Console.WriteLine(EncryptByRSA(str, File.ReadAllText(publicKey)));
            Console.ReadKey();

RSA解密:

/// <summary> RSA解密
        /// </summary>
        /// <param name="ciphertext">密文</param>
        /// <param name="privateKey">私钥</param>
        /// <returns>明文字符串</returns>
        public static string DecryptByRSA(string ciphertext, string privateKey)
        {
            try
            {
                UnicodeEncoding byteConverter = new UnicodeEncoding();
                using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
                {
                    RSA.FromXmlString(privateKey);
                    byte[] encryptedData = Convert.FromBase64String(ciphertext);
                    byte[] decryptedData = RSA.Decrypt(encryptedData, false);
                    return byteConverter.GetString(decryptedData);
                }
            }
            catch (Exception)
            {
                return null;
            }

        }

            string str = "123465";
            string publicKey = @"G:\2018\code\code\bin\Debug\RSA加密_PublicKey_20180420145359.xml";
            string privateKey = @"G:\2018\code\code\bin\Debug\RSA解密_PrivateKey_20180420145359.xml";    
            string ss=EncryptByRSA(str, File.ReadAllText(publicKey));
            Console.WriteLine(DecryptByRSA(ss, File.ReadAllText(privateKey)));
            Console.ReadKey();

 

转载于:https://www.cnblogs.com/MrZheng/p/8891463.html

  • 0
    点赞
  • 0
    评论
  • 2
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2021 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值