zoukankan      html  css  js  c++  java
  • 二维码的生成及常用加密解密

    二维码的生成使用工具: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:2018codecodeinDebug.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:2018codecodein";
                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:2018codecodeinDebugRSA    加密_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:2018codecodeinDebugRSA加密_PublicKey_20180420145359.xml";
                string privateKey = @"G:2018codecodeinDebugRSA解密_PrivateKey_20180420145359.xml";    
                string ss=EncryptByRSA(str, File.ReadAllText(publicKey));
                Console.WriteLine(DecryptByRSA(ss, File.ReadAllText(privateKey)));
                Console.ReadKey();
  • 相关阅读:
    python 打印对象的所有属性值
    selenium+python测试
    java连接3种数据库 JdbcLinkDB --201801
    又来折腾--正则表达式
    Excel 将A表的基础数据拼接到B表中来-三种方法: ctrl+回车, VLOOKUP()函数,宏
    Excel如何快速统计一列中相同数值出现的个数--数据透视表
    Jmeter、Postman 、 loadrunner SoapUI 接口测试工具
    delphi 获取时间戳 如何得到 和 js 中 new Date().getTime();的 相同?
    IIS部署项目
    C#使用log4net记录日志
  • 原文地址:https://www.cnblogs.com/MrZheng/p/8891463.html
Copyright © 2011-2022 走看看