public void TestBlowfish() { // arrange using (var csp = new BlowfishCryptoProvider()) { var random = new Random(42); var keySizes = csp.ValidKeySizes; foreach (var sz in keySizes) { csp.KeySize = sz; var key = new byte[sz / 8]; GenerateKey(key); var data = new byte[(int)(sz / 8 * 1.5)]; random.NextBytes(data); var msSrc = new MemoryStream(); var mswSrc = msSrc.AsUnclosable(); var msDst = new MemoryStream(); var mswDst = msDst.AsUnclosable(); mswSrc.Write(data, 0, data.Length); mswSrc.Position = 0; // act csp.Key = key; csp.Encrypt(mswDst, mswSrc, (int)mswSrc.Length); mswSrc.Position = 0; mswDst.Position = 0; csp.Decrypt(mswSrc, mswDst, (int)mswDst.Length); var decryptedData = msSrc.GetBuffer(); // assert for (int i = 0; i < msSrc.Length; i++) { Assert.AreEqual(data[i], decryptedData[i]); } } } }
public void TestBlowfishDefaultKey() { // arrange using (var csp = new BlowfishCryptoProvider()) { var random = new Random(42); var data = new byte[(int)(csp.KeySize * 1.5)]; random.NextBytes(data); var msSrc = new MemoryStream(); var mswSrc = msSrc.AsUnclosable(); var msDst = new MemoryStream(); var mswDst = msDst.AsUnclosable(); mswSrc.Write(data, 0, data.Length); mswSrc.Position = 0; // act csp.Encrypt(mswDst, mswSrc, (int)mswSrc.Length); mswSrc.Position = 0; mswDst.Position = 0; csp.Decrypt(mswSrc, mswDst, (int)mswDst.Length); var decryptedData = msSrc.GetBuffer(); // assert for (int i = 0; i < msSrc.Length; i++) { Assert.AreEqual(data[i], decryptedData[i]); } } }