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);
        }
Example #2
0
        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);
        }