K
Kelvin
Hi All,
I have still problem on Encryption string using, decryption string
using ASP/VB/C#.. I don't know why have different results and using
same algorithm.
Please advise.
PHP Sources Code
<?php
$key = "pg";
$random_number = kelvintai;
$encrypted=encrypt($key,$random_number);
$decrypted=decrypt($key,$encrypted);
echo "NUMBER: $random_number<br>
ENCRYPTED VALUE: $encrypted<br>
DECRYPTED VALUE: $decrypted";
/* Your functions without the comments are below */
function encrypt($key, $plain_text) {
$plain_text = trim($plain_text);
$iv = substr(md5($key), 0,mcrypt_get_iv_size
(MCRYPT_CAST_128,MCRYPT_MODE_ECB));
$c_t = mcrypt_ecb (MCRYPT_CAST_128, $key, $plain_text, MCRYPT_ENCRYPT,
$iv);
return trim(chop(base64_encode($c_t)));
}
function decrypt($key, $c_t) {
$c_t = trim(chop(base64_decode($c_t)));
$iv = substr(md5($key), 0,mcrypt_get_iv_size
(MCRYPT_CAST_128,MCRYPT_MODE_ECB));
$p_t = mcrypt_ecb (MCRYPT_CAST_128, $key, $c_t, MCRYPT_DECRYPT, $iv);
return trim(chop($p_t));
}
?>
ASP.NET/VB.NET Sources Code.
Public Shared Function DecryptTripleDES(ByVal sOut As String, ByVal
sKey As String) As String
Dim DES As New
System.Security.Cryptography.TripleDESCryptoServiceProvider
Dim hashMD5 As New
System.Security.Cryptography.MD5CryptoServiceProvider
' scramble the key
sKey = ScrambleKey(sKey)
' Compute the MD5 hash.
DES.Key = hashMD5.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(sKey))
' Set the cipher mode.
DES.Mode = System.Security.Cryptography.CipherMode.ECB
' Create the decryptor.
Dim DESDecrypt As
System.Security.Cryptography.ICryptoTransform = DES.CreateDecryptor()
Dim Buffer As Byte() = Convert.FromBase64String(sOut)
' Transform and return the string.
Return System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer,
0, Buffer.Length))
End Function
Private Shared Function ScrambleKey(ByVal v_strKey As String) As
String
Dim sbKey As New System.Text.StringBuilder
Dim intPtr As Integer
For intPtr = 1 To v_strKey.Length
Dim intIn As Integer = v_strKey.Length - intPtr + 1
sbKey.Append(Mid(v_strKey, intIn, 1))
Next
Dim strKey As String = sbKey.ToString
Return sbKey.ToString
End Function
I have still problem on Encryption string using, decryption string
using ASP/VB/C#.. I don't know why have different results and using
same algorithm.
Please advise.
PHP Sources Code
<?php
$key = "pg";
$random_number = kelvintai;
$encrypted=encrypt($key,$random_number);
$decrypted=decrypt($key,$encrypted);
echo "NUMBER: $random_number<br>
ENCRYPTED VALUE: $encrypted<br>
DECRYPTED VALUE: $decrypted";
/* Your functions without the comments are below */
function encrypt($key, $plain_text) {
$plain_text = trim($plain_text);
$iv = substr(md5($key), 0,mcrypt_get_iv_size
(MCRYPT_CAST_128,MCRYPT_MODE_ECB));
$c_t = mcrypt_ecb (MCRYPT_CAST_128, $key, $plain_text, MCRYPT_ENCRYPT,
$iv);
return trim(chop(base64_encode($c_t)));
}
function decrypt($key, $c_t) {
$c_t = trim(chop(base64_decode($c_t)));
$iv = substr(md5($key), 0,mcrypt_get_iv_size
(MCRYPT_CAST_128,MCRYPT_MODE_ECB));
$p_t = mcrypt_ecb (MCRYPT_CAST_128, $key, $c_t, MCRYPT_DECRYPT, $iv);
return trim(chop($p_t));
}
?>
ASP.NET/VB.NET Sources Code.
Public Shared Function DecryptTripleDES(ByVal sOut As String, ByVal
sKey As String) As String
Dim DES As New
System.Security.Cryptography.TripleDESCryptoServiceProvider
Dim hashMD5 As New
System.Security.Cryptography.MD5CryptoServiceProvider
' scramble the key
sKey = ScrambleKey(sKey)
' Compute the MD5 hash.
DES.Key = hashMD5.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(sKey))
' Set the cipher mode.
DES.Mode = System.Security.Cryptography.CipherMode.ECB
' Create the decryptor.
Dim DESDecrypt As
System.Security.Cryptography.ICryptoTransform = DES.CreateDecryptor()
Dim Buffer As Byte() = Convert.FromBase64String(sOut)
' Transform and return the string.
Return System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer,
0, Buffer.Length))
End Function
Private Shared Function ScrambleKey(ByVal v_strKey As String) As
String
Dim sbKey As New System.Text.StringBuilder
Dim intPtr As Integer
For intPtr = 1 To v_strKey.Length
Dim intIn As Integer = v_strKey.Length - intPtr + 1
sbKey.Append(Mid(v_strKey, intIn, 1))
Next
Dim strKey As String = sbKey.ToString
Return sbKey.ToString
End Function