public byte[] EncryptEcb(byte[] data, byte[] seed, bool useEntropy = true) { byte[] ciphertext; using (var rand = new DeterministicCryptoRandomGenerator(seed, useEntropy)) { using (var aesAlg = Aes.Create()) { var keyBytes = new byte[aesAlg.KeySize / 8]; rand.GetBytes(keyBytes, 0, aesAlg.KeySize / 8); aesAlg.Key = keyBytes; aesAlg.Mode = CipherMode.ECB; aesAlg.Padding = PaddingMode.Zeros; // Create a crypto stream to perform encryption using (ICryptoTransform ecryptor = aesAlg.CreateEncryptor()) { // write encrypted bytes to memory ciphertext = TransformHelper.PerformCryptography(ecryptor, data); } } } return(ciphertext); }
public byte[] DecryptCbc(byte[] ciphertext, byte[] seed) { using (var rand = new DeterministicCryptoRandomGenerator(seed, false)) { byte[] iv; using (var aesAlg = Aes.Create()) { aesAlg.KeySize = AesKeySize; var keyBytes = new byte[aesAlg.KeySize / 8]; rand.GetBytes(keyBytes, 0, aesAlg.KeySize / 8); iv = keyBytes; aesAlg.Key = keyBytes; aesAlg.IV = iv; aesAlg.Mode = CipherMode.CBC; aesAlg.Padding = PaddingMode.Zeros; // Create the streams used for encryption. // Open a new memory stream to write the encrypted data to // Create a crypto stream to perform encryption using (var decryptor = aesAlg.CreateDecryptor()) { // write encrypted bytes to memory return(TransformHelper.PerformCryptography(decryptor, ciphertext)); } } } }
public byte[] ConputeMacWithKey(byte[] data, byte[] seed) { using (var rand = new DeterministicCryptoRandomGenerator(seed, false)) { byte[] iv; byte[] ciphertext; byte[] keyBytes; using (var aesAlg = Aes.Create()) { aesAlg.KeySize = AesKeySize; keyBytes = new byte[aesAlg.KeySize / 8]; rand.GetBytes(keyBytes, 0, aesAlg.KeySize / 8); iv = new byte[AesKeySize / 8]; aesAlg.Key = keyBytes; aesAlg.Padding = PaddingMode.PKCS7; aesAlg.IV = iv; aesAlg.Mode = CipherMode.CBC; // Create the streams used for encryption. // Open a new memory stream to write the encrypted data to // Create a crypto stream to perform encryption using (var ecryptor = aesAlg.CreateEncryptor()) { // write encrypted bytes to memory ciphertext = TransformHelper.PerformCryptography(ecryptor, data); } } // return last block return(ciphertext.Skip(ciphertext.Length - AesBlockSize).ToArray()); } }
public byte[] DecryptCbc(byte[] ciphertext, byte[] seed, PaddingMode paddingMode = PaddingMode.PKCS7) { using (var rand = new DeterministicCryptoRandomGenerator(seed, false)) { byte[] iv; using (var aesAlg = Aes.Create()) { if (ciphertext.Length < aesAlg.BlockSize * 2 / 8) { throw new Exception("Invalid CT len, expecting at least 2 blocks"); } aesAlg.KeySize = AesKeySize; var keyBytes = new byte[aesAlg.KeySize / 8]; rand.GetBytes(keyBytes, 0, aesAlg.KeySize / 8); iv = ciphertext.Take(aesAlg.BlockSize / 8).ToArray(); ciphertext = ciphertext.Skip(aesAlg.BlockSize / 8).ToArray(); aesAlg.Key = keyBytes; aesAlg.IV = iv; aesAlg.Mode = CipherMode.CBC; aesAlg.Padding = paddingMode; // Create the streams used for encryption. // Open a new memory stream to write the encrypted data to // Create a crypto stream to perform encryption using (var decryptor = aesAlg.CreateDecryptor()) { // write encrypted bytes to memory return(TransformHelper.PerformCryptography(decryptor, ciphertext)); } } } }
private byte[] GetSecretKey(string userId, string challengeId) { using (var hash = SHA256.Create()) { var seed = hash.ComputeHash(Encoding.ASCII.GetBytes(userId + challengeId)); var prg = new DeterministicCryptoRandomGenerator(seed, false); var keyLengthBytes = new byte[4]; prg.GetBytes(keyLengthBytes); var keyLength = BitConverter.ToUInt32(keyLengthBytes, 0) % 64 + 1; var key = new byte[keyLength]; prg.GetBytes(key); return(key); } }
public byte[] GetKey(byte[] seed) { byte[] keyBytes = new byte[AesKeySize / 8]; using (var rand = new DeterministicCryptoRandomGenerator(seed, false)) { rand.GetBytes(keyBytes, 0, keyBytes.Length); } return(keyBytes); }
public byte[] GetRandomString(int length, byte[] seed, bool useEntropy) { var result = new byte[length]; using (var rand = new DeterministicCryptoRandomGenerator(seed, useEntropy)) { rand.GetBytes(result); } return(result); }
public byte[] EncryptCbc(byte[] data, byte[] seed, bool useEntropy = true, bool includeIv = true) { byte[] iv = new byte[AesBlockSize]; using (var rand = new DeterministicCryptoRandomGenerator(seed, useEntropy)) { byte[] ciphertext; using (var aesAlg = Aes.Create()) { var keyBytes = new byte[aesAlg.KeySize / 8]; rand.GetBytes(keyBytes, 0, aesAlg.KeySize / 8); aesAlg.Key = keyBytes; rand.GetBytes(iv); aesAlg.IV = iv; aesAlg.Mode = CipherMode.CBC; aesAlg.Padding = PaddingMode.Zeros; // Create the streams used for encryption. // Open a new memory stream to write the encrypted data to // Create a crypto stream to perform encryption using (ICryptoTransform ecryptor = aesAlg.CreateEncryptor()) { // write encrypted bytes to memory ciphertext = TransformHelper.PerformCryptography(ecryptor, data); } } if (!includeIv) { return(ciphertext); } var result = new byte[iv.Length + ciphertext.Length]; Array.Copy(iv, 0, result, 0, iv.Length); Array.Copy(ciphertext, 0, result, iv.Length, ciphertext.Length); // Return the encrypted bytes from the memory stream. return(result); } }
private byte[] PadData(byte[] data, byte[] seed, bool useEntropy = true) { byte[] result; using (var rand = new DeterministicCryptoRandomGenerator(seed, useEntropy)) { byte[] randomData = new byte[2]; rand.GetBytes(randomData, 0, randomData.Length); var firstOffsetValue = BitConverter.ToUInt32(data, 0); var secondOffsetValue = BitConverter.ToUInt32(data, 1); var firstOffSet = (int)(firstOffsetValue % 5 + 5); var secondOffSet = (int)(secondOffsetValue % 5 + 5); result = new byte[data.Length + firstOffSet + secondOffSet]; rand.GetBytes(result, 0, firstOffSet); Array.Copy(data, 0, result, firstOffSet, data.Length); rand.GetBytes(result, data.Length + firstOffSet, secondOffSet); } return(result); }
public byte[] ApplyMac(byte[] data, byte[] seed) { using (var rand = new DeterministicCryptoRandomGenerator(seed, false)) { var hmacKey = new byte[PaddingOracleManger.HmacKeyLen]; rand.GetBytes(hmacKey, 0, hmacKey.Length); using (HMAC hmac = new HMACSHA256(hmacKey)) { var mac = hmac.ComputeHash(data); var result = new byte[data.Length + hmac.HashSize / 8]; Array.Copy(data, 0, result, 0, data.Length); Array.Copy(mac, 0, result, data.Length, hmac.HashSize / 8); return(result); } } }
public byte[] VerifyMac(byte[] data, byte[] seed) { using (var rand = new DeterministicCryptoRandomGenerator(seed, false)) { var hmacKey = new byte[PaddingOracleManger.HmacKeyLen]; rand.GetBytes(hmacKey, 0, hmacKey.Length); using (HMAC hmac = new HMACSHA256(hmacKey)) { var dataToCheck = data.Take(data.Length - (hmac.HashSize / 8)).ToArray(); var mac = data.Skip(dataToCheck.Length).ToArray(); var computedMac = hmac.ComputeHash(dataToCheck); if (!CompareHelper.CompareArrays(mac, computedMac)) { throw new Exception("Invalid MAC!"); } return(dataToCheck); } } }
public byte[] DecryptCtr(byte[] ciphertext, byte[] seed, byte[] ctr = null) { if (ciphertext.Length < AesBlockSize && ctr == null) { throw new Exception($"CT len should be >= {AesBlockSize} bytes!"); } if (ctr != null && ctr.Length != 16) { throw new Exception($"CTR shoul be {AesBlockSize} bytes!"); } byte[] encrypted; if (ctr == null) { ctr = ciphertext.Take(16).ToArray(); encrypted = ciphertext.Skip(16).ToArray(); } else { encrypted = ciphertext; } var keyRand = new DeterministicCryptoRandomGenerator(seed, false); byte[] result; using (var aesAlg = new Aes128CounterMode(ctr)) { var keyBytes = new byte[aesAlg.KeySize / 8]; keyRand.GetBytes(keyBytes, 0, aesAlg.KeySize / 8); aesAlg.Key = keyBytes; using (ICryptoTransform decryptor = aesAlg.CreateDecryptor()) { result = TransformHelper.PerformCryptography(decryptor, encrypted); } } return(result); }
public byte[] EncryptCrt(byte[] data, byte[] seed, bool useEntropy = true, bool includeCtr = true) { byte[] ciphertext; byte[] ctrInit = new byte[AesBlockSize]; using (var rand = new DeterministicCryptoRandomGenerator(seed, useEntropy)) { using (var keyRand = new DeterministicCryptoRandomGenerator(seed, false)) { rand.GetBytes(ctrInit); byte[] ctr = new byte[ctrInit.Length]; ctrInit.CopyTo(ctr, 0); using (var aesAlg = new Aes128CounterMode(ctr)) { var keyBytes = new byte[aesAlg.KeySize / 8]; keyRand.GetBytes(keyBytes, 0, aesAlg.KeySize / 8); aesAlg.Key = keyBytes; // Create a crypto stream to perform encryption using (ICryptoTransform ecryptor = aesAlg.CreateEncryptor()) { // write encrypted bytes to memory ciphertext = TransformHelper.PerformCryptography(ecryptor, data); } } if (!includeCtr) { return(ciphertext); } var result = new byte[ctrInit.Length + ciphertext.Length]; Array.Copy(ctrInit, 0, result, 0, ctrInit.Length); Array.Copy(ciphertext, 0, result, ctrInit.Length, ciphertext.Length); // Return the encrypted bytes from the memory stream. return(result); } } }