I was hoping someone could help a non-PHP developer with an example of how to decrypt a Triple DES URL Encoded string (e.g. passed to the page in a querystring variable named “q”).
The key is 24 bit and the intialization vector is 8 bit.
In C# the original data is:
- UTF-8 encoded as a byte array
- Encrypted with the key and IV
- Encoded as a Base 64 string
- URL encoded as a string
…
byte[] rawData;
TripleDES tripleDESalg = TripleDES.Create();
rawData = UTF8Encoding.UTF8.GetBytes(message);
ICryptoTransform cTransform = tripleDESalg.CreateEncryptor(key, IV);
byte[] resultArray = cTransform.TransformFinalBlock(rawData, 0, rawData.Length);
tripleDESalg.Clear();
encryptedText = Convert.ToBase64String(resultArray);
…
In a PHP page, I can’t really find examples on how to these steps together:
- Decode the URL encoded string in the querystring variable “q”
- Decode the Base 64 encoded string (e.g. base64_decode)
- Decrypt the value using the same key and iv that was used to encrypt it (assume you control the encrypting and decrypting and both system know what the key and iv are)
- Print/echo the output
Get value from query string
$string = $_REQUEST['q'];URL Decode
urldecodeBase 64 Decode
base64_decodeDecryption
Use
mcrypt_decrypt.For the first parameter, you pass in the cipher (List of Ciphers), which, in your case, would be
MCRYPT_DES.The remaining parameters would be the ciphertext, key, iv etc.
The PHP manual is exhaustive. Use it.