I am trying to send some encrypted data from a php application to be
decoded in a .Net application. Both apps encode/decode a given string
but generate different encrypted results. Anyone have any idea? Code
to follow:
php====>
<?php
// Designate string to be encrypted
$string = "This is a test";
// Encryption/decryption key
$key = pack('H*',md5(" mysecretkey"));
echo "PHP:KeySiz e:";
echo mcrypt_get_key_ size('tripledes ', 'ecb')*8;
echo "<BR>";
echo "PHP:BlockSize: ";
echo mcrypt_get_bloc k_size('tripled es', 'ecb')*8;
echo "<BR>";
// Encryption Algorithm
$cipher_alg = MCRYPT_TRIPLEDE S;
#$cipher_alg = ;
#$cipher_alg = ;
// Create the initialization vector for added security.
//$iv = mcrypt_create_i v(mcrypt_get_iv _size($cipher_a lg,
MCRYPT_MODE_ECB ), MCRYPT_RAND);
$iv = pack("H*","ec78 7813562c5be0");
// Output original string
echo "PHP:Origin al string:$string <br>";
echo "PHP:Origin al Key:". base64_encode($ key)."<br>";
echo "PHP:Encryp tion Method: tripledes <br>";
echo "PHP:IV:".base6 4_encode($iv)." <br>";
// Encrypt $string
//$encrypted_stri ng = mcrypt_encrypt( $cipher_alg, $key, $string,
MCRYPT_MODE_CBC , $iv);
$encrypted_stri ng = mcrypt_encrypt( $cipher_alg, $key, $string,
MCRYPT_MODE_CFB , $iv);
$decrypted_stri ng = mcrypt_decrypt( $cipher_alg, $key,
$encrypted_stri ng, MCRYPT_MODE_CFB , $iv);
echo "PHP:Encryp ted string
Base64:".base64 _encode($encryp ted_string)."<b r>";
echo "PHP:Decryp ted string:$decrypt ed_string";
?>
<============== =====
PHP OUTPUTPHP:
KeySize:192
PHP:BlockSize:6 4
PHP:Original string:This is a test
PHP:Original Key:NNLkJVyOa+s 8QvJN/X5tqQ==
PHP:Encryption Method: tripledes
PHP:IV:7Hh4E1Ys W+A=
PHP:Encrypted string Base64:A+9SKuoL dZaATqa+qmTipg= =
PHP:Decrypted string:This is a test
=============== ============>
C# (.NET)
using System;
using System.Security .Cryptography;
using System.Text;
namespace test
{
class TryDes{
public string EncryptTripleDE S(string Plaintext,
string key) {
TripleDESCrypto ServiceProvider DES = new
TripleDESCrypto ServiceProvider ();
MD5CryptoServic eProvider hashMD5 = new
MD5CryptoServic eProvider();
DES.Key =
hashMD5.Compute Hash(ASCIIEncod ing.ASCII.GetBy tes(key));
DES.Mode = CipherMode.CFB;
DES.IV =
Convert.FromBas e64String("7Hh4 E1YsW+A=");
DES.Padding=Pad dingMode.Zeros;
Console.WriteLi ne("");
Console.WriteLi ne("NET:KeySize :"+DES.KeySize) ;
Console.WriteLi ne("NET:BlockSi ze:"+DES.BlockS ize);
Console.WriteLi ne("NET:Origina l
String:"+Plaint ext);
Console.WriteLi ne("NET:Origina l
Key:"+Convert.T oBase64String(D ES.Key));
Console.WriteLi ne("NET:Encrypt ion
Method:TripeDES ");
//DES.Mode = CipherMode.ECB;
Console.WriteLi ne(
"NET:IV:"+Conve rt.ToBase64Stri ng(DES.IV));
ICryptoTransfor m DESEncrypt =
DES.CreateEncry ptor(DES.Key,DE S.IV);
byte[] Buffer =
ASCIIEncoding.A SCII.GetBytes(P laintext);
return
Convert.ToBase6 4String(DESEncr ypt.TransformFi nalBlock(Buffer , 0,
Buffer.Length)) ;
}
public string DecryptTripleDE S(string base64Text,
string key){
TripleDESCrypto ServiceProvider DES = new
TripleDESCrypto ServiceProvider ();
MD5CryptoServic eProvider hashMD5 = new
MD5CryptoServic eProvider();
DES.Key =
hashMD5.Compute Hash(ASCIIEncod ing.ASCII.GetBy tes(key));
DES.Mode = CipherMode.CFB;
DES.Padding=Pad dingMode.Zeros;
DES.IV = Convert.FromBas e64String("7Hh4 E1YsW+A=");
ICryptoTransfor m DESDecrypt =
DES.CreateDecry ptor(DES.Key,DE S.IV);
byte[] Buffer =
Convert.FromBas e64String(base6 4Text);
return
ASCIIEncoding.A SCII.GetString( DESDecrypt.Tran sformFinalBlock (Buffer,
0, Buffer.Length)) ;
}
}
class Class1
{
static void Main(string[] args) {
TryDes md = new TryDes();
string secrekey = "mysecretke y";
string sometest = md.EncryptTripl eDES("This is
a test",secrekey) ;
Console.WriteLi ne("NET:Encrypt ed string
Base64:"+somete st);
Console.WriteLi ne("NET:Decrypt ed
string:"+md.Dec ryptTripleDES(s ometest,secreke y));
Console.WriteLi ne("");
}
}
}
<============== ========
..Net Output
NET:KeySize:192
NET:BlockSize:6 4
NET:Original String:This is a test
NET:Original Key:NNLkJVyOa+s 8QvJN/X5tqQ==
NET:Encryption Method:TripeDES
NET:IV:7Hh4E1Ys W+A=
NET:Encrypted string Base64:qbGgiTZp 9YTGOutg8IGlqw= =
NET:Decrypted string:This is a test
decoded in a .Net application. Both apps encode/decode a given string
but generate different encrypted results. Anyone have any idea? Code
to follow:
php====>
<?php
// Designate string to be encrypted
$string = "This is a test";
// Encryption/decryption key
$key = pack('H*',md5(" mysecretkey"));
echo "PHP:KeySiz e:";
echo mcrypt_get_key_ size('tripledes ', 'ecb')*8;
echo "<BR>";
echo "PHP:BlockSize: ";
echo mcrypt_get_bloc k_size('tripled es', 'ecb')*8;
echo "<BR>";
// Encryption Algorithm
$cipher_alg = MCRYPT_TRIPLEDE S;
#$cipher_alg = ;
#$cipher_alg = ;
// Create the initialization vector for added security.
//$iv = mcrypt_create_i v(mcrypt_get_iv _size($cipher_a lg,
MCRYPT_MODE_ECB ), MCRYPT_RAND);
$iv = pack("H*","ec78 7813562c5be0");
// Output original string
echo "PHP:Origin al string:$string <br>";
echo "PHP:Origin al Key:". base64_encode($ key)."<br>";
echo "PHP:Encryp tion Method: tripledes <br>";
echo "PHP:IV:".base6 4_encode($iv)." <br>";
// Encrypt $string
//$encrypted_stri ng = mcrypt_encrypt( $cipher_alg, $key, $string,
MCRYPT_MODE_CBC , $iv);
$encrypted_stri ng = mcrypt_encrypt( $cipher_alg, $key, $string,
MCRYPT_MODE_CFB , $iv);
$decrypted_stri ng = mcrypt_decrypt( $cipher_alg, $key,
$encrypted_stri ng, MCRYPT_MODE_CFB , $iv);
echo "PHP:Encryp ted string
Base64:".base64 _encode($encryp ted_string)."<b r>";
echo "PHP:Decryp ted string:$decrypt ed_string";
?>
<============== =====
PHP OUTPUTPHP:
KeySize:192
PHP:BlockSize:6 4
PHP:Original string:This is a test
PHP:Original Key:NNLkJVyOa+s 8QvJN/X5tqQ==
PHP:Encryption Method: tripledes
PHP:IV:7Hh4E1Ys W+A=
PHP:Encrypted string Base64:A+9SKuoL dZaATqa+qmTipg= =
PHP:Decrypted string:This is a test
=============== ============>
C# (.NET)
using System;
using System.Security .Cryptography;
using System.Text;
namespace test
{
class TryDes{
public string EncryptTripleDE S(string Plaintext,
string key) {
TripleDESCrypto ServiceProvider DES = new
TripleDESCrypto ServiceProvider ();
MD5CryptoServic eProvider hashMD5 = new
MD5CryptoServic eProvider();
DES.Key =
hashMD5.Compute Hash(ASCIIEncod ing.ASCII.GetBy tes(key));
DES.Mode = CipherMode.CFB;
DES.IV =
Convert.FromBas e64String("7Hh4 E1YsW+A=");
DES.Padding=Pad dingMode.Zeros;
Console.WriteLi ne("");
Console.WriteLi ne("NET:KeySize :"+DES.KeySize) ;
Console.WriteLi ne("NET:BlockSi ze:"+DES.BlockS ize);
Console.WriteLi ne("NET:Origina l
String:"+Plaint ext);
Console.WriteLi ne("NET:Origina l
Key:"+Convert.T oBase64String(D ES.Key));
Console.WriteLi ne("NET:Encrypt ion
Method:TripeDES ");
//DES.Mode = CipherMode.ECB;
Console.WriteLi ne(
"NET:IV:"+Conve rt.ToBase64Stri ng(DES.IV));
ICryptoTransfor m DESEncrypt =
DES.CreateEncry ptor(DES.Key,DE S.IV);
byte[] Buffer =
ASCIIEncoding.A SCII.GetBytes(P laintext);
return
Convert.ToBase6 4String(DESEncr ypt.TransformFi nalBlock(Buffer , 0,
Buffer.Length)) ;
}
public string DecryptTripleDE S(string base64Text,
string key){
TripleDESCrypto ServiceProvider DES = new
TripleDESCrypto ServiceProvider ();
MD5CryptoServic eProvider hashMD5 = new
MD5CryptoServic eProvider();
DES.Key =
hashMD5.Compute Hash(ASCIIEncod ing.ASCII.GetBy tes(key));
DES.Mode = CipherMode.CFB;
DES.Padding=Pad dingMode.Zeros;
DES.IV = Convert.FromBas e64String("7Hh4 E1YsW+A=");
ICryptoTransfor m DESDecrypt =
DES.CreateDecry ptor(DES.Key,DE S.IV);
byte[] Buffer =
Convert.FromBas e64String(base6 4Text);
return
ASCIIEncoding.A SCII.GetString( DESDecrypt.Tran sformFinalBlock (Buffer,
0, Buffer.Length)) ;
}
}
class Class1
{
static void Main(string[] args) {
TryDes md = new TryDes();
string secrekey = "mysecretke y";
string sometest = md.EncryptTripl eDES("This is
a test",secrekey) ;
Console.WriteLi ne("NET:Encrypt ed string
Base64:"+somete st);
Console.WriteLi ne("NET:Decrypt ed
string:"+md.Dec ryptTripleDES(s ometest,secreke y));
Console.WriteLi ne("");
}
}
}
<============== ========
..Net Output
NET:KeySize:192
NET:BlockSize:6 4
NET:Original String:This is a test
NET:Original Key:NNLkJVyOa+s 8QvJN/X5tqQ==
NET:Encryption Method:TripeDES
NET:IV:7Hh4E1Ys W+A=
NET:Encrypted string Base64:qbGgiTZp 9YTGOutg8IGlqw= =
NET:Decrypted string:This is a test
Comment