/// <summary> /// PinPadCryptographyMethodController string parser. /// </summary> /// <param name="reader">String reader.</param> /// <returns>PinPadCryptographyMethodController</returns> public static CryptographyMethod CustomStringParser(StringReader reader) { int intValue = StringParser.IntegerStringParser(reader, 1).Value; int keyManagementMethodValue = (intValue / 2) + 1; KeyManagementMode keyManagementMethod = (KeyManagementMode)keyManagementMethodValue; int cryptographyMethodValue = (intValue % 2) + 1; CryptographyMode cryptographyMethod = (CryptographyMode)cryptographyMethodValue; CryptographyMethod value = new CryptographyMethod(keyManagementMethod, cryptographyMethod); return(value); }
public static string EncryptOrPadding(this string toEncrypt, CryptographyMode cryptographyMode) { byte[] keyArray = null; byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt); System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader(); // Get the key from config file string key = (string)settingsReader.GetValue("HashKey", typeof(String)); //If hashing use get hashcode regards to your key if (CryptographyMode.Md5 == cryptographyMode) { using (MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider()) { keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key)); //Always release the resources and flush data // of the Cryptographic service provide. Best Practice hashmd5.Clear(); } } else if (CryptographyMode.Padding == cryptographyMode) { keyArray = UTF8Encoding.UTF8.GetBytes(key); } using (TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider()) { //set the secret key for the tripleDES algorithm tdes.Key = keyArray; //mode of operation. there are other 4 modes. //We choose ECB(Electronic code Book) tdes.Mode = CipherMode.ECB; //padding mode(if any extra byte added) tdes.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = tdes.CreateEncryptor(); //transform the specified region of bytes array to resultArray byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); //Release resources held by TripleDes Encryptor tdes.Clear(); //Return the encrypted data into unreadable string format return(Convert.ToBase64String(resultArray, 0, resultArray.Length)); } }
public static string DecryptMd5Padding(this string cipherString, CryptographyMode cryptographyMode) { byte[] keyArray = null; //get the byte code of the string byte[] toEncryptArray = Convert.FromBase64String(cipherString); System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader(); //Get your key from config file to open the lock! string key = (string)settingsReader.GetValue("HashKey", typeof(String)); if (CryptographyMode.Md5 == cryptographyMode) { //if hashing was used get the hash code with regards to your key using (MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider()) { keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key)); //release any resource held by the MD5CryptoServiceProvider hashmd5.Clear(); } } else if (CryptographyMode.Padding == cryptographyMode) { //if hashing was not implemented get the byte code of the key keyArray = UTF8Encoding.UTF8.GetBytes(key); } using (TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider()) { //set the secret key for the tripleDES algorithm tdes.Key = keyArray; //mode of operation. there are other 4 modes. //We choose ECB(Electronic code Book) tdes.Mode = CipherMode.ECB; //padding mode(if any extra byte added) tdes.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = tdes.CreateDecryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); //Release resources held by TripleDes Encryptor tdes.Clear(); //return the Clear decrypted TEXT return(UTF8Encoding.UTF8.GetString(resultArray)); } }
/// <summary> /// Gets the PinBlock and KeySerialNumber of a card using DUKPT mode /// </summary> /// <param name="cryptographyMode">Cryptography Mode</param> /// <param name="keyIndex">Pinpad Key Index</param> /// <param name="pan">Card Pan</param> /// <param name="pinMinLength">Card Pin Minimum length</param> /// <param name="pinMaxLength">Card Pin Maximum length</param> /// <param name="message">Pin Entry Message</param> /// <param name="pinBlock">Retrieved pin block or null on failure</param> /// <param name="ksn">Retrieved key serial number of null on failure</param> /// <returns>true on success, false on failure</returns> public bool GetDukptPin(CryptographyMode cryptographyMode, int keyIndex, string pan, int pinMinLength, int pinMaxLength, SimpleMessageProperty message, out HexadecimalData pinBlock, out HexadecimalData ksn) { // Creater GPN request: GpnRequest request = new GpnRequest(); request.GPN_METHOD.Value = new CryptographyMethod(KeyManagementMode.DerivedUniqueKeyPerTransaction, cryptographyMode); request.GPN_KEYIDX.Value = keyIndex; request.GPN_WKENC.Value = new HexadecimalData(new byte[0]); request.GPN_PAN.Value = pan; // Settings to capture PIN: GpnPinEntryRequest entry = new GpnPinEntryRequest(); entry.GPN_MIN.Value = pinMinLength; entry.GPN_MAX.Value = pinMaxLength; entry.GPN_MSG.Value = message; request.GPN_ENTRIES.Value.Add(entry); // Sends the request and gets it's response: GpnResponse response = this.Communication.SendRequestAndReceiveResponse <GpnResponse>(request); // If none response war received: if (response == null) { pinBlock = null; ksn = null; return(false); } // If a response was received: else { pinBlock = response.GPN_PINBLK.Value; ksn = response.GPN_KSN.Value; return(true); } }
/*---------------------------------------------------------------------------------------------------- * Return a copy of <TModel> with encrypted/decrypted string properties. * ----------------------------------------------------------------------------------------------------*/ private TModel ApplyCryptography(TModel model, CryptographyMode mode) { dynamic cryptedModel = Activator.CreateInstance(typeof(TModel)); foreach (var prop in model.GetType().GetProperties()) { if (prop.PropertyType == typeof(string)) { string propertyValue = prop.GetValue(model).ToString(); if (mode == CryptographyMode.Decrypt) { prop.SetValue(cryptedModel, cryptographer.Decrypt(propertyValue)); } else if (mode == CryptographyMode.Encrypt) { prop.SetValue(cryptedModel, cryptographer.Encypt(propertyValue)); } } } return(cryptedModel); }
// Public Methods /// <summary> /// Obtains the current KSN (Key Serial Number) of an index in the table. /// </summary> /// <param name="indexToSearch">KSN index.</param> /// <param name="cryptographyMode">Cryptography method.</param> /// <returns>The obtained KSN, or null if the KSN was not found.</returns> public string GetDukptSerialNumber(int indexToSearch, CryptographyMode cryptographyMode) { // Setup request: GduRequest request = new GduRequest(); request.GDU_IDX.Value = indexToSearch; request.GDU_METHOD.Value = new CryptographyMethod(KeyManagementMode.DerivedUniqueKeyPerTransaction, cryptographyMode); // Send request to the pinpad: GduResponse response = this.communication .SendRequestAndReceiveResponse <GduResponse>(request); // Verify if the command was successful: if (response != null && response.RSP_STAT.Value == AbecsResponseStatus.ST_OK) { // Return KSN (if any): return(response.GDU_KSN.Value); } // Failure: return(null); }
public string GetDukptSerialNumber(int indexToSearch, CryptographyMode cryptographyMode) { return("mocked KSN"); }
private void AddEventToCryptographyMenu(Control control) { CryptographyMode cryptographyMode = control as CryptographyMode; cryptographyMode.DESEvent += (sender, e) => { MenuItemList_UnChekedAll(); DES_Tsmi.Checked = true; WindowPanel.Controls.Clear(); WindowPanel.Controls.Add(PanelList["DES"]); }; cryptographyMode.TripleDESEvent += (sender, e) => { MenuItemList_UnChekedAll(); TripleDES_Tsmi.Checked = true; WindowPanel.Controls.Clear(); WindowPanel.Controls.Add(PanelList["TripleDES"]); }; cryptographyMode.AESEvent += (sender, e) => { MenuItemList_UnChekedAll(); AES_Tsmi.Checked = true; WindowPanel.Controls.Clear(); WindowPanel.Controls.Add(PanelList["AES"]); }; cryptographyMode.RC2Event += (sender, e) => { MenuItemList_UnChekedAll(); RC2_Tsmi.Checked = true; WindowPanel.Controls.Clear(); WindowPanel.Controls.Add(PanelList["RC2"]); }; cryptographyMode.SEEDEvent += (sender, e) => { MenuItemList_UnChekedAll(); SEED_Tsmi.Checked = true; WindowPanel.Controls.Clear(); WindowPanel.Controls.Add(PanelList["SEED"]); }; cryptographyMode.ARIAEvent += (sender, e) => { MenuItemList_UnChekedAll(); ARIA_Tsmi.Checked = true; WindowPanel.Controls.Clear(); WindowPanel.Controls.Add(PanelList["ARIA"]); }; cryptographyMode.BlowFishEvent += (sender, e) => { MenuItemList_UnChekedAll(); BlowFish_Tsmi.Checked = true; WindowPanel.Controls.Clear(); WindowPanel.Controls.Add(PanelList["BlowFish"]); }; cryptographyMode.MD5Event += (sender, e) => { MenuItemList_UnChekedAll(); MD5_Tsmi.Checked = true; WindowPanel.Controls.Clear(); WindowPanel.Controls.Add(PanelList["MD5"]); }; cryptographyMode.RIPEMD160Event += (sender, e) => { MenuItemList_UnChekedAll(); RIPEMD160_Tsmi.Checked = true; WindowPanel.Controls.Clear(); WindowPanel.Controls.Add(PanelList["RIPEMD160"]); }; cryptographyMode.SHAEvent += (sender, e) => { MenuItemList_UnChekedAll(); SHA_Tsmi.Checked = true; WindowPanel.Controls.Clear(); WindowPanel.Controls.Add(PanelList["SHA"]); }; cryptographyMode.RSAEvent += (sender, e) => { MenuItemList_UnChekedAll(); RSA_Tsmi.Checked = true; WindowPanel.Controls.Clear(); WindowPanel.Controls.Add(PanelList["RSA"]); }; cryptographyMode.ECDSAEvent += (sender, e) => { MenuItemList_UnChekedAll(); ECDSA_Tsmi.Checked = true; WindowPanel.Controls.Clear(); WindowPanel.Controls.Add(PanelList["ECDSA"]); }; cryptographyMode.SteganographyEvent += (sender, e) => { MenuItemList_UnChekedAll(); Steganography_Tsmi.Checked = true; WindowPanel.Controls.Clear(); WindowPanel.Controls.Add(PanelList["Steganography"]); }; }
/// <summary> /// Constructor with initial value. /// </summary> /// <param name="keyManagement">Online Pin Cryptography Key Management Mode.</param> /// <param name="cryptography">Online Pin Cryptography Mode.</param> public CryptographyMethod(KeyManagementMode keyManagement, CryptographyMode cryptography) : this() { this.KeyManagement = keyManagement; this.Cryptography = cryptography; }