public void TestDecryptWrongAesKey() { const string dataFileName = "PNG_transparency_demonstration_1.png"; Assert.Equal(true, File.Exists(dataFileName)); var dataToEncrypt = File.ReadAllBytes(dataFileName); var pass = Encoding.UTF8.GetBytes("TestPassword"); var salt = new byte[32]; var mySha256 = SHA256.Create(); int seed = BitConverter.ToInt32(mySha256.ComputeHash(BitConverter.GetBytes((int)DateTime.UtcNow.Ticks & DateTime.UtcNow.Millisecond)), 0); var randomSource = new Random(seed); randomSource.NextBytes(salt); var keyIvGenerated = CryptConfigFileHelper.GenKeyAndIv(pass, salt); byte[] encryptedData = keyIvGenerated.EncryptData(dataToEncrypt); var changedKey = new byte[keyIvGenerated.Key.Length]; keyIvGenerated.Key.CopyTo(changedKey, 0); changedKey[0] = (byte)(changedKey[0] ^ 3); Assert.Throws<EnumException<AesKeyIvPair.EDecryptDataErrCodes>>(() => { byte[] originData = new AesKeyIvPair {Iv = keyIvGenerated.Iv, Key = changedKey} .DecryptData(encryptedData); _output.WriteLine("Err key decrypted"); Assert.Equal(originData, dataToEncrypt); }); }
public static AesKeyIvPair GenAesKeyIvPair() { var result = new AesKeyIvPair(); MiscFuncs.GetRandomBytes(result.Iv); MiscFuncs.GetRandomBytes(result.Key); return result; }
public static AesKeyIvPair GenAesKeyIvPair() { var result = new AesKeyIvPair(); MiscFuncs.GetRandomBytes(result.Iv); MiscFuncs.GetRandomBytes(result.Key); return(result); }