public static string TripleDesDecrypt(this string value, string password) { var passBytes = Encoding.ASCII.GetBytes(password); return (Encoding.UTF8.GetString(TripleDesCipher.TripleDesDecrypt(Base64.DecodeToArray(value), passBytes, passBytes))); }
public static string TripleDesEncryptToArray(this string value, string password, string iv) { var bytes = Encoding.UTF8.GetBytes(value); var encrypted = TripleDesCipher.TripleDesEncrypt(Encoding.UTF8.GetChars(bytes), Encoding.ASCII.GetBytes(password), Encoding.ASCII.GetBytes(iv)); return(encrypted.ToArrayString()); }
public static string TripleDesEncrypt(this string value, string password) { var passBytes = Encoding.ASCII.GetBytes(password); return (Base64.Encode(TripleDesCipher.TripleDesEncrypt(Encoding.UTF8.GetChars(Encoding.UTF8.GetBytes(value)), passBytes, passBytes))); }
[Ignore] // placeholder for actual test public void TripleDesCipherConstructorTest() { byte[] key = null; // TODO: Initialize to an appropriate value CipherMode mode = null; // TODO: Initialize to an appropriate value CipherPadding padding = null; // TODO: Initialize to an appropriate value TripleDesCipher target = new TripleDesCipher(key, mode, padding); Assert.Inconclusive("TODO: Implement code to verify target"); }
public void Test_Cipher_3DES_CBC() { var input = new byte[] { 0x00, 0x00, 0x00, 0x1c, 0x0a, 0x05, 0x00, 0x00, 0x00, 0x0c, 0x73, 0x73, 0x68, 0x2d, 0x75, 0x73, 0x65, 0x72, 0x61, 0x75, 0x74, 0x68, 0x72, 0x4e, 0x06, 0x08, 0x28, 0x2d, 0xaa, 0xe2, 0xb3, 0xd9 }; var key = new byte[] { 0x78, 0xf6, 0xc6, 0xbb, 0x57, 0x03, 0x69, 0xca, 0xba, 0x31, 0x18, 0x2f, 0x2f, 0x4c, 0x35, 0x34, 0x64, 0x06, 0x85, 0x30, 0xbe, 0x78, 0x60, 0xb3 }; var iv = new byte[] { 0xc0, 0x75, 0xf2, 0x26, 0x0a, 0x2a, 0x42, 0x96 }; var output = new byte[] { 0x28, 0x77, 0x2f, 0x07, 0x3e, 0xc2, 0x27, 0xa6, 0xdb, 0x36, 0x4d, 0xc6, 0x7a, 0x26, 0x7a, 0x38, 0xe6, 0x54, 0x0b, 0xab, 0x07, 0x87, 0xf0, 0xa4, 0x73, 0x1f, 0xde, 0xe6, 0x81, 0x1d, 0x4b, 0x4b }; var testCipher = new TripleDesCipher(key, new CbcCipherMode(iv), null); var r = testCipher.Encrypt(input); if (!r.SequenceEqual(output)) { Assert.Fail("Invalid encryption"); } }
[Ignore] // placeholder for actual test public void EncryptBlockTest() { byte[] key = null; // TODO: Initialize to an appropriate value CipherMode mode = null; // TODO: Initialize to an appropriate value CipherPadding padding = null; // TODO: Initialize to an appropriate value TripleDesCipher target = new TripleDesCipher(key, mode, padding); // TODO: Initialize to an appropriate value byte[] inputBuffer = null; // TODO: Initialize to an appropriate value int inputOffset = 0; // TODO: Initialize to an appropriate value int inputCount = 0; // TODO: Initialize to an appropriate value byte[] outputBuffer = null; // TODO: Initialize to an appropriate value int outputOffset = 0; // TODO: Initialize to an appropriate value int expected = 0; // TODO: Initialize to an appropriate value int actual; actual = target.EncryptBlock(inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset); Assert.AreEqual(expected, actual); Assert.Inconclusive("Verify the correctness of this test method."); }
private void Open(Stream privateKey, string passPhrase) { if (privateKey == null) { throw new ArgumentNullException("privateKey"); } Match privateKeyMatch; using (var sr = new StreamReader(privateKey)) { var text = sr.ReadToEnd(); privateKeyMatch = PrivateKeyRegex.Match(text); } if (!privateKeyMatch.Success) { throw new SshException("Invalid private key file."); } var keyName = privateKeyMatch.Result("${keyName}"); var cipherName = privateKeyMatch.Result("${cipherName}"); var salt = privateKeyMatch.Result("${salt}"); var data = privateKeyMatch.Result("${data}"); var binaryData = Convert.FromBase64String(data); byte[] decryptedData; if (!string.IsNullOrEmpty(cipherName) && !string.IsNullOrEmpty(salt)) { if (string.IsNullOrEmpty(passPhrase)) { throw new SshPassPhraseNullOrEmptyException("Private key is encrypted but passphrase is empty."); } var binarySalt = new byte[salt.Length / 2]; for (var i = 0; i < binarySalt.Length; i++) { binarySalt[i] = Convert.ToByte(salt.Substring(i * 2, 2), 16); } CipherInfo cipher; switch (cipherName) { case "DES-EDE3-CBC": cipher = new CipherInfo(192, (key, iv) => new TripleDesCipher(key, new CbcCipherMode(iv), new PKCS7Padding())); break; case "DES-EDE3-CFB": cipher = new CipherInfo(192, (key, iv) => new TripleDesCipher(key, new CfbCipherMode(iv), new PKCS7Padding())); break; case "DES-CBC": cipher = new CipherInfo(64, (key, iv) => new DesCipher(key, new CbcCipherMode(iv), new PKCS7Padding())); break; case "AES-128-CBC": cipher = new CipherInfo(128, (key, iv) => new AesCipher(key, new CbcCipherMode(iv), new PKCS7Padding())); break; case "AES-192-CBC": cipher = new CipherInfo(192, (key, iv) => new AesCipher(key, new CbcCipherMode(iv), new PKCS7Padding())); break; case "AES-256-CBC": cipher = new CipherInfo(256, (key, iv) => new AesCipher(key, new CbcCipherMode(iv), new PKCS7Padding())); break; default: throw new SshException(string.Format(CultureInfo.CurrentCulture, "Private key cipher \"{0}\" is not supported.", cipherName)); } decryptedData = DecryptKey(cipher, binaryData, passPhrase, binarySalt); } else { decryptedData = binaryData; } switch (keyName) { case "RSA": _key = new RsaKey(decryptedData.ToArray()); HostKey = new KeyHostAlgorithm("ssh-rsa", _key); break; case "DSA": _key = new DsaKey(decryptedData.ToArray()); HostKey = new KeyHostAlgorithm("ssh-dss", _key); break; case "SSH2 ENCRYPTED": var reader = new SshDataReader(decryptedData); var magicNumber = reader.ReadUInt32(); if (magicNumber != 0x3f6ff9eb) { throw new SshException("Invalid SSH2 private key."); } reader.ReadUInt32(); // Read total bytes length including magic number var keyType = reader.ReadString(); var ssh2CipherName = reader.ReadString(); var blobSize = (int)reader.ReadUInt32(); byte[] keyData; if (ssh2CipherName == "none") { keyData = reader.ReadBytes(blobSize); } else if (ssh2CipherName == "3des-cbc") { if (string.IsNullOrEmpty(passPhrase)) { throw new SshPassPhraseNullOrEmptyException("Private key is encrypted but passphrase is empty."); } var key = GetCipherKey(passPhrase, 192 / 8); var ssh2Сipher = new TripleDesCipher(key, new CbcCipherMode(new byte[8]), new PKCS7Padding()); keyData = ssh2Сipher.Decrypt(reader.ReadBytes(blobSize)); } else { throw new SshException(string.Format("Cipher method '{0}' is not supported.", cipherName)); } // TODO: Create two specific data types to avoid using SshDataReader class reader = new SshDataReader(keyData); var decryptedLength = reader.ReadUInt32(); if (decryptedLength > blobSize - 4) { throw new SshException("Invalid passphrase."); } if (keyType == "if-modn{sign{rsa-pkcs1-sha1},encrypt{rsa-pkcs1v2-oaep}}") { var exponent = reader.ReadBigIntWithBits(); //e var d = reader.ReadBigIntWithBits(); //d var modulus = reader.ReadBigIntWithBits(); //n var inverseQ = reader.ReadBigIntWithBits(); //u var q = reader.ReadBigIntWithBits(); //p var p = reader.ReadBigIntWithBits(); //q _key = new RsaKey(modulus, exponent, d, p, q, inverseQ); HostKey = new KeyHostAlgorithm("ssh-rsa", _key); } else if (keyType == "dl-modp{sign{dsa-nist-sha1},dh{plain}}") { var zero = reader.ReadUInt32(); if (zero != 0) { throw new SshException("Invalid private key"); } var p = reader.ReadBigIntWithBits(); var g = reader.ReadBigIntWithBits(); var q = reader.ReadBigIntWithBits(); var y = reader.ReadBigIntWithBits(); var x = reader.ReadBigIntWithBits(); _key = new DsaKey(p, q, g, y, x); HostKey = new KeyHostAlgorithm("ssh-dss", _key); } else { throw new NotSupportedException(string.Format("Key type '{0}' is not supported.", keyType)); } break; default: throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Key '{0}' is not supported.", keyName)); } }
public static string TripleDesEncrypt(this string value, byte[] password, byte[] iv) { return(TripleDesCipher.TripleDesEncrypt(value, password, iv)); }
public static string TripleDesEncrypt(this string value, string password, string iv) { return (Base64.Encode(TripleDesCipher.TripleDesEncrypt(Encoding.UTF8.GetChars(Encoding.UTF8.GetBytes(value)), Encoding.ASCII.GetBytes(password), Encoding.ASCII.GetBytes(iv)))); }
public static string TripleDesDecrypt(this string value, string password, string iv) { return(Encoding.UTF8.GetString(TripleDesCipher.TripleDesDecrypt(Base64.DecodeToArray(value), Encoding.ASCII.GetBytes(password), Encoding.ASCII.GetBytes(iv)))); }
private void Encrypt() { rtb_CipherText.TextChanged -= Rtb_CipherText_TextChanged; rtb_CipherText.Text = String.Empty; try { switch (cipherMode) { case CipherMode.Base64: rtb_CipherText.Text = rtb_PlainText.Text.Base64Encode(); break; case CipherMode.Caesar: rtb_CipherText.Text = rtb_PlainText.Text.CaesarEncrypt((int)nud_Key.Value); break; case CipherMode.Des: if (chk_ByteArrayValues.Checked) { var encryptedText = new StringBuilder(); var encryptedBytes = DesCipher.DesEncrypt(Encoding.UTF8.GetChars(Encoding.UTF8.GetBytes(rtb_PlainText.Text)), Encoding.ASCII.GetBytes(tb_Password.Text), Encoding.ASCII.GetBytes(tb_IV.Text)); foreach (var encryptedByte in encryptedBytes) { encryptedText.AppendFormat("[{0}]", encryptedByte); } rtb_CipherText.Text = encryptedText.ToString(); } else { rtb_CipherText.Text = rtb_PlainText.Text.DesEncrypt(tb_Password.Text, tb_IV.Text); } break; case CipherMode.Rotate: rtb_CipherText.Text = rtb_PlainText.Text.RotateEncrypt((int)nud_Key.Value); break; case CipherMode.TripleDes: if (chk_ByteArrayValues.Checked) { if (chk_KeyAndIVInByteArrayFormat.Checked) { } else { var encryptedText = new StringBuilder(); var encryptedBytes = TripleDesCipher.TripleDesEncrypt(Encoding.UTF8.GetChars(Encoding.UTF8.GetBytes(rtb_PlainText.Text)), Encoding.ASCII.GetBytes(tb_Password.Text), Encoding.ASCII.GetBytes(tb_IV.Text)); foreach (var encryptedByte in encryptedBytes) { encryptedText.AppendFormat("[{0}]", encryptedByte); } rtb_CipherText.Text = encryptedText.ToString(); } } else { if (chk_KeyAndIVInByteArrayFormat.Checked) { } else { rtb_CipherText.Text = rtb_PlainText.Text.TripleDesEncrypt(tb_Password.Text, tb_IV.Text); } } break; case CipherMode.Xor: rtb_CipherText.Text = rtb_PlainText.Text.XorCrypt(tb_Password.Text); break; } } catch (Exception ex) { lbl_Error.Text = ex.Message; } finally { rtb_CipherText.TextChanged += Rtb_CipherText_TextChanged; } }
private void Decrypt() { rtb_PlainText.TextChanged -= Rtb_PlainText_TextChanged; rtb_PlainText.Text = String.Empty; try { switch (cipherMode) { case CipherMode.Base64: rtb_PlainText.Text = rtb_CipherText.Text.Base64Decode(); break; case CipherMode.Caesar: rtb_PlainText.Text = rtb_CipherText.Text.CaesarDecrypt((int)nud_Key.Value); break; case CipherMode.Des: if (chk_ByteArrayValues.Checked) { var byteStrings = rtb_CipherText.Text.Replace("[", "").Split(']'); var bytes = new byte[byteStrings.Length - 1]; for (var i = 0; i < bytes.Length; i++) { bytes[i] = Convert.ToByte(byteStrings[i]); } rtb_PlainText.Text = Encoding.UTF8.GetString(DesCipher.DesDecrypt(bytes, Encoding.ASCII.GetBytes(tb_Password.Text), Encoding.ASCII.GetBytes(tb_IV.Text))); } else { rtb_PlainText.Text = rtb_CipherText.Text.DesDecrypt(tb_Password.Text, tb_IV.Text); } break; case CipherMode.Rotate: rtb_PlainText.Text = rtb_CipherText.Text.RotateDecrypt((int)nud_Key.Value); break; case CipherMode.TripleDes: if (chk_ByteArrayValues.Checked) { var bytes = rtb_CipherText.Text.StringToByteArray(); if (chk_KeyAndIVInByteArrayFormat.Checked) { var keyBytes = tb_Password.Text.StringToByteArray(); var ivBytes = tb_IV.Text.StringToByteArray(); rtb_PlainText.Text = Encoding.UTF8.GetString(TripleDesCipher.TripleDesDecrypt(bytes, keyBytes, ivBytes)); } else { rtb_PlainText.Text = Encoding.UTF8.GetString(TripleDesCipher.TripleDesDecrypt(bytes, Encoding.ASCII.GetBytes(tb_Password.Text), Encoding.ASCII.GetBytes(tb_IV.Text))); } } else { if (chk_KeyAndIVInByteArrayFormat.Checked) { var keyBytes = tb_Password.Text.StringToByteArray(); var ivBytes = tb_IV.Text.StringToByteArray(); rtb_PlainText.Text = rtb_CipherText.Text.TripleDesDecrypt(keyBytes, ivBytes); } else { rtb_PlainText.Text = rtb_CipherText.Text.TripleDesDecrypt(tb_Password.Text, tb_IV.Text); } } break; case CipherMode.Xor: rtb_PlainText.Text = rtb_CipherText.Text.XorCrypt(tb_Password.Text); break; } } catch (Exception ex) { rtb_PlainText.Text = ex.Message; } finally { rtb_PlainText.TextChanged += Rtb_PlainText_TextChanged; } }