用Encrypt、Decrypt对密码进行编码和解码操作。

    在程序中调用这个类Encryption:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace DBTools
{
    public class Encryption
    {
        /// <summary> 
        /// 加密数据 
        /// </summary> 
        /// <param name="Text"></param> 
        /// <param name="sKey"></param> 
        /// <returns></returns> 
        public static string Encrypt(string Text, string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray;
            inputByteArray = Encoding.Default.GetBytes(Text);
            des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                ret.AppendFormat("{0:X2}", b);
            }
            return ret.ToString();
        }
        /// <summary> 
        /// 解密数据 
        /// </summary> 
        /// <param name="Text"></param> 
        /// <param name="sKey"></param> 
        /// <returns></returns> 
        public static string Decrypt(string Text, string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            int len;
            len = Text.Length / 2;
            byte[] inputByteArray = new byte[len];
            int x, i;
            for (x = 0; x < len; x++)
            {
                i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
                inputByteArray[x] = (byte)i;
            }
            des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return Encoding.Default.GetString(ms.ToArray());
        } 
        // MD5 32 位
        public static String Encrypt32(String convertString)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(convertString);
            bytes = md5.ComputeHash(bytes);
            md5.Clear();
            string ret = "";
            for (int i = 0; i < bytes.Length; i++)
            {
                ret += Convert.ToString(bytes[i], 16).PadLeft(2, '0');
            }
            return ret.PadLeft(32, '0').ToLower();
        }
        // MD5 16 位
        public static string Encrypt16(string convertString)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(convertString)), 4, 8);
            t2 = t2.Replace("-", "");
            return t2.ToLower();
        }
    }
}
    cs:

编码时:string Pwd = DBTools.Encryption.Encrypt(LoginPwd, "Master");    
            //LoginPwd--要加密的数据。也可以将二进制值传递给此函数。此参数区分大小写,即使是在不区分大小写的数据库中也是如此。
            //Master--用来对 LoginPwd 进行加密的加密密钥。解密时必须使用同一密钥才能获得原始值。此参数区分大小写,即使是在不区分大小写的数据库中也是如此。
与大多数口令一样,最好选择无法被轻易猜到的密钥值。建议选择满足以下条件的密钥值:长度至少为 16 个字符,混合使用大小写并包含数字、字母和特殊字符。每次要对数据进行解密时,都需要使用此密钥。

解码时:string Pwd = DBTools.Encryption.Decrypt(LoginPwd, "Master"); //跟编码讲解一致



以下是利用MATLAB实现DES加解密,并采用OFB分组密码模式的示例代码: ```matlab % DES加解密并采用OFB分组密码模式 % 输入明文和密钥 plaintext = input('请输入明文:', 's'); key = input('请输入密钥:', 's'); % 将明文和密钥转换为二进制序列 plaintext_bin = reshape(dec2bin(plaintext, 8).' - '0', 1, []); key_bin = reshape(dec2bin(key, 8).' - '0', 1, []); % DES加密 ciphertext_bin = des(plaintext_bin, key_bin, 'encrypt'); % OFB分组密码模式 block_size = 64; % 分组长度为64位 iv = randi([0 1], 1, block_size); % 随机生成初始向量 num_blocks = ceil(length(ciphertext_bin) / block_size); % 计算块数 output = ''; for i = 1:num_blocks % 加密初始向量 iv = des(iv, key_bin, 'encrypt'); % 取出当前块的密文 block = ciphertext_bin((i-1)*block_size+1:i*block_size); % 与加密后的初始向量异或得到输出 output_block = xor(block, iv); % 将输出拼接起来 output = [output, output_block]; end % 输出密文和解密结果 ciphertext = char(bin2dec(reshape(num2str(output), 8, []).')); fprintf('加密后的密文:%s\n', ciphertext); plaintext_decrypted = des(output, key_bin, 'decrypt'); plaintext_decrypted = char(bin2dec(reshape(num2str(plaintext_decrypted), 8, []).')); fprintf('解密后的明文:%s\n', plaintext_decrypted); ``` 该代码会先要求输入明文和密钥,然后将它们转换为二进制序列,并使用`des`函数进行加密。接下来,采用OFB分组密码模式,将随机生成的初始向量和加密后的初始向量异或得到输出,并输出密文和解密结果。 注意,由于MATLAB中的DES实现需要使用Cryptographic Toolbox,因此需要具备该工具箱才能运行上述代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值