• C# DES (ECB模式) 加密解密 --单倍长


    加密:  调用时: Encrypt_DES16(“2AF349243535BCD3”, "1111111111111111");

     public static string Encrypt_DES16(string str_in_data, string str_DES_KEY) //数据为十六进制
            {
                try
                {
                    byte[] shuju = new byte[8];
                    byte[] keys = new byte[8];
                    for (int i = 0; i < 8; i++)
                    {
                        shuju[i] = Convert.ToByte(str_in_data.Substring(i * 2, 2), 16);
                        keys[i] = Convert.ToByte(str_DES_KEY.Substring(i * 2, 2), 16);
                    }
                    DES desEncrypt = new DESCryptoServiceProvider();
                    desEncrypt.Mode = CipherMode.ECB;
                    //desEncrypt.Key = ASCIIEncoding.ASCII.GetBytes(str_DES_KEY);
                    desEncrypt.Key = keys;
                    byte[] Buffer;
                    Buffer = shuju;//ASCIIEncoding.ASCII.GetBytes(str_in_data);
                    ICryptoTransform transForm = desEncrypt.CreateEncryptor();
                    byte[] R;
                    R = transForm.TransformFinalBlock(Buffer, 0, Buffer.Length);
                    string return_str = "";
                    foreach (byte b in R)
                    {
                        return_str += b.ToString("X2");
                    }
                    return_str = return_str.Substring(0, 16);
                    return return_str;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

    解密:调用时: Encrypt_DES16(“C47EC89B0A247A47”, "1111111111111111");

     //DES解密
            public static string Decrypt_DES16(string str_in_data, string str_DES_KEY)//数据和密钥为十六进制
            {
                byte[] shuju = new byte[8];
                byte[] keys = new byte[8];
                for (int i = 0; i < 8; i++)
                {
                    shuju[i] = Convert.ToByte(str_in_data.Substring(i * 2, 2), 16);
                    keys[i] = Convert.ToByte(str_DES_KEY.Substring(i * 2, 2), 16);
                }
                DES desDecrypt = new DESCryptoServiceProvider();
                desDecrypt.Mode = CipherMode.ECB;
                desDecrypt.Key = keys;
                desDecrypt.Padding = System.Security.Cryptography.PaddingMode.None;
                byte[] Buffer = shuju;
                ICryptoTransform transForm = desDecrypt.CreateDecryptor();
                byte[] R;
                R = transForm.TransformFinalBlock(Buffer, 0, Buffer.Length);
                string return_str = "";
                foreach (byte b in R)
                {
                    return_str += b.ToString("X2");
                }
                return return_str;
            }
    


  • 相关阅读:
    c++ list_iterator demo
    模板元编程例子
    !a && !b 和 !(a || b) 的故事
    简明解释算法中的大O符号
    重构oceanbase的一个函数
    正则表达式识别汉字
    编写易于理解代码的六种方式
    Linux下的tar压缩解压缩命令详解
    2013 年 —— Facebook 在开源方面的工作介绍
    Kent Beck揭秘Facebook开发部署流程
  • 原文地址:https://www.cnblogs.com/yxwkf/p/5424481.html
Copyright © 2020-2023  润新知