Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

AMPscript Encrypt with PHP Decrypt?

General Tech Bugs & Fixes

Max. 2000 characters
Replies

usr_profile.png

User

( 5 months ago )

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.

usr_profile.png

User

( 5 months ago )

 

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());

what's your interest


forum_ban8_5d8c5fd7cf6f7.gif