private void btnDecrypt_Click(object sender, EventArgs e) { txtException.Text = null; txtDecrypted.Text = null; try { byte[] cipherText = QuickCrypto.StringToByteArray(txtEncryptedToken.Text.Substring(32)); byte[] ivBytes = QuickCrypto.StringToByteArray(txtEncryptedToken.Text.Substring(0, 32)); string plaintext = null; using (Aes aesAlg = Aes.Create()) { aesAlg.Key = encryptionKey; aesAlg.IV = ivBytes; aesAlg.Padding = PaddingMode.None; ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); using (MemoryStream msDecrypt = new MemoryStream(cipherText)) using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) using (StreamReader srDecrypt = new StreamReader(csDecrypt)) { plaintext = srDecrypt.ReadToEnd(); } } for (int i = 0; i < plaintext.Length; i++) { char c = plaintext[i]; if ((int)c < 32 || (int)c > 126) { txtDecrypted.Text += @"\" + (int)c; } else { txtDecrypted.Text += c; } } } catch (Exception ex) { txtException.Text = ex.ToString(); } }
public TransactionResult Get(string ticket) { if (String.IsNullOrEmpty(ticket)) { return(new TransactionResult()); } var encryptionKeyString = "4f256766a4075027695bd48b444e1ac5c7c17385a1b4f1779e6385da24d97997"; byte[] encryptionKey = QuickCrypto.StringToByteArray(encryptionKeyString); byte[] ticketBytes = QuickCrypto.StringToByteArray(ticket.Substring(32)); byte[] ivBytes = QuickCrypto.StringToByteArray(ticket.Substring(0, 32)); string decrypted; try { decrypted = QuickCrypto.DecryptStringFromBytes_Aes( ticketBytes, encryptionKey, ivBytes); } catch { return(new TransactionResult() { Status = "Bad Token" }); } long accountNumber = 0; string[] ticketParts = decrypted.Split('|'); // some fake validation bool validAccount = Int64.TryParse(ticketParts[0], out accountNumber); if (ticketParts.Length == 4 && validAccount) { return(new TransactionResult() { Status = "OK", ConfirmationNumber = 123 }); } return(new TransactionResult() { Status = "Invalid Request" }); }
public Form1() { InitializeComponent(); encryptionKey = QuickCrypto.StringToByteArray(this.encryptionKeyString); }