public static byte[] EncryptSalsa20(byte[] input, string key) { byte[] initKey = SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(key)); byte[] initVec = SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(key).Reverse().ToArray()); initKey = initKey.Crop(32); initVec = initVec.Crop(8); var enc = new Salsa20().CreateEncryptor(initKey, initVec); byte[] output = enc.TransformFinalBlock(input, 0, input.Length); return output; }
public void IV() { using (SymmetricAlgorithm salsa20 = new Salsa20()) { Assert.Throws<ArgumentNullException>(() => salsa20.IV = null); Assert.Throws<CryptographicException>(() => salsa20.IV = new byte[9]); byte[] iv = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }; salsa20.IV = iv; CollectionAssert.AreEqual(iv, salsa20.IV); } }
public void Rounds() { using (Salsa20 salsa20 = new Salsa20()) { Assert.AreEqual(20, salsa20.Rounds); salsa20.Rounds = 12; Assert.AreEqual(12, salsa20.Rounds); salsa20.Rounds = 8; Assert.AreEqual(8, salsa20.Rounds); Assert.Throws<ArgumentOutOfRangeException>(() => salsa20.Rounds = 16); } }
public void BlockSize() { using (SymmetricAlgorithm salsa20 = new Salsa20()) { Assert.AreEqual(512, salsa20.BlockSize); KeySizes[] sizes = salsa20.LegalBlockSizes; Assert.AreEqual(1, sizes.Length); Assert.AreEqual(512, sizes[0].MinSize); Assert.AreEqual(512, sizes[0].MaxSize); Assert.AreEqual(0, sizes[0].SkipSize); Assert.Throws<CryptographicException>(() => salsa20.BlockSize = 128); } }
public void Key() { using (SymmetricAlgorithm salsa20 = new Salsa20()) { KeySizes[] sizes = salsa20.LegalKeySizes; Assert.AreEqual(1, sizes.Length); Assert.AreEqual(128, sizes[0].MinSize); Assert.AreEqual(256, sizes[0].MaxSize); Assert.AreEqual(128, sizes[0].SkipSize); Assert.Throws<ArgumentNullException>(() => salsa20.Key = null); Assert.Throws<CryptographicException>(() => salsa20.Key = new byte[8]); byte[] key16 = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; salsa20.Key = key16; CollectionAssert.AreEqual(key16, salsa20.Key); byte[] key32 = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f }; salsa20.Key = key32; CollectionAssert.AreEqual(key32, salsa20.Key); } }
public void CreateDecryptorInvalid() { using (SymmetricAlgorithm salsa20 = new Salsa20()) { Assert.Throws<ArgumentNullException>(() => salsa20.CreateDecryptor(null, new byte[8])); Assert.Throws<ArgumentNullException>(() => salsa20.CreateDecryptor(new byte[16], null)); Assert.Throws<CryptographicException>(() => salsa20.CreateDecryptor(new byte[15], new byte[8])); Assert.Throws<CryptographicException>(() => salsa20.CreateDecryptor(new byte[16], new byte[7])); } }
public void EcryptTestVector131072(string strComment, int rounds, string key, string iv, string strOutput0_63, string strOutput65472_65535, string strOutput65536_65599, string strOutput131008_131071) { byte[] output; // use the Salsa20 transform directly using (SymmetricAlgorithm salsa20 = new Salsa20() { Rounds = rounds }) using (ICryptoTransform encrypt = salsa20.CreateEncryptor(ToBytes(key), ToBytes(iv))) { // input is all zeroes byte[] input = new byte[131072]; output = encrypt.TransformFinalBlock(input, 0, input.Length); } CollectionAssert.AreEqual(ToBytes(strOutput0_63), output.Skip(0).Take(64).ToList()); CollectionAssert.AreEqual(ToBytes(strOutput65472_65535), output.Skip(65472).Take(64).ToList()); CollectionAssert.AreEqual(ToBytes(strOutput65536_65599), output.Skip(65536).Take(64).ToList()); CollectionAssert.AreEqual(ToBytes(strOutput131008_131071), output.Skip(131008).Take(64).ToList()); }
public void EcryptTestVector512(string strComment, int rounds, string key, string iv, string strOutput0_63, string strOutput192_255, string strOutput256_319, string strOutput448_511) { const int c_nSize = 512; byte[] output = new byte[c_nSize]; // encrypt by using CryptoStream using (SymmetricAlgorithm salsa20 = new Salsa20() { Rounds = rounds }) using (ICryptoTransform encrypt = salsa20.CreateEncryptor(ToBytes(key), ToBytes(iv))) using (MemoryStream streamInput = new MemoryStream(new byte[c_nSize], false)) using (CryptoStream streamEncrypted = new CryptoStream(streamInput, encrypt, CryptoStreamMode.Read)) { streamEncrypted.Read(output, 0, output.Length); } CollectionAssert.AreEqual(ToBytes(strOutput0_63), output.Skip(0).Take(64).ToList()); CollectionAssert.AreEqual(ToBytes(strOutput192_255), output.Skip(192).Take(64).ToList()); CollectionAssert.AreEqual(ToBytes(strOutput256_319), output.Skip(256).Take(64).ToList()); CollectionAssert.AreEqual(ToBytes(strOutput448_511), output.Skip(448).Take(64).ToList()); }
public void EncryptDecrypt() { // generate data to encrypt byte[] input; using (MemoryStream stream = new MemoryStream()) using (BinaryWriter writer = new BinaryWriter(stream)) { for (short value = -1024; value <= 1023; value++) writer.Write(value); writer.Flush(); input = stream.ToArray(); } using (SymmetricAlgorithm salsa20 = new Salsa20()) { byte[] encrypted = new byte[input.Length]; using (ICryptoTransform encrypt = salsa20.CreateEncryptor()) encrypt.TransformBlock(input, 0, input.Length, encrypted, 0); byte[] decrypted = new byte[input.Length]; using (ICryptoTransform decrypt = salsa20.CreateDecryptor()) decrypt.TransformBlock(encrypted, 0, encrypted.Length, decrypted, 0); CollectionAssert.AreEqual(input, decrypted); } }
public void Dispose() { using (SymmetricAlgorithm salsa20 = new Salsa20()) using (ICryptoTransform transform = salsa20.CreateEncryptor(new byte[16], new byte[8])) { transform.Dispose(); transform.Dispose(); Assert.Throws<ObjectDisposedException>(() => transform.TransformBlock(new byte[1], 0, 1, new byte[1], 0)); Assert.Throws<ObjectDisposedException>(() => transform.TransformFinalBlock(new byte[1], 0, 1)); ((IDisposable) salsa20).Dispose(); ((IDisposable) salsa20).Dispose(); } }
public void TransformFinalBlock() { using (SymmetricAlgorithm salsa20 = new Salsa20()) using (ICryptoTransform transform = salsa20.CreateEncryptor(new byte[16], new byte[8])) { byte[] aby0 = new byte[0]; byte[] aby1 = new byte[1]; Assert.Throws<ArgumentNullException>(() => transform.TransformFinalBlock(null, 0, 0)); Assert.Throws<ArgumentOutOfRangeException>(() => transform.TransformFinalBlock(aby0, -1, 0)); Assert.Throws<ArgumentOutOfRangeException>(() => transform.TransformFinalBlock(aby0, 1, 0)); Assert.Throws<ArgumentOutOfRangeException>(() => transform.TransformFinalBlock(aby1, 0, -1)); Assert.Throws<ArgumentOutOfRangeException>(() => transform.TransformFinalBlock(aby1, 0, 2)); Assert.Throws<ArgumentOutOfRangeException>(() => transform.TransformFinalBlock(aby1, 1, 1)); } }
public void CryptoTransform() { using (SymmetricAlgorithm salsa20 = new Salsa20()) using (ICryptoTransform transform = salsa20.CreateEncryptor(new byte[16], new byte[8])) { Assert.AreEqual(salsa20.BlockSize / 8, transform.InputBlockSize); Assert.AreEqual(salsa20.BlockSize / 8, transform.OutputBlockSize); Assert.IsFalse(transform.CanReuseTransform); Assert.IsTrue(transform.CanTransformMultipleBlocks); } }