public static byte[] EncryptByteBlock(byte[] dataToEncrypt, byte[] password) { if (dataToEncrypt == null) { throw new ArgumentNullException("dataToEncrypt"); } if (password == null) { throw new ArgumentNullException("password"); } var aes = new Aes(); using (var rngCsp = new RNGCryptoServiceProvider()) { var salt = new byte[32]; rngCsp.GetBytes(salt); var compressed = Compressor.Compress(dataToEncrypt); var encrpytedMessage = aes.Encrypt(compressed, password, salt, PBKDF2_ITERATIONS); var fullMessage = ByteHelpers.Combine(salt, encrpytedMessage); return fullMessage; } }
public async Task<string> Decrypt(string encryptedPremiumToken, string apiKey) { var aes = new Aes(); var sha1 = new SHA1Hash(); var keyHash = await sha1.GetHashAsync(apiKey).ConfigureAwait(false); var unencryptedPremiumToken = await aes.DecryptAsync(encryptedPremiumToken, keyHash).ConfigureAwait(false); return unencryptedPremiumToken; }
public void TestEncryptFile() { var encryptor = new Aes(); string file = FILE1, encrypted = FILE1 + ".aes_encrypted", decrypted = FILE1 + ".aes_decrypted", key = "password", salt = "this_is_salt"; // without salt encryptor.EncryptFile(file, encrypted, key); Assert.True(File.Exists(encrypted)); encryptor.DecryptFile(encrypted, decrypted, key); Assert.True(File.Exists(decrypted)); Assert.Equal(new FileInfo(file).ReadBytes().ToHexaDecimalString(), new FileInfo(decrypted).ReadBytes().ToHexaDecimalString()); // with salt encryptor.EncryptFile(file, encrypted, key, salt); Assert.True(File.Exists(encrypted)); encryptor.DecryptFile(encrypted, decrypted, key); Assert.True(File.Exists(decrypted)); Assert.Equal(new FileInfo(file).ReadBytes().ToHexaDecimalString(), new FileInfo(decrypted).ReadBytes().ToHexaDecimalString()); }
public void TestEncryptStringToBase64() { var encryptor = new Aes(); string raw = "Đây là Unicode string", key = "password", salt = "this_is_salt"; string encrypted; string decrypted; // without salt encrypted = encryptor.EncryptToBase64(raw, key); Assert.NotNull(encrypted); decrypted = encryptor.DecryptFromBase64ToString(encrypted, key); Assert.NotNull(decrypted); Assert.Equal(raw, decrypted); // with salt encrypted = encryptor.EncryptToBase64(raw, key, salt); Assert.NotNull(encrypted); decrypted = encryptor.DecryptFromBase64ToString(encrypted, key); Assert.NotNull(decrypted); Assert.Equal(raw, decrypted); }
public void TestEncryptString() { var encryptor = new Aes(); string raw = "Đây là Unicode string", key = "password", salt = "this_is_salt"; byte[] encrypted; byte[] decrypted; // without salt encrypted = encryptor.Encrypt(raw, key); Assert.NotNull(encrypted); decrypted = encryptor.Decrypt(encrypted, key); Assert.NotNull(decrypted); Assert.Equal(raw, decrypted.GetString()); // with salt encrypted = encryptor.Encrypt(raw, key, salt); Assert.NotNull(encrypted); decrypted = encryptor.Decrypt(encrypted, key); Assert.NotNull(decrypted); Assert.Equal(raw, decrypted.GetString()); }
private static string _Decrypt(string cipherText, Keys objKey) { var cipherBytes = Convert.FromBase64String(cipherText.Trim()); using (var encryptor = Aes.Create()) { if (encryptor == null) { return(cipherText); } var pdb = new Rfc2898DeriveBytes(objKey.KeyCode, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); encryptor.Key = pdb.GetBytes(32); encryptor.IV = pdb.GetBytes(16); using (var ms = new MemoryStream()) { using (var cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write)) { cs.Write(cipherBytes, 0, cipherBytes.Length); cs.Close(); } cipherText = Encoding.Unicode.GetString(ms.ToArray()); } } return(cipherText.Trim()); }
public void EncryptEcb(Span <byte> data) { Vector128 <byte>[] keys = roundKeys; Span <Vector128 <byte> > blocks = MemoryMarshal.Cast <byte, Vector128 <byte> >(data); for (int i = 0; i < blocks.Length; i++) { Vector128 <byte> b = blocks[i]; b = Sse2.Xor(b, keys[0]); b = Aes.Encrypt(b, keys[1]); b = Aes.Encrypt(b, keys[2]); b = Aes.Encrypt(b, keys[3]); b = Aes.Encrypt(b, keys[4]); b = Aes.Encrypt(b, keys[5]); b = Aes.Encrypt(b, keys[6]); b = Aes.Encrypt(b, keys[7]); b = Aes.Encrypt(b, keys[8]); b = Aes.Encrypt(b, keys[9]); b = Aes.EncryptLast(b, keys[10]); blocks[i] = b; } }
/// <summary> /// DES解密 /// </summary> /// <param name="decryptString">待解密的字符串</param> /// <param name="decryptKey">解密密钥,要求为16位,和加密密钥相同</param> /// <returns>解密成功返回解密后的字符串,失败返回空</returns> public static string DESDecrypt(string decryptString, string decryptKey) { try { if (decryptKey.Length != 16) { return(string.Empty); } byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 16)); byte[] rgbIV = Keys; byte[] inputByteArray = Convert.FromBase64String(decryptString); var DCSP = Aes.Create(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); Byte[] inputByteArrays = new byte[inputByteArray.Length]; cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return(Encoding.UTF8.GetString(mStream.ToArray())); } catch { return(string.Empty); } }
public static string Encrypt(string plainText) { try { var key = GetKey(); var cryptoProvider = Aes.Create(); var memStream = new MemoryStream(); var cryptoStream = new CryptoStream(memStream, cryptoProvider.CreateEncryptor(Encoding.ASCII.GetBytes(key[0]), Encoding.ASCII.GetBytes(key[1])), CryptoStreamMode.Write); var writer = new StreamWriter(cryptoStream); writer.Write(plainText); writer.Flush(); cryptoStream.FlushFinalBlock(); writer.Flush(); var cipher = Convert.ToBase64String(memStream.ToArray(), 0, Convert.ToInt32(memStream.Length)); return(cipher.Replace('+', '!')); } catch (Exception ex) { throw ex; } }
public static string Encrypt(string stringToEncrypt) { byte[] encryptedToken; using (var aes = Aes.Create()) { aes.Key = _keyBytes; aes.IV = _ivBytes; var encryptor = aes.CreateEncryptor(aes.Key, aes.IV); using (var mStream = new MemoryStream()) { using (var cryptoStream = new CryptoStream(mStream, encryptor, CryptoStreamMode.Write)) { using (var streamWriter = new StreamWriter(cryptoStream)) { streamWriter.Write(stringToEncrypt); } encryptedToken = mStream.ToArray(); } } } return(Convert.ToBase64String(encryptedToken)); }
public static MemoryStream Encrypt(this Stream inputStream, byte[] key, byte[] iv) { if (key.Length != _KEY_LENGTH / 8 || iv.Length != _KEY_LENGTH / 8) { throw new ArgumentException("Wrong length of encryption key or iv", "key"); } using (var algorithm = Aes.Create()) { algorithm.KeySize = _KEY_LENGTH; algorithm.Mode = CipherMode.CBC; algorithm.Padding = PaddingMode.PKCS7; using (var inStream = inputStream) using (var outStream = new MemoryStream()) { using (var encryptor = algorithm.CreateEncryptor(key, iv)) using (var crypt = new CryptoStream(outStream, encryptor, CryptoStreamMode.Write)) using (var compress = new GZipStream(crypt, CompressionMode.Compress)) inStream.CopyTo(compress); return(outStream); } } }
public static string EncryptStringAES256(string plainText, string key, string iv) { byte[] result; Aes aes256Item = null; try { aes256Item = Aes.Create(); aes256Item.Padding = PaddingMode.PKCS7; aes256Item.Mode = CipherMode.CBC; ICryptoTransform transform = aes256Item.CreateEncryptor(Encoding.UTF8.GetBytes(key), Encoding.UTF8.GetBytes(iv)); byte[] bText = Encoding.UTF8.GetBytes(plainText); result = transform.TransformFinalBlock(bText, 0, bText.Length); } finally { if (aes256Item != null) { aes256Item.Clear(); } } return(Convert.ToBase64String(result)); }
public static string Encrypt(string _textToEncrypt) { byte[] clearBytes = Encoding.Unicode.GetBytes(_textToEncrypt); using (Aes encryptor = Aes.Create()) { Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(ENCRIPTION_KEY, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); encryptor.Key = pdb.GetBytes(32); encryptor.IV = pdb.GetBytes(16); using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)) { cs.Write(clearBytes, 0, clearBytes.Length); cs.Close(); } _textToEncrypt = Convert.ToBase64String(ms.ToArray()); } } return(_textToEncrypt); }
public static string EncryptString_Aes(string plainText) { // Check arguments. if (plainText == null || plainText.Length <= 0) { throw new ArgumentNullException("plainText"); } string encrypted; // Create an Aes object // with the specified key and IV. using (Aes aesAlg = Aes.Create()) { aesAlg.Key = Convert.FromBase64String(EncryptionKey); aesAlg.IV = Convert.FromBase64String(EncriptionIV); // Create an encryptor to perform the stream transform. ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); // Create the streams used for encryption. using (MemoryStream msEncrypt = new MemoryStream()) { using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) { using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) { //Write all data to the stream. swEncrypt.Write(plainText); } encrypted = Convert.ToBase64String(msEncrypt.ToArray()); } } } return(encrypted); }
static string DecryptStringFromBytes_Aes(string cipherText, byte[] Key, byte[] IV) { string result = null; using (var aesAlg = Aes.Create()) { aesAlg.Key = Key; aesAlg.IV = IV; var decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); using (var msDecrypt = new MemoryStream(Convert.FromBase64String(cipherText))) { using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (var srDecrypt = new StreamReader(csDecrypt)) { result = srDecrypt.ReadToEnd(); } } } } return(result); }
/* * Function for decrypting text as bytes from a key and IV byte array * Based on documentation from Microsoft: https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.aes?view=netframework-4.8 */ private static string Decrypt(byte[] text, byte[] Key, byte[] IV) { string plainText = string.Empty; using (Aes aesAlgo = Aes.Create()) { aesAlgo.Key = Key; aesAlgo.IV = IV; ICryptoTransform decryptor = aesAlgo.CreateDecryptor(aesAlgo.Key, aesAlgo.IV); using (MemoryStream msDecrypt = new MemoryStream(text)) { using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (StreamReader scDecrypt = new StreamReader(csDecrypt)) { plainText = scDecrypt.ReadToEnd(); } } } } return(plainText); }
// Central Method for string encryption public static string encrypt(string encryptString) { string EncryptionKey = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; byte[] clearBytes = Encoding.Unicode.GetBytes(encryptString); using (System.Security.Cryptography.Aes encryptor = Aes.Create()) { Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); encryptor.Key = pdb.GetBytes(32); encryptor.IV = pdb.GetBytes(16); using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)) { cs.Write(clearBytes, 0, clearBytes.Length); cs.Close(); } encryptString = Convert.ToBase64String(ms.ToArray()); } } return(encryptString); }
static string DecryptStringFromBytes_Aes(byte[] cipherTextCombined, byte[] key) { // Declare the string used to hold the decrypted text. string plaintext = null; using (Aes aesAlg = Aes.Create()) { aesAlg.Key = key; byte[] IV = new byte[aesAlg.BlockSize / 8]; byte[] cipherText = new byte[cipherTextCombined.Length - IV.Length]; Array.Copy(cipherTextCombined, IV, IV.Length); Array.Copy(cipherTextCombined, IV.Length, cipherText, 0, cipherText.Length); aesAlg.IV = IV; // Create a decrytor to perform the stream transform. ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); // Create the streams used for decryption. using (MemoryStream msDecrypt = new MemoryStream(cipherText)) { using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (StreamReader srDecrypt = new StreamReader(csDecrypt)) { // Read the decrypted bytes from the decrypting stream and place them in a string. plaintext = srDecrypt.ReadToEnd(); } } } } return(plaintext); }
private static Vector128 <byte>[] KeyExpansion(Span <byte> key) { var keys = new Vector128 <byte> [20]; keys[0] = Unsafe.ReadUnaligned <Vector128 <byte> >(ref key[0]); MakeRoundKey(keys, 1, 0x01); MakeRoundKey(keys, 2, 0x02); MakeRoundKey(keys, 3, 0x04); MakeRoundKey(keys, 4, 0x08); MakeRoundKey(keys, 5, 0x10); MakeRoundKey(keys, 6, 0x20); MakeRoundKey(keys, 7, 0x40); MakeRoundKey(keys, 8, 0x80); MakeRoundKey(keys, 9, 0x1b); MakeRoundKey(keys, 10, 0x36); for (int i = 1; i < 10; i++) { keys[10 + i] = Aes.InverseMixColumns(keys[i]); } return(keys); }
public static string Encryptt(string encryptString) { string EncryptionKey = "RE358P71305KMCHA8721DFA684ZXCZNCXD0QMVJD4220L"; byte[] clearBytes = Encoding.Unicode.GetBytes(encryptString); using (Aes encryptor = Aes.Create()) { Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); encryptor.Key = pdb.GetBytes(32); encryptor.IV = pdb.GetBytes(16); using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)) { cs.Write(clearBytes, 0, clearBytes.Length); cs.Close(); } encryptString = Convert.ToBase64String(ms.ToArray()); } } return(encryptString); }
public override void Bad() { if (privateFive == 5) { string plainText = "ABCDEFG123456"; byte[] encrypted; using (Aes aesAlg = Aes.Create()) { // Create an encryptor to perform the stream transform. ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); // Create the streams used for encryption. using (MemoryStream msEncrypt = new MemoryStream()) { /* FLAW: Missing required step (using CryptoStream). This will result in the payload to remain in plaintext form */ using (StreamWriter swEncrypt = new StreamWriter(msEncrypt)) { swEncrypt.Write(plainText); } encrypted = msEncrypt.ToArray(); } } IO.WriteLine(IO.ToHex(encrypted)); } }
public static string Encrypt(string clearText) { string EncryptionKey = "MAKV2SPBNI99212"; byte[] clearBytes = Encoding.Unicode.GetBytes(clearText); using (Aes encryptor = Aes.Create()) { Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 73, 118, 97, 110, 32, 77, 101, 100, 118, 101, 100, 101, 118 }); encryptor.Key = pdb.GetBytes(32); encryptor.IV = pdb.GetBytes(16); using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)) { cs.Write(clearBytes, 0, clearBytes.Length); cs.Close(); } clearText = Convert.ToBase64String(ms.ToArray()); } } return(clearText); }
public static string Encrypt(string encryptString) { string EncryptionKey = "0ram@1234xxxxxxxxxxtttttuuuuuiiiiio"; //we can change the code converstion key as per our requirement byte[] clearBytes = Encoding.Unicode.GetBytes(encryptString); using (Aes encryptor = Aes.Create()) { Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); encryptor.Key = pdb.GetBytes(32); encryptor.IV = pdb.GetBytes(16); using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)) { cs.Write(clearBytes, 0, clearBytes.Length); cs.Close(); } encryptString = Convert.ToBase64String(ms.ToArray()); } } return(encryptString); }
public byte[] Encrypt(string plainText) { byte[] encrypted; using (Aes aesAlg = Aes.Create()) { aesAlg.GenerateIV(); aeskey = aesAlg.Key; aesiv = aesAlg.IV; ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); using (MemoryStream msEncrypt = new MemoryStream()) { using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) { using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) { swEncrypt.Write(plainText); } encrypted = msEncrypt.ToArray(); } } return(encrypted); } }
public static byte[] AESEncrypt(string plainText, string key) { byte[] encrypted; byte[] IV; using (Aes aesAlg = Aes.Create()) { aesAlg.Key = Convert.FromBase64String(key); aesAlg.GenerateIV(); IV = aesAlg.IV; aesAlg.Mode = CipherMode.CBC; aesAlg.Padding = PaddingMode.PKCS7; ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); using (MemoryStream msEncrypt = new MemoryStream()) { using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) { using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) { swEncrypt.Write(plainText); } encrypted = msEncrypt.ToArray(); } } } byte[] combinedIvCt = new byte[IV.Length + encrypted.Length]; Array.Copy(IV, 0, combinedIvCt, 0, IV.Length); Array.Copy(encrypted, 0, combinedIvCt, IV.Length, encrypted.Length); return(combinedIvCt); }
public void DecryptEcb(Span <byte> data) { Vector128 <byte>[] keys = roundKeys; Span <Vector128 <byte> > blocks = MemoryMarshal.Cast <byte, Vector128 <byte> >(data); for (int i = 0; i < blocks.Length; i++) { Vector128 <byte> b = blocks[i]; b = Sse2.Xor(b, keys[10]); b = Aes.Decrypt(b, keys[19]); b = Aes.Decrypt(b, keys[18]); b = Aes.Decrypt(b, keys[17]); b = Aes.Decrypt(b, keys[16]); b = Aes.Decrypt(b, keys[15]); b = Aes.Decrypt(b, keys[14]); b = Aes.Decrypt(b, keys[13]); b = Aes.Decrypt(b, keys[12]); b = Aes.Decrypt(b, keys[11]); b = Aes.DecryptLast(b, keys[0]); blocks[i] = b; } }
public static byte[] AESEncrypt(byte[] key, byte[] data) { byte[] iv = new byte[16]; using (Aes aes = Aes.Create()) { aes.Key = key; aes.IV = iv; ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV); using (MemoryStream memoryStream = new MemoryStream()) { using (CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, encryptor, CryptoStreamMode.Write)) { using (BinaryWriter streamWriter = new BinaryWriter((Stream)cryptoStream)) { streamWriter.Write(data); } return(memoryStream.ToArray()); } } } }
public string Encrypt(string password, string saltValue) { string secretKey = "0406b130-bd65-11ea-b3de-0242ac130004"; var saltBuffer = Encoding.UTF8.GetBytes(saltValue); byte[] clearBytes = Encoding.Unicode.GetBytes(password); using (Aes encryptor = Aes.Create()) { Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(secretKey, saltBuffer); encryptor.Key = pdb.GetBytes(32); encryptor.IV = pdb.GetBytes(16); using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)) { cs.Write(clearBytes, 0, clearBytes.Length); cs.Close(); } password = Convert.ToBase64String(ms.ToArray()); } } return(password); }
public static string DecryptStringFromBytes_Aes(byte[] cipherTextCombined, byte[] Key) { string plaintext = null; using (Aes aesAlg = Aes.Create()) { aesAlg.Key = Key; byte[] IV = new byte[aesAlg.BlockSize / 8]; byte[] cipherText = new byte[cipherTextCombined.Length - IV.Length]; Array.Copy(cipherTextCombined, IV, IV.Length); Array.Copy(cipherTextCombined, IV.Length, cipherText, 0, cipherText.Length); aesAlg.IV = IV; aesAlg.Mode = CipherMode.CBC; ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); using (var msDecrypt = new MemoryStream(cipherText)) { using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (var srDecrypt = new StreamReader(csDecrypt)) { // Read the decrypted bytes from the decrypting stream // and place them in a string. plaintext = srDecrypt.ReadToEnd(); } } } } return plaintext; }
private static void Encrypt(string inputFilePath, string outputfilePath) { using (Aes encryptor = Aes.Create()) { Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); encryptor.Key = pdb.GetBytes(32); encryptor.IV = pdb.GetBytes(16); using (FileStream fsOutput = new FileStream(outputfilePath, FileMode.Create)) { using (CryptoStream cs = new CryptoStream(fsOutput, encryptor.CreateEncryptor(), CryptoStreamMode.Write)) { using (FileStream fsInput = new FileStream(inputFilePath, FileMode.Open)) { int data; while ((data = fsInput.ReadByte()) != -1) { cs.WriteByte((byte)data); } } } } } File.Delete(inputFilePath); }
public override ICryptoTransform CreateEncryptor(byte[] key, byte[] iv, byte[] authenticationData) { if (key == null) { throw new CryptographicException("No key material"); } if (iv == null) { throw new CryptographicException("No initialization vector"); } // Create the AES provider using (var aes = Aes.Create()) { aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.PKCS7; aes.KeySize = key.Length * 8; aes.Key = key; aes.IV = iv; return(aes.CreateEncryptor()); } }
public static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key) { byte[] encrypted; byte[] IV; using (Aes aesAlg = Aes.Create()) { aesAlg.Key = Key; aesAlg.GenerateIV(); IV = aesAlg.IV; aesAlg.Mode = CipherMode.CBC; var encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); using (var msEncrypt = new MemoryStream()) { using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) { using (var swEncrypt = new StreamWriter(csEncrypt)) { swEncrypt.Write(plainText); } encrypted = msEncrypt.ToArray(); } } } var combinedIvCt = new byte[IV.Length + encrypted.Length]; Array.Copy(IV, 0, combinedIvCt, 0, IV.Length); Array.Copy(encrypted, 0, combinedIvCt, IV.Length, encrypted.Length); return(combinedIvCt); }
public static String Encrypt(String plainText, byte[] salt) { try { byte[] clearBytes = Encoding.Unicode.GetBytes(plainText); using (Aes encryptor = Aes.Create()) { Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(_encryptionKey, salt); encryptor.Key = pdb.GetBytes(32); encryptor.IV = pdb.GetBytes(16); using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)) { cs.Write(clearBytes, 0, clearBytes.Length); cs.Close(); } plainText = Convert.ToBase64String(ms.ToArray()); } } } catch (Exception ex) { throw ex; } return(plainText); }
public string Encrypt(string toEncrypt, string encryptionKey, string salt) { ThrowOnBlank(nameof(encryptionKey), encryptionKey); ThrowOnBlank(nameof(salt), salt); byte[] toEncryptBytes = Encoding.Unicode.GetBytes(toEncrypt); byte[] saltBytes = Encoding.Unicode.GetBytes(salt); using (Aes encryptor = Aes.Create()) { Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(encryptionKey, saltBytes); encryptor.Key = pdb.GetBytes(32); encryptor.IV = pdb.GetBytes(16); using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)) { cs.Write(toEncryptBytes, 0, toEncryptBytes.Length); cs.Close(); } toEncrypt = Convert.ToBase64String(ms.ToArray()); } } return(toEncrypt); }
public static string Decrypt(string cipherText, string saltValue) { string secretKey = "0406b130-bd65-11ea-b3de-0242ac130004"; var saltBuffer = Encoding.UTF8.GetBytes(saltValue); cipherText = cipherText.Replace(" ", "+"); byte[] cipherBytes = Convert.FromBase64String(cipherText); using (Aes encryptor = Aes.Create()) { Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(secretKey, saltBuffer); encryptor.Key = pdb.GetBytes(32); encryptor.IV = pdb.GetBytes(16); using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write)) { cs.Write(cipherBytes, 0, cipherBytes.Length); cs.Close(); } cipherText = Encoding.Unicode.GetString(ms.ToArray()); } } return(cipherText); }
private static byte[] AesDecryptDirectKey(Aes aes, byte[] key, byte[] iv, byte[] cipherBytes) { using (MemoryStream output = new MemoryStream()) using (CryptoStream cryptoStream = new CryptoStream(output, aes.CreateDecryptor(key, iv), CryptoStreamMode.Write)) { cryptoStream.Write(cipherBytes, 0, cipherBytes.Length); cryptoStream.FlushFinalBlock(); return output.ToArray(); } }
private static void VerifyKeyGeneration(Aes aes) { int keySize = aes.KeySize; aes.GenerateKey(); byte[] key = aes.Key; Assert.NotNull(key); Assert.Equal(keySize, aes.KeySize); Assert.Equal(keySize, key.Length * 8); // Standard randomness caveat: There's a very low chance that the generated key -is- // all zeroes. For a 128-bit key this is 1/2^128, which is more unlikely than 1/10^38. Assert.NotEqual(new byte[key.Length], key); }
private static void ValidateTransformProperties(Aes aes, ICryptoTransform transform) { Assert.NotNull(transform); Assert.Equal(aes.BlockSize, transform.InputBlockSize * 8); Assert.Equal(aes.BlockSize, transform.OutputBlockSize * 8); Assert.True(transform.CanTransformMultipleBlocks); }
public void SetUp() { _aes = new Aes(); }
private static void RandomKeyRoundtrip(Aes aes) { byte[] decryptedBytes; byte[] encryptedBytes; using (MemoryStream input = new MemoryStream(s_multiBlockBytes)) using (CryptoStream cryptoStream = new CryptoStream(input, aes.CreateEncryptor(), CryptoStreamMode.Read)) using (MemoryStream output = new MemoryStream()) { cryptoStream.CopyTo(output); encryptedBytes = output.ToArray(); } Assert.NotEqual(s_multiBlockBytes, encryptedBytes); using (MemoryStream input = new MemoryStream(encryptedBytes)) using (CryptoStream cryptoStream = new CryptoStream(input, aes.CreateDecryptor(), CryptoStreamMode.Read)) using (MemoryStream output = new MemoryStream()) { cryptoStream.CopyTo(output); decryptedBytes = output.ToArray(); } Assert.Equal(s_multiBlockBytes, decryptedBytes); }
public static byte[] DecryptByteBlock(byte[] dataToDecrypt, byte[] password) { if (dataToDecrypt == null) { throw new ArgumentNullException("dataToDecrypt"); } if (password == null) { throw new ArgumentNullException("password"); } var aes = new Aes(); var salt = ByteHelpers.CreateSpecialByteArray(32); var message = ByteHelpers.CreateSpecialByteArray(dataToDecrypt.Length - 32); Buffer.BlockCopy(dataToDecrypt, 0, salt, 0, 32); Buffer.BlockCopy(dataToDecrypt, 32, message, 0, dataToDecrypt.Length - 32); var deCompressed = Compressor.Decompress(aes.Decrypt(message, password, salt, PBKDF2_ITERATIONS)); return deCompressed; }
async Task<PremiumAccessToken> GetPremiumTokenInternal(string encryptedPremiumToken, string apiKey) { var premiumToken = _settings.AccountOptions.UserInfo.Token; try { var premiumCached = _firstCompleted && premiumToken.IsPremium() && premiumToken.IsValidInNearFuture(); if (!premiumCached) { var aes = new Aes(); var sha1 = new SHA1Hash(); var keyHash = await sha1.GetHashAsync(apiKey).ConfigureAwait(false); var unencryptedPremiumToken = await aes.DecryptAsync(encryptedPremiumToken, keyHash).ConfigureAwait(false); premiumToken = JsonConvert.DeserializeObject<PremiumAccessToken>(unencryptedPremiumToken); } } catch (NotPremiumUserException) { premiumToken = null; } return premiumToken; }
public AesTransform(Aes algo, bool encryption, byte[] key, byte[] iv) : base(algo, encryption, iv) { if (key == null) throw new CryptographicException("key is null"); if ((iv != null) && (iv.Length != (algo.BlockSize >> 3))) { string msg = Locale.GetText("IV length is invalid ({0} bytes), it should be {1} bytes long.", iv.Length, (algo.BlockSize >> 3)); throw new CryptographicException(msg); } int keySize = key.Length; if (keySize != 16 && keySize != 24 && keySize != 32) { string msg = Locale.GetText("Key is too small ({0} bytes), it should be {1}, {2} or {3} bytes long.", keySize, 16, 24, 32); throw new CryptographicException(msg); } keySize <<= 3; // bytes -> bits this.Nk = (keySize >> 5); // div 32 if (Nk == 8) { Nr = 14; } else if (Nk == 6) { Nr = 12; } else { Nr = 10; } // Setup Expanded Key int exKeySize = Nb * (Nr + 1); UInt32[] exKey = new UInt32[exKeySize]; int pos = 0; for (int i = 0; i < Nk; i++) { UInt32 value = ((UInt32)key[pos++] << 24); value |= ((UInt32)key[pos++] << 16); value |= ((UInt32)key[pos++] << 8); value |= ((UInt32)key[pos++]); exKey[i] = value; } for (int i = Nk; i < exKeySize; i++) { UInt32 temp = exKey[i - 1]; if (i % Nk == 0) { UInt32 rot = (UInt32)((temp << 8) | ((temp >> 24) & 0xff)); temp = SubByte(rot) ^ Rcon[i / Nk]; } else if (Nk > 6 && (i % Nk) == 4) { temp = SubByte(temp); } exKey[i] = exKey[i - Nk] ^ temp; } #if NET_2_1 // Silverlight 2.0 only supports CBC if (!encryption) { #else if (!encryption && (algo.Mode == CipherMode.ECB || algo.Mode == CipherMode.CBC)) { #endif for (int i = 0, k = exKeySize - Nb; i < k; i += Nb, k -= Nb) { for (int j = 0; j < Nb; j++) { uint temp = exKey[i + j]; exKey[i + j] = exKey[k + j]; exKey[k + j] = temp; } } for (int i = Nb; i < exKey.Length - Nb; i++) { exKey[i] = iT0[SBox[(exKey[i] >> 24)]] ^ iT1[SBox[(byte)(exKey[i] >> 16)]] ^ iT2[SBox[(byte)(exKey[i] >> 8)]] ^ iT3[SBox[(byte)exKey[i]]]; } } expandedKey = exKey; } public void Clear() { Dispose(true); }