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 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 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 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(); } } }
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."); }
static byte[] Decrypt(byte[] encrypted, string key) { using (SymmetricAlgorithm salsa20 = new Salsa20()) { var dataKey = new byte[8]; var keyBytes = Encoding.UTF8.GetBytes(key); byte[] decrypted = new byte[encrypted.Length]; using (var decrypt = salsa20.CreateDecryptor(keyBytes, dataKey)) decrypt.TransformBlock(encrypted, 0, encrypted.Length, decrypted, 0); return(decrypted); } }
public static byte[] DecryptFile(string name, byte[] data, string decryptionKeyName) { byte[] key = new byte[16]; using (BinaryReader reader = new BinaryReader(new FileStream(decryptionKeyName + ".ak", FileMode.Open))) { key = reader.ReadBytes(16); } byte[] IV = name.ToByteArray(); Array.Copy(IV, 8, IV, 0, 8); Array.Resize(ref IV, 8); using (var salsa = new Salsa20()) { var decryptor = salsa.CreateDecryptor(key, IV); return(decryptor.TransformFinalBlock(data, 0, data.Length)); } }
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); } }