Find your content:

Search form

You are here

AMPscript Encrypt with PHP Decrypt?

 
Share

I am trying to understand how AMPscript EncryptSymmetric or DecryptSymmetric might work with openssl or mcrypt in PHP.

Essentially - is there a way to use AMPscript to encrypt, and PHP to decrypt? It seems like no matter what I try, it's just not working on one side or another. I'm able to encrypt/decrypt on AMPscript or on PHP, but can't seem to cross over.


Attribution to: Kelly J Andrews

Possible Suggestion/Solution #1

The Encrypt and Decrypt functions in ExactTarget use different methods to encrypt. So encrypting with AES can't be decrypted with AES in PHP.

Actually I remember doing this a while ago. I managed to recreate the DES ECB PKCS7 Algorithm in C#.

Basically what needs to be done is to convert the plain text password to Hex

Base64Hex.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace base64Hex
{
    class Program
    {
        static string key = "THISISTHEKEY";
        static byte[] bytekey;
        static void Main(string[] args)
        {
            //bytekey = Convert.FromBase64String(key);
            bytekey = Encoding.UTF8.GetBytes(key);
            key = BitConverter.ToString(bytekey).Replace("-", string.Empty);
            Console.WriteLine("0x" + key);
            Console.ReadLine();
        }
    }
}

DESTest.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Diagnostics;

namespace DESTest
{
    class Program
    {

        static string encKey = "KEY GENERATED FROM ABOVE";
        static byte[] dec;
        static string url = "";


        /// <summary>
        /// Output encrypted URL
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {

            var encryptedCode = Encrypt("TextToBeEncrypted");
            var decryptedCode = Decrypt(encryptedCode);

        }

        /// <summary>
        /// Decrypt an encrypted string.
        /// </summary>
        /// <param name="encryptedString"></param>
        /// <returns></returns>
        public static string Decrypt(string encryptedString)
        {
            DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider();
            desProvider.Mode = CipherMode.ECB;
            desProvider.Padding = PaddingMode.PKCS7;
            dec = Convert.FromBase64String(encKey);
            desProvider.Key = dec;
            using (MemoryStream stream = new MemoryStream(Convert.FromBase64String(encryptedString)))
            {
                using (CryptoStream cs = new CryptoStream(stream, desProvider.CreateDecryptor(), CryptoStreamMode.Read))
                {
                    using (StreamReader sr = new StreamReader(cs, Encoding.UTF8))
                    {
                        return Convert.FromBase64String(sr.ReadToEnd()).ToString();
                    }
                }
            }
        }



        /// <summary>
        /// Encrypt a string with DES ECB -> Base64Encoded
        /// </summary>
        /// <param name="stringtodecrypt"></param>
        /// <returns></returns>
        public static string Encrypt(string stringtodecrypt)
        {
            DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider();
            desProvider.Mode = CipherMode.ECB;
            desProvider.Padding = PaddingMode.PKCS7;
            dec = Convert.FromBase64String(encKey);
            desProvider.Key = dec;


            using (MemoryStream stream = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(stream, desProvider.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    byte[] data = Encoding.UTF8.GetBytes(stringtodecrypt);
                    cs.Write(data, 0, data.Length);
                    cs.FlushFinalBlock();
                    return Convert.ToBase64String(stream.ToArray());
                }

            }
        }
    }
}

Attribution to: Amtera
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/31339

My Block Status

My Block Content