mirror of
https://github.com/wismna/ModernKeePass.git
synced 2025-10-03 15:40:18 -04:00
Code cleanup
This commit is contained in:
@@ -13,66 +13,62 @@ namespace ModernKeePassLib.Test.Cryptography.Cipher
|
|||||||
public class StandardAesEngineTests
|
public class StandardAesEngineTests
|
||||||
{
|
{
|
||||||
// Test vector (official ECB test vector #356)
|
// Test vector (official ECB test vector #356)
|
||||||
private byte[] pbReferenceCT = new byte[16]
|
private readonly byte[] _pbReferenceCt =
|
||||||
{
|
{
|
||||||
0x75, 0xD1, 0x1B, 0x0E, 0x3A, 0x68, 0xC4, 0x22,
|
0x75, 0xD1, 0x1B, 0x0E, 0x3A, 0x68, 0xC4, 0x22,
|
||||||
0x3D, 0x88, 0xDB, 0xF0, 0x17, 0x97, 0x7D, 0xD7
|
0x3D, 0x88, 0xDB, 0xF0, 0x17, 0x97, 0x7D, 0xD7
|
||||||
};
|
};
|
||||||
|
private readonly byte[] _pbIv = new byte[16];
|
||||||
|
private readonly byte[] _pbTestKey = new byte[32];
|
||||||
|
private readonly byte[] _pbTestData =
|
||||||
|
{
|
||||||
|
0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||||
|
};
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void TestEncryptStream()
|
public void TestEncryptStream()
|
||||||
{
|
{
|
||||||
byte[] pbIV = new byte[16];
|
using (var outStream = new MemoryStream(new byte[16]))
|
||||||
byte[] pbTestKey = new byte[32];
|
{
|
||||||
byte[] pbTestData = new byte[16];
|
var aes = new StandardAesEngine();
|
||||||
pbTestData[0] = 0x04;
|
using (var inStream = aes.EncryptStream(outStream, _pbTestKey, _pbIv))
|
||||||
|
{
|
||||||
var outStream = new MemoryStream(new byte[16]);
|
new BinaryWriter(inStream).Write(_pbTestData);
|
||||||
var aes = new StandardAesEngine();
|
Assert.AreEqual(16, outStream.Position);
|
||||||
var inStream = aes.EncryptStream(outStream, pbTestKey, pbIV);
|
outStream.Position = 0;
|
||||||
new BinaryWriter(inStream).Write(pbTestData);
|
var outBytes = new BinaryReaderEx(outStream, Encoding.UTF8, string.Empty).ReadBytes(16);
|
||||||
Assert.AreEqual(16, outStream.Position);
|
Assert.IsTrue(MemUtil.ArraysEqual(outBytes, _pbReferenceCt));
|
||||||
outStream.Position = 0;
|
}
|
||||||
var outBytes = new BinaryReaderEx(outStream, Encoding.UTF8, string.Empty).ReadBytes(16);
|
}
|
||||||
Assert.IsTrue(MemUtil.ArraysEqual(outBytes, pbReferenceCT));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void TestDecryptStream()
|
public void TestDecryptStream()
|
||||||
{
|
{
|
||||||
byte[] pbIV = new byte[16];
|
|
||||||
byte[] pbTestKey = new byte[32];
|
|
||||||
byte[] pbTestData = new byte[16];
|
|
||||||
pbTestData[0] = 0x04;
|
|
||||||
|
|
||||||
// Possible Mono Bug? This only works with size >= 48
|
// Possible Mono Bug? This only works with size >= 48
|
||||||
var inStream = new MemoryStream(new byte[32]);
|
using (var inStream = new MemoryStream(new byte[32]))
|
||||||
inStream.Write(pbReferenceCT, 0, pbReferenceCT.Length);
|
{
|
||||||
inStream.Position = 0;
|
inStream.Write(_pbReferenceCt, 0, _pbReferenceCt.Length);
|
||||||
var aes = new StandardAesEngine();
|
inStream.Position = 0;
|
||||||
var outStream = aes.DecryptStream(inStream, pbTestKey, pbIV);
|
var aes = new StandardAesEngine();
|
||||||
var outBytes = new BinaryReaderEx(outStream, Encoding.UTF8, string.Empty).ReadBytes(16);
|
using (var outStream = aes.DecryptStream(inStream, _pbTestKey, _pbIv))
|
||||||
Assert.IsTrue(MemUtil.ArraysEqual(outBytes, pbTestData));
|
{
|
||||||
|
var outBytes = new BinaryReaderEx(outStream, Encoding.UTF8, string.Empty).ReadBytes(16);
|
||||||
|
Assert.IsTrue(MemUtil.ArraysEqual(outBytes, _pbTestData));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void TestBouncyCastleAes()
|
public void TestBouncyCastleAes()
|
||||||
{
|
{
|
||||||
byte[] pbIV = new byte[16];
|
|
||||||
byte[] pbTestKey = new byte[32];
|
|
||||||
byte[] pbTestData = new byte[16];
|
|
||||||
/*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;
|
|
||||||
|
|
||||||
var aesEngine = new AesEngine();
|
var aesEngine = new AesEngine();
|
||||||
//var parametersWithIv = new ParametersWithIV(new KeyParameter(pbTestKey), pbIV);
|
//var parametersWithIv = new ParametersWithIV(new KeyParameter(pbTestKey), pbIV);
|
||||||
aesEngine.Init(true, new KeyParameter(pbTestKey));
|
aesEngine.Init(true, new KeyParameter(_pbTestKey));
|
||||||
Assert.AreEqual(aesEngine.GetBlockSize(), pbTestData.Length);
|
Assert.AreEqual(aesEngine.GetBlockSize(), _pbTestData.Length);
|
||||||
aesEngine.ProcessBlock(pbTestData, 0, pbTestData, 0);
|
aesEngine.ProcessBlock(_pbTestData, 0, _pbTestData, 0);
|
||||||
Assert.IsTrue(MemUtil.ArraysEqual(pbTestData, pbReferenceCT));
|
Assert.IsTrue(MemUtil.ArraysEqual(_pbTestData, _pbReferenceCt));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user