public static byte[] EncryptAes(this byte[] @this) { var key = new byte[16]; var iv = new byte[16]; s_random.GetNonZeroBytes(key); s_random.GetNonZeroBytes(iv); var data = @this.FastClone(); using (var aes = new RijndaelManaged()) { aes.Mode = CipherMode.CFB; aes.Padding = PaddingMode.None; aes.KeySize = 192; aes.BlockSize = 128; aes.GenerateKeys(data.Length); var blockSize = aes.BlockSize / 8; var needChange = data.Length % blockSize != 0; var diff = blockSize - data.Length % blockSize; if (needChange) { Array.Resize(ref data, data.Length + diff); } data = aes.Encrypt(data); if (needChange) { Array.Resize(ref data, data.Length - diff); } return(data); } }
/// <summary> /// 加密 /// </summary> /// <param name="str">要加密的 string 字符串</param> /// <param name="key"></param> /// <returns></returns> public static string Encrypt(this RijndaelManaged manager, string str, Encoding encoding = null) { byte[] stringArray = (encoding ?? Encoding.UTF8).GetBytes(str); byte[] result = manager.Encrypt(stringArray); return(Convert.ToBase64String(result)); }
public void SaveInferredMeasurements(List <Measurement> measurements) { foreach (var measurement in measurements) { measurement.Count = _cryptographyTool.Encrypt(measurement.Count, CryptoConstants.passPhrase); } inferredDataRepo.Save(measurements); }
public void TestRijndaelAlgorithm() { var provider = new RijndaelManaged(); provider.Key = new Byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }; provider.IV = new Byte[] { 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 }; provider.Padding = PaddingMode.Zeros; provider.Mode = CipherMode.CBC; var encodedData = provider.Encrypt(_clearData); var decodedData = provider.Decrypt(encodedData); CollectionAssert.AreEqual(_clearData, decodedData); }
public static byte[] EncryptAES(byte[] data) { using (var aes = new RijndaelManaged()) { aes.Mode = CipherMode.CFB; aes.Padding = PaddingMode.Zeros; aes.KeySize = 192; aes.BlockSize = 128; Array.Resize(ref data, data.Length + 16 - data.Length % 16); aes.GenerateKeys(data.Length); return(aes.Encrypt(data)); } }