public void TestHexEncodeDecode() { String data = "*****@*****.**"; String encodeData = CryptUtil.Encode(Encoding.UTF8.GetBytes(data), MasterCard.Core.Security.Fle.DataEncoding.HEX); byte[] decodeDataBytes = CryptUtil.Decode(encodeData, MasterCard.Core.Security.Fle.DataEncoding.HEX); String dataOut = System.Text.Encoding.UTF8.GetString(decodeDataBytes); Assert.AreEqual(data, dataOut); }
public IDictionary <String, Object> Decrypt(IDictionary <String, Object> map) { SmartMap smartMap = new SmartMap(map); foreach (String fieldToDecrypt in configuration.FieldsToDecrypt) { if (smartMap.ContainsKey(fieldToDecrypt)) { String baseKey = ""; if (fieldToDecrypt.IndexOf(".") > 0) { baseKey = fieldToDecrypt.Substring(0, fieldToDecrypt.LastIndexOf(".")); baseKey += "."; } //need to read the key String encryptedKey = (String)smartMap.Get(baseKey + configuration.EncryptedKeyFiledName); smartMap.Remove(baseKey + configuration.EncryptedKeyFiledName); byte[] encryptedKeyByteArray = CryptUtil.Decode(encryptedKey, configuration.DataEncoding); //need to decryt with RSA byte[] secretKeyBytes = null; if (smartMap.ContainsKey(baseKey + configuration.OaepHashingAlgorithmFieldName)) { string oaepHashingAlgorithm = (String)smartMap.Get(baseKey + configuration.OaepHashingAlgorithmFieldName); oaepHashingAlgorithm = oaepHashingAlgorithm.Replace("SHA", "SHA-"); RSAEncryptionPadding customEncryptionPadding = configuration.OaepEncryptionPadding; if (oaepHashingAlgorithm.Equals("SHA-256")) { customEncryptionPadding = RSAEncryptionPadding.OaepSHA256; } else if (oaepHashingAlgorithm.Equals("SHA-512")) { customEncryptionPadding = RSAEncryptionPadding.OaepSHA512; } secretKeyBytes = CryptUtil.DecryptRSA(encryptedKeyByteArray, this.privateKey, customEncryptionPadding); } else { secretKeyBytes = CryptUtil.DecryptRSA(encryptedKeyByteArray, this.privateKey, configuration.OaepEncryptionPadding); } //need to read the iv String ivString = (String)smartMap.Get(baseKey + configuration.IvFieldName); smartMap.Remove(baseKey + configuration.IvFieldName); byte[] ivByteArray = CryptUtil.Decode(ivString.ToString(), configuration.DataEncoding); // remove the field that are not required in the map if (smartMap.ContainsKey(configuration.PublicKeyFingerprintFiledName)) { smartMap.Remove(configuration.PublicKeyFingerprintFiledName); } //need to decrypt the data String encryptedData = (String)smartMap.Get(baseKey + configuration.EncryptedDataFieldName); smartMap.Remove(baseKey + configuration.EncryptedDataFieldName); byte[] encryptedDataByteArray = CryptUtil.Decode(encryptedData, configuration.DataEncoding); byte[] decryptedDataByteArray = CryptUtil.DecryptAES(ivByteArray, secretKeyBytes, encryptedDataByteArray, configuration.SymmetricKeysize, configuration.SymmetricMode, configuration.SymmetricPadding); String decryptedDataString = System.Text.Encoding.UTF8.GetString(decryptedDataByteArray); if (decryptedDataString.StartsWith("{")) { Dictionary <String, Object> decryptedDataMap = JsonConvert.DeserializeObject <Dictionary <String, Object> >(decryptedDataString); foreach (KeyValuePair <String, Object> entry in decryptedDataMap) { smartMap.Add(baseKey + configuration.EncryptedDataFieldName + "." + entry.Key, entry.Value); } } else { smartMap.Add(baseKey + configuration.EncryptedDataFieldName, decryptedDataString); } break; } } return(smartMap); }