Example #1
0
        public void Encrypt256()
        {
            var plaintext     = Encoding.UTF8.GetBytes(Plaintext);
            var etmCiphertext = RijndaelEtM.Encrypt(plaintext, Password, Iv, KeySize.Aes256);

            Assert.Equal(Convert.ToBase64String(etmCiphertext), EtmProof256);
        }
Example #2
0
        public void CalculateMac256()
        {
            var proof256 = Convert.FromBase64String(EtmProof256);
            var mac      = RijndaelEtM.CalculateMac(proof256, Iv);

            Assert.True(mac.SequenceEqual(MacProof256));
        }
Example #3
0
        public void RandomIv192()
        {
            var etmCiphertext1 = RijndaelEtM.Encrypt(Plaintext, Password, KeySize.Aes192);
            var etmCiphertext2 = RijndaelEtM.Encrypt(Plaintext, Password, KeySize.Aes192);
            var plaintext      = RijndaelEtM.Decrypt(etmCiphertext1, Password, KeySize.Aes192);

            Assert.Equal(plaintext, Plaintext);
            Assert.NotEqual(etmCiphertext1, etmCiphertext2);
        }
Example #4
0
        public void BinaryBlob()
        {
            var plainblob = UTF8Encoding.UTF8.GetBytes(Plaintext);

            var cipherblob  = RijndaelEtM.EncryptBinary(plainblob, Password, KeySize.Aes128);
            var plainresult = RijndaelEtM.DecryptBinary(cipherblob, Password, KeySize.Aes128);

            Assert.Equal(plainblob, plainresult);
        }
        public static string Decrypt(string cipherText, string passPhrase)
        {
            string decrypted = RijndaelEtM.Decrypt(cipherText, passPhrase, KeySize.Aes256);

            return(decrypted);

            /**
             * // Get the complete stream of bytes that represent:
             * // [32 bytes of Salt] + [32 bytes of IV] + [n bytes of CipherText]
             * var cipherTextBytesWithSaltAndIv = Convert.FromBase64String(cipherText);
             * // Get the saltbytes by extracting the first 32 bytes from the supplied cipherText bytes.
             * var saltStringBytes = cipherTextBytesWithSaltAndIv.Take(Keysize / 8).ToArray();
             * // Get the IV bytes by extracting the next 32 bytes from the supplied cipherText bytes.
             * var ivStringBytes = cipherTextBytesWithSaltAndIv.Skip(Keysize / 8).Take(Keysize / 8).ToArray();
             * // Get the actual cipher text bytes by removing the first 64 bytes from the cipherText string.
             * var cipherTextBytes = cipherTextBytesWithSaltAndIv.Skip((Keysize / 8) * 2).Take(cipherTextBytesWithSaltAndIv.Length - ((Keysize / 8) * 2)).ToArray();
             *
             * using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations))
             * {
             *  var keyBytes = password.GetBytes(Keysize / 8);
             *  using (var symmetricKey = new RijndaelManaged())
             *  {
             *      symmetricKey.BlockSize = 256;
             *      symmetricKey.Mode = CipherMode.CBC;
             *      symmetricKey.Padding = PaddingMode.PKCS7;
             *      using (var decryptor = symmetricKey.CreateDecryptor(keyBytes, ivStringBytes))
             *      {
             *          using (var memoryStream = new MemoryStream(cipherTextBytes))
             *          {
             *              using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
             *              {
             *                  var plainTextBytes = new byte[cipherTextBytes.Length];
             *                  var decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
             *                  memoryStream.Close();
             *                  cryptoStream.Close();
             *                  return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
             *              }
             *          }
             *      }
             *  }
             * }**/
        }
        public static string Encrypt(string plainText, string passPhrase)
        {
            string encrypted = RijndaelEtM.Encrypt(plainText, passPhrase, KeySize.Aes256);

            return(encrypted);

            /**
             * var saltStringBytes = Generate256BitsOfRandomEntropy();
             * var ivStringBytes = Generate256BitsOfRandomEntropy();
             * var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
             * using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations))
             * {
             *  var keyBytes = password.GetBytes(Keysize / 8);
             *  using (var symmetricKey = new  RijndaelManaged())
             *  {
             *      symmetricKey.BlockSize = 256;
             *      symmetricKey.Mode = CipherMode.CBC;
             *      symmetricKey.Padding = PaddingMode.PKCS7;
             *      using (var encrypter = symmetricKey.CreateEncryptor(keyBytes, ivStringBytes))
             *      {
             *          using (var memoryStream = new MemoryStream())
             *          {
             *              using (var cryptoStream = new CryptoStream(memoryStream, encrypter, CryptoStreamMode.Write))
             *              {
             *                  cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
             *                  cryptoStream.FlushFinalBlock();
             *                  // Create the final bytes as a concatenation of the random salt bytes, the random iv bytes and the cipher bytes.
             *                  var cipherTextBytes = saltStringBytes;
             *                  cipherTextBytes = cipherTextBytes.Concat(ivStringBytes).ToArray();
             *                  cipherTextBytes = cipherTextBytes.Concat(memoryStream.ToArray()).ToArray();
             *                  memoryStream.Close();
             *                  cryptoStream.Close();
             *                  return Convert.ToBase64String(cipherTextBytes);
             *              }
             *          }
             *      }
             *  }
             * }**/
        }
Example #7
0
        public void Decrypt256()
        {
            var plaintext = RijndaelEtM.Decrypt(EtmProof256, Password, KeySize.Aes256);

            Assert.Equal(plaintext, Plaintext);
        }
Example #8
0
        public void Decrypt192()
        {
            var plaintext = RijndaelEtM.Decrypt(EtmProof192, Password, KeySize.Aes192);

            Assert.Equal(plaintext, Plaintext);
        }
Example #9
0
 public static string Decrypt(string cipherText, string password)
 {
     return(RijndaelEtM.Decrypt(cipherText, password, KeySize.Aes256));
 }
Example #10
0
 public static string Encrypt(string plainText, string password)
 {
     return(RijndaelEtM.Encrypt(plainText, password, KeySize.Aes256));
 }
Example #11
0
 /// <summary>
 /// Encrypt a binary blob
 /// </summary>
 /// <param name="msg">The binary "plaintext"</param>
 /// <returns>The binary "ciphertext" with IV and MAC</returns>
 public byte[] EncryptMessage(byte[] msg)
 {
     return(RijndaelEtM.EncryptBinary(msg, EncryptionKey, KeySize.Aes256));
 }
Example #12
0
        public static string Decrypt(string cipherText, string passPhrase)
        {
            string decrypted = RijndaelEtM.Decrypt(cipherText, passPhrase, KeySize.Aes256);

            return(decrypted);
        }
Example #13
0
        public static string Encrypt(string plainText, string passPhrase)
        {
            string encrypted = RijndaelEtM.Encrypt(plainText, passPhrase, KeySize.Aes256);

            return(encrypted);
        }