Exemple #1
0
 public void CreateEncryptorInvalid()
 {
     using (SymmetricAlgorithm salsa20 = new Salsa20())
     {
         Assert.Throws <ArgumentNullException>(() => salsa20.CreateEncryptor(null, new byte[8]));
         Assert.Throws <ArgumentNullException>(() => salsa20.CreateEncryptor(new byte[16], null));
         Assert.Throws <CryptographicException>(() => salsa20.CreateEncryptor(new byte[15], new byte[8]));
         Assert.Throws <CryptographicException>(() => salsa20.CreateEncryptor(new byte[16], new byte[7]));
     }
 }
 public void CreateEncryptorInvalid()
 {
     using (SymmetricAlgorithm salsa20 = new Salsa20())
     {
         Assert.Throws<ArgumentNullException>(() => salsa20.CreateEncryptor(null, new byte[8]));
         Assert.Throws<ArgumentNullException>(() => salsa20.CreateEncryptor(new byte[16], null));
         Assert.Throws<CryptographicException>(() => salsa20.CreateEncryptor(new byte[15], new byte[8]));
         Assert.Throws<CryptographicException>(() => salsa20.CreateEncryptor(new byte[16], new byte[7]));
     }
 }
Exemple #3
0
        public void Load(string filePath = null)
        {
            if (cryptPass == null && (Program.settings.EncryptAccounts || Program.settings.DecryptAccounts))
            {
                Forms.CryptPassForm form = new Forms.CryptPassForm();
                form.ShowDialog();
                cryptPass = form.Password;
            }

            if (filePath == null && this.filePath != null)
            {
                filePath = this.filePath;
            }

            if (!Program.settings.DecryptAccounts)
            {
                try
                {
                    string text = File.ReadAllText(filePath);
                    accounts = JsonConvert.DeserializeObject <List <Account> >(text);
                }
                catch (FileNotFoundException e)
                {
                    // silent
                    File.WriteAllText(e.FileName, "[]");
                    accounts.Clear();
                }
            }
            else
            {
                try
                {
                    byte[] textBytes  = File.ReadAllBytes(filePath);
                    byte[] cryptBytes = new byte[textBytes.Length];
                    using (var decrypt = crypt.CreateDecryptor(cryptPass, salsaIV))
                        decrypt.TransformBlock(textBytes, 0, textBytes.Length, cryptBytes, 0);
                    string rawJson = Encoding.UTF8.GetString(cryptBytes);
                    if (!rawJson.StartsWith("SHIT"))
                    {
                        System.Windows.Forms.MessageBox.Show("Doesn't look like the inputted password is it bud.\n Restart launcher and try again.", "GW Launcher - Invalid Password");
                    }
                    accounts = JsonConvert.DeserializeObject <List <Account> >(rawJson.Substring(4));
                }
                catch (FileNotFoundException)
                {
                    // silent
                    byte[] bytes      = Encoding.UTF8.GetBytes("SHIT[]");
                    var    cryptBytes = new byte[bytes.Length];
                    using (var encrypt = crypt.CreateEncryptor(cryptPass, salsaIV))
                        encrypt.TransformBlock(bytes, 0, bytes.Length, cryptBytes, 0);
                    File.WriteAllBytes(filePath, cryptBytes);
                    accounts.Clear();
                }
            }
        }
Exemple #4
0
        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);
            }
        }
        private byte[] DoSalsa20(byte[] iv, byte[] key, byte[] data)
        {
            Salsa20 s = new Salsa20();

            ICryptoTransform enc = s.CreateEncryptor(key, iv);

            byte[] result = new byte[32];

            enc.TransformBlock(data, 0, data.Length, result, 0);

            return(result);
        }
        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);
            }
        }
 public void GenerateRandom()
 {
     byte[] rng = new byte[Program.RNG_COUNT * Program.INT_SIZE];
     using (SymmetricAlgorithm salsa20 = new Salsa20())
         using (ICryptoTransform encrypt = salsa20.CreateEncryptor(Guid.NewGuid().ToByteArray(),
                                                                   Guid.NewGuid().ToByteArray().Take(8).ToArray()))
             using (MemoryStream streamInput = new MemoryStream(rng, false))
                 using (CryptoStream streamEncrypted = new CryptoStream(streamInput, encrypt, CryptoStreamMode.Read))
                 {
                     streamEncrypted.ReadAsync(rng).GetAwaiter().GetResult();
                 }
 }
Exemple #8
0
        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);
                }
        }
        public static void Crypt(CryptVerbs options)
        {
            if (!File.Exists(options.InputPath))
            {
                Console.WriteLine("[X] File to encrypt does not exist.");
                return;
            }

            byte[] input = File.ReadAllBytes(options.InputPath);
            if (!string.IsNullOrEmpty(options.Salsa20KeyEncrypt))
            {
                byte[] keyBytes = MiscUtils.StringToByteArray(options.Salsa20KeyEncrypt);
                using SymmetricAlgorithm salsa20 = new Salsa20();
                byte[] dataKey = new byte[8];

                Console.WriteLine("[:] Encrypting..");
                using var decrypt = salsa20.CreateDecryptor(keyBytes, dataKey);
                decrypt.TransformBlock(input, 0, input.Length, input, 0);
            }
            else if (!string.IsNullOrEmpty(options.Salsa20KeyDecrypt))
            {
                byte[] keyBytes = MiscUtils.StringToByteArray(options.Salsa20KeyDecrypt);
                using SymmetricAlgorithm salsa20 = new Salsa20();
                byte[] dataKey = new byte[8];

                Console.WriteLine("[:] Decrypting..");
                using var encrypt = salsa20.CreateEncryptor(keyBytes, dataKey);
                encrypt.TransformBlock(input, 0, input.Length, input, 0);
            }
            else
            {
                Keyset[] keysets = CheckKeys();
                if (keysets is null)
                {
                    return;
                }

                Keyset keys = keysets.Where(e => e.GameCode.Equals(options.GameCode, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
                if (keys is null)
                {
                    Console.WriteLine($"Keyset with GameCode '{options.GameCode}' does not exist in the keyset file.");
                    return;
                }

                Console.WriteLine("[!] Crypting..");
                keys.CryptData(input, 0);
            }

            Console.WriteLine($"[:] Saving file as {options.OutputPath}..");
            File.WriteAllBytes(options.OutputPath, input);
            Console.WriteLine("[/] Done.");
        }
Exemple #10
0
        static byte[] Encrypt(byte[] input, string key)
        {
            using (SymmetricAlgorithm salsa20 = new Salsa20())
            {
                var dataKey  = new byte[8];
                var keyBytes = Encoding.UTF8.GetBytes(key);

                byte[] encrypted = new byte[input.Length];
                using (var encrypt = salsa20.CreateEncryptor(keyBytes, dataKey))
                    encrypt.TransformBlock(input, 0, input.Length, encrypted, 0);

                return(encrypted);
            }
        }
        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();
            }
        }
Exemple #12
0
        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();
                }
        }
Exemple #13
0
        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));
                }
        }
Exemple #14
0
        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());
        }
Exemple #15
0
        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 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 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 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 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());
        }