Пример #1
0
 //获取随机DESKey
 private void btnGetDESKey_Click(object sender, EventArgs e)
 {
     if (txtMd5Key.Text.Length <= 0)
     {
         MessageBox.Show("请输出DesKey");
     }
     txtDesKey.Text = OtherCrypt.GetMd5(txtMd5Key.Text).Substring(0, 8);
 }
Пример #2
0
        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="Key">密钥</param>
        /// <param name="plainStr">明文字符串</param>
        /// <param name="ivStr">加密向量</param>
        /// <param name="cMode">加密模式</param>
        /// <param name="pMode">填充模式</param>
        /// <returns>密文</returns>
        public static String Encrypt(String Key, String plainStr, String ivStr = "", CipherMode cMode = CipherMode.ECB, PaddingMode pMode = PaddingMode.PKCS7)
        {
            var aes = new RijndaelManaged();

            //秘钥的大小,以位为单位
            aes.KeySize = 256;
            //支持的块大小
            aes.BlockSize = 128;
            //填充模式
            aes.Padding = pMode;
            aes.Mode    = cMode;
            //aes.Key = Convert.FromBase64String(Key);
            aes.Key = Encoding.Default.GetBytes(OtherCrypt.GetMd5(Key));//采用MD5加密一次,密钥可变长度
            aes.IV  = IV;
            if (ivStr.Length > 0)
            {
                if (ivStr.StartsWith("0x"))
                {
                    aes.IV = HexToByte(ivStr.Substring(2));
                }
                else
                {
                    //aes.IV = Encoding.Default.GetBytes(OtherCrypt.GetMd5(ivStr).Substring(0, 16));
                    aes.IV = Encoding.Default.GetBytes(ivStr);
                }
            }
            var encrypt = aes.CreateEncryptor(aes.Key, aes.IV);

            byte[] xBuff = new byte[0];

            using (var ms = new MemoryStream())
            {
                using (var cs = new CryptoStream(ms, encrypt, CryptoStreamMode.Write))
                {
                    byte[] xXml = Encoding.UTF8.GetBytes(plainStr);
                    cs.Write(xXml, 0, xXml.Length);
                }
                xBuff = ms.ToArray();
            }
            return(Convert.ToBase64String(xBuff));
        }
Пример #3
0
 //MD5加密算法
 private void btnEncrypt5_Click(object sender, EventArgs e)
 {
     txtCiphertext5.Text = OtherCrypt.GetMd5(txtPlaintext5.Text);
 }