private static void TestRijndael() { // Test vector (official ECB test vector #356) byte[] pbIV = new byte[16]; byte[] pbTestKey = new byte[32]; byte[] pbTestData = new byte[16]; byte[] pbReferenceCT = new byte[16] { 0x75, 0xD1, 0x1B, 0x0E, 0x3A, 0x68, 0xC4, 0x22, 0x3D, 0x88, 0xDB, 0xF0, 0x17, 0x97, 0x7D, 0xD7 }; int i; for(i = 0; i < 16; ++i) pbIV[i] = 0; for(i = 0; i < 32; ++i) pbTestKey[i] = 0; for(i = 0; i < 16; ++i) pbTestData[i] = 0; pbTestData[0] = 0x04; #if KeePassUAP AesEngine r = new AesEngine(); r.Init(true, new KeyParameter(pbTestKey)); if(r.GetBlockSize() != pbTestData.Length) throw new SecurityException(KLRes.EncAlgorithmAes + " (BS)."); r.ProcessBlock(pbTestData, 0, pbTestData, 0); #else RijndaelManaged r = new RijndaelManaged(); if(r.BlockSize != 128) // AES block size { Debug.Assert(false); r.BlockSize = 128; } r.IV = pbIV; r.KeySize = 256; r.Key = pbTestKey; r.Mode = CipherMode.ECB; ICryptoTransform iCrypt = r.CreateEncryptor(); iCrypt.TransformBlock(pbTestData, 0, 16, pbTestData, 0); #endif if(!MemUtil.ArraysEqual(pbTestData, pbReferenceCT)) throw new SecurityException(KLRes.EncAlgorithmAes + "."); }
private IBufferedCipher createCipher(bool forEncryption) { // IBufferedCipher cipher = CipherUtilities.GetCipher("AES/CFB/NoPadding"); IBlockCipher blockCipher = new AesEngine(); int bits = 8 * blockCipher.GetBlockSize(); // TODO Is this right? blockCipher = new CfbBlockCipher(blockCipher, bits); IBufferedCipher cipher = new BufferedBlockCipher(blockCipher); // SecureRandom random = new SecureRandom(); byte[] keyBytes = new byte[32]; //random.NextBytes(keyBytes); KeyParameter key = new KeyParameter(keyBytes); byte[] iv = new byte[cipher.GetBlockSize()]; //random.NextBytes(iv); cipher.Init(forEncryption, new ParametersWithIV(key, iv)); return cipher; }