/// <summary> /// 加密或解密字节数组,使用Rfc2898DeriveBytes与TripleDESCryptoServiceProvider的加密提供程序生成的密匙和初始化向量 /// </summary> /// <param name="pwd">需要加密或解密的密码字符串</param> /// <param name="bytes">用来加密的字节数组</param> /// <param name="encrypt">true:加密,false:解密</param> /// <returns></returns> private static byte[] CryptBytes(string pwd, byte[] bytes, bool encrypt) { //第三方加密服务商 var desProvider = new TripleDESCryptoServiceProvider(); //找到此提供程序的有效密钥大小 int keySizeBits = 0; for (int i = 1024; i >= 1; i--) { if (desProvider.ValidKeySize(i)) { keySizeBits = i; break; } } //获取此提供程序的块大小 int blockSizeBits = desProvider.BlockSize; //生成密钥和初始化向量 byte[] key = null; byte[] iv = null; byte[] salt = { 0x10, 0x20, 0x12, 0x23, 0x37, 0xA4, 0xC5, 0xA6, 0xF1, 0xF0, 0xEE, 0x21, 0x22, 0x45 }; MakeKeyAndIv(pwd, salt, keySizeBits, blockSizeBits, ref key, ref iv); //进行加密或解密 ICryptoTransform cryptoTransform = encrypt ? desProvider.CreateEncryptor(key, iv) : desProvider.CreateDecryptor(key, iv); //创建输出流 var outStream = new MemoryStream(); //附加一个加密流输出流 var cryptoStream = new CryptoStream(outStream, cryptoTransform, CryptoStreamMode.Write); //写字节到加密流中 cryptoStream.Write(bytes, 0, bytes.Length); try { cryptoStream.FlushFinalBlock(); } catch (CryptographicException) { // Ignore this one. The password is bad. } //保存结果 byte[] result = outStream.ToArray(); //关闭流 try { cryptoStream.Close(); } catch (CryptographicException) { // Ignore this one. The password is bad. } outStream.Close(); return(result); }
/// <summary> /// Encrypt or decrypt a byte array using the TripleDESCryptoServiceProvider crypto provider and Rfc2898DeriveBytes to build the key and initialization vector. /// </summary> /// <param name="password">The password String to use in encrypting or decrypting.</param> /// <param name="inBytes">The array of bytes to encrypt.</param> /// <param name="encrypt">True to encrypt, False to decrypt.</param> /// <returns></returns> /// <remarks></remarks> public static byte[] CryptBytes(String password, byte[] inBytes, bool encrypt) { // Make a triple DES service provider. var desProvider = new TripleDESCryptoServiceProvider(); // Find a valid key size for this provider. var keySize = 0; for (var i = 1024; i >= 1; i--) { if (desProvider.ValidKeySize(i)) { keySize = i; break; } } // Get the block size for this provider. var blockSize = desProvider.BlockSize; // Generate the key and initialization vector. byte[] key = null; byte[] iv = null; byte[] salt = { 0x10, 0x20, 0x12, 0x23, 0x37, 0xA4, 0xC5, 0xA6, 0xF1, 0xF0, 0xEE, 0x21, 0x22, 0x45 }; MakeKeyAndIv(password, salt, keySize, blockSize, ref key, ref iv); // Make the encryptor or decryptor. var cryptoTransform = encrypt ? desProvider.CreateEncryptor(key, iv) : desProvider.CreateDecryptor(key, iv); byte[] result; // Create the output stream. using (var streamOut = new MemoryStream()) { // Attach a crypto stream to the output stream. var streamCrypto = new CryptoStream(streamOut, cryptoTransform, CryptoStreamMode.Write); // Write the bytes into the CryptoStream. streamCrypto.Write(inBytes, 0, inBytes.Length); try { streamCrypto.FlushFinalBlock(); } catch (CryptographicException) { // Ignore this one. The password is bad. } // Save the result. result = streamOut.ToArray(); // Close the stream. try { streamCrypto.Close(); } catch (CryptographicException) { // Ignore this one. The password is bad. } streamOut.Close(); } return(result); }
public static string EncryptStringWith3DES(string data, string key, string iv) { UnicodeEncoding unicode = new UnicodeEncoding(); Byte[] Bytes = unicode.GetBytes(data); MemoryStream mem = new MemoryStream(100); TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider(); Byte[] KeyBytes = unicode.GetBytes(key); Byte[] tmpBytes = new Byte[16]; Array.Copy(KeyBytes, tmpBytes, KeyBytes.Length < 16 ? KeyBytes.Length : 16); KeyBytes = tmpBytes; if (tdes.ValidKeySize(KeyBytes.Length * 8)) { System.Diagnostics.Debug.WriteLine("Key size valid"); } if (TripleDESCryptoServiceProvider.IsWeakKey(KeyBytes)) { System.Diagnostics.Debug.WriteLine("Key weak"); } CryptoStream CrStream = new CryptoStream(mem, tdes.CreateEncryptor(KeyBytes, unicode.GetBytes(iv)), CryptoStreamMode.Write); for (int i = 0; i < Bytes.Length; i++) { CrStream.WriteByte(Bytes[i]); } CrStream.FlushFinalBlock(); string result = Convert.ToBase64String(mem.GetBuffer(), 0, (int)mem.Length); CrStream.Dispose(); return(result); }
public static bool ValidateKeySize(EncryptionAlgorithm algID, int Lenght) { switch (algID) { case EncryptionAlgorithm.DES: DES des = new DESCryptoServiceProvider(); return(des.ValidKeySize(Lenght)); case EncryptionAlgorithm.Rc2: RC2 rc = new RC2CryptoServiceProvider(); return(rc.ValidKeySize(Lenght)); case EncryptionAlgorithm.Rijndael: Rijndael rj = new RijndaelManaged(); return(rj.ValidKeySize(Lenght)); case EncryptionAlgorithm.TripleDes: TripleDES tDes = new TripleDESCryptoServiceProvider(); return(tDes.ValidKeySize(Lenght)); default: throw new CryptographicException("Algorithm " + algID + " Not Supported!"); } }