public IDictionary <string, object> Encrypt(IDictionary <string, object> map) { if (map.ContainsKey("cardInfo")) { string text = JsonConvert.SerializeObject((IDictionary <string, object>)map["cardInfo"]); text = CryptUtil.SanitizeJson(text); Tuple <byte[], byte[], byte[]> expr_3D = CryptUtil.EncryptAES(Encoding.UTF8.GetBytes(text)); byte[] item = expr_3D.Item1; byte[] item2 = expr_3D.Item2; byte[] arg_57_0 = expr_3D.Item3; string value = CryptUtil.HexEncode(item); string value2 = CryptUtil.HexEncode(arg_57_0); string value3 = CryptUtil.HexEncode(CryptUtil.EncrytptRSA(item2, this.publicKey)); string value4 = this.publicKeyFingerPrint; Dictionary <string, object> dictionary = new Dictionary <string, object>(); dictionary.Add("publicKeyFingerprint", value4); dictionary.Add("encryptedKey", value3); dictionary.Add("oaepHashingAlgorithm", "SHA256"); dictionary.Add("iv", value); dictionary.Add("encryptedData", value2); map.Remove("cardInfo"); map.Add("cardInfo", dictionary); } return(map); }
public void TestFullEndToEndEncryptDecrypt() { string certPath = MasterCard.Core.Util.GetCurrenyAssemblyPath() + "\\Test\\certificate.p12"; X509Certificate2 cert = new X509Certificate2(certPath, "", X509KeyStorageFlags.Exportable); var publicKey = cert.GetRSAPublicKey() as RSACng; var privateKey = cert.GetRSAPrivateKey() as RSACng; String data = "*****@*****.**"; Tuple <byte[], byte[], byte[]> aesResult = CryptUtil.EncryptAES(Encoding.UTF8.GetBytes(data), 128, CipherMode.CBC, PaddingMode.PKCS7); byte[] ivBytes = aesResult.Item1; // 5) generate AES SecretKey byte[] secretKeyBytes = aesResult.Item2; // 6) encrypt payload byte[] encryptedDataBytes = aesResult.Item3; byte[] encryptedSecretKey = CryptUtil.EncrytptRSA(secretKeyBytes, publicKey, RSAEncryptionPadding.OaepSHA256); byte[] decryptedSecretKey = CryptUtil.DecryptRSA(encryptedSecretKey, privateKey, RSAEncryptionPadding.OaepSHA256); byte[] decryptedDataBytes = CryptUtil.DecryptAES(ivBytes, decryptedSecretKey, encryptedDataBytes, 128, CipherMode.CBC, PaddingMode.PKCS7); String dataOut = System.Text.Encoding.UTF8.GetString(decryptedDataBytes); Assert.AreEqual(data, dataOut); }
public void TestEncryptDecryptAES() { String data = "*****@*****.**"; Tuple <byte[], byte[], byte[]> tuple = CryptUtil.EncryptAES(System.Text.Encoding.UTF8.GetBytes(data)); byte[] decryptedData = CryptUtil.DecryptAES(tuple.Item1, tuple.Item2, tuple.Item3); String data2 = System.Text.Encoding.UTF8.GetString(decryptedData); Assert.AreEqual(data, data2); }
public Dictionary <String, Object> Encrypt(IDictionary <String, Object> map) { if (map.ContainsKey("cardInfo")) { // 1) extract the encryptedData from map IDictionary <String, Object> encryptedDataMap = (IDictionary <String, Object>)map["cardInfo"]; // 2) create json string String payload = JsonConvert.SerializeObject(encryptedDataMap); // 3) escaping the string payload = CryptUtil.SanitizeJson(payload); Tuple <byte[], byte[], byte[]> aesResult = CryptUtil.EncryptAES(System.Text.Encoding.UTF8.GetBytes(payload)); // 4) generate random iv byte[] iv = aesResult.Item1; // 5) generate AES SecretKey byte[] key = aesResult.Item2; // 6) encrypt payload byte[] encryptedData = aesResult.Item3; String hexIv = CryptUtil.HexEncode(iv); String hexEncryptedData = CryptUtil.HexEncode(encryptedData); // 7) encrypt secretKey with issuer key byte[] encryptedSecretKey = CryptUtil.EncrytptRSA(key, this.publicKey); String hexEncryptedKey = CryptUtil.HexEncode(encryptedSecretKey); String fingerprintHexString = publicKeyFingerPrint; Dictionary <String, Object> encryptedMap = new Dictionary <String, Object>(); encryptedMap.Add("publicKeyFingerprint", fingerprintHexString); encryptedMap.Add("encryptedKey", hexEncryptedKey); encryptedMap.Add("oaepHashingAlgorithm", "SHA256"); encryptedMap.Add("iv", hexIv); encryptedMap.Add("encryptedData", hexEncryptedData); map.Remove("cardInfo"); map.Add("cardInfo", encryptedMap); } return(new Dictionary <String, Object>(map)); }
public IDictionary <String, Object> Encrypt(IDictionary <String, Object> map) { //requestMap is a SmartMap it offers a easy way to do nested lookups. SmartMap smartMap = new SmartMap(map); if (this.publicKey != null) { foreach (String fieldToEncrypt in configuration.FieldsToEncrypt) { if (smartMap.ContainsKey(fieldToEncrypt)) { String payload = null; // 1) extract the encryptedData from map Object tmpObjectToEncrypt = smartMap.Get(fieldToEncrypt); smartMap.Remove(fieldToEncrypt); if (tmpObjectToEncrypt.GetType() == typeof(Dictionary <String, Object>)) { // 2) create json string payload = JsonConvert.SerializeObject(tmpObjectToEncrypt); // 3) escaping the string payload = CryptUtil.SanitizeJson(payload); } else { payload = tmpObjectToEncrypt.ToString(); } Tuple <byte[], byte[], byte[]> aesResult = CryptUtil.EncryptAES(System.Text.Encoding.UTF8.GetBytes(payload), configuration.SymmetricKeysize, configuration.SymmetricMode, configuration.SymmetricPadding); // 4) generate random iv byte[] ivBytes = aesResult.Item1; // 5) generate AES SecretKey byte[] secretKeyBytes = aesResult.Item2; // 6) encrypt payload byte[] encryptedDataBytes = aesResult.Item3; String ivValue = CryptUtil.Encode(ivBytes, configuration.DataEncoding); String encryptedDataValue = CryptUtil.Encode(encryptedDataBytes, configuration.DataEncoding); // 7) encrypt secretKey with issuer key byte[] encryptedSecretKey = CryptUtil.EncrytptRSA(secretKeyBytes, this.publicKey, configuration.OaepEncryptionPadding); String encryptedKeyValue = CryptUtil.Encode(encryptedSecretKey, configuration.DataEncoding); String fingerprintHexString = publicKeyFingerPrint; String baseKey = ""; if (fieldToEncrypt.IndexOf(".") > 0) { baseKey = fieldToEncrypt.Substring(0, fieldToEncrypt.IndexOf(".")); baseKey += "."; } if (configuration.PublicKeyFingerprintFiledName != null) { smartMap.Add(baseKey + configuration.PublicKeyFingerprintFiledName, fingerprintHexString); } if (configuration.OaepHashingAlgorithmFieldName != null) { smartMap.Add(baseKey + configuration.OaepHashingAlgorithmFieldName, configuration.OaepHashingAlgorithm); } smartMap.Add(baseKey + configuration.IvFieldName, ivValue); smartMap.Add(baseKey + configuration.EncryptedKeyFiledName, encryptedKeyValue); smartMap.Add(baseKey + configuration.EncryptedDataFieldName, encryptedDataValue); break; } } } return(smartMap); }