如下,有c#的加解密方法,我自己写了一下. 可是不论是加密方法,还是解密方法,都没办法和c#的结果一样.请问哪里不对啊?
public static string Decrypt(string Text, string sKey)
{
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
int num = Text.Length / 2;
byte[] buffer = new byte[num];
for (int i = 0; i < num; i++)
{
int num3 = Convert.ToInt32(Text.Substring(i * 2, 2), 0x10);
buffer[i] = (byte)num3;
}
provider.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
provider.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
stream2.Write(buffer, 0, buffer.Length);
stream2.FlushFinalBlock();
return Encoding.Default.GetString(stream.ToArray());
} public static string Encrypt(string Text, string sKey)
{
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
byte[] bytes = Encoding.Default.GetBytes(Text);
provider.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
provider.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
MemoryStream stream = new MemoryStream(); string x = provider.Mode.ToString();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
stream2.Write(bytes, 0, bytes.Length);
stream2.FlushFinalBlock();
StringBuilder builder = new StringBuilder();
foreach (byte num in stream.ToArray())
{
builder.AppendFormat("{0:X2}", num);
}
return builder.ToString();
}php的public function encrypt($input, $key)
{ $size = mcrypt_get_block_size(MCRYPT_DES, MCRYPT_MODE_CBC);
$input = $this->pkcs5_pad($input, $size);
$td = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_CBC, '');
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key, $iv);
$data = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$data = base64_encode($data);
return $data;
} private function pkcs5_pad($text, $blocksize)
{
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
} public function decrypt($sStr, $sKey)
{
$decrypted = mcrypt_decrypt(
MCRYPT_DES,
$sKey,
base64_decode($sStr),
MCRYPT_MODE_CBC
); $dec_s = strlen($decrypted);
$padding = ord($decrypted[$dec_s - 1]);
$decrypted = substr($decrypted, 0, -$padding);
return $decrypted;
}
public static string Decrypt(string Text, string sKey)
{
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
int num = Text.Length / 2;
byte[] buffer = new byte[num];
for (int i = 0; i < num; i++)
{
int num3 = Convert.ToInt32(Text.Substring(i * 2, 2), 0x10);
buffer[i] = (byte)num3;
}
provider.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
provider.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
stream2.Write(buffer, 0, buffer.Length);
stream2.FlushFinalBlock();
return Encoding.Default.GetString(stream.ToArray());
} public static string Encrypt(string Text, string sKey)
{
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
byte[] bytes = Encoding.Default.GetBytes(Text);
provider.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
provider.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
MemoryStream stream = new MemoryStream(); string x = provider.Mode.ToString();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
stream2.Write(bytes, 0, bytes.Length);
stream2.FlushFinalBlock();
StringBuilder builder = new StringBuilder();
foreach (byte num in stream.ToArray())
{
builder.AppendFormat("{0:X2}", num);
}
return builder.ToString();
}php的public function encrypt($input, $key)
{ $size = mcrypt_get_block_size(MCRYPT_DES, MCRYPT_MODE_CBC);
$input = $this->pkcs5_pad($input, $size);
$td = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_CBC, '');
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key, $iv);
$data = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$data = base64_encode($data);
return $data;
} private function pkcs5_pad($text, $blocksize)
{
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
} public function decrypt($sStr, $sKey)
{
$decrypted = mcrypt_decrypt(
MCRYPT_DES,
$sKey,
base64_decode($sStr),
MCRYPT_MODE_CBC
); $dec_s = strlen($decrypted);
$padding = ord($decrypted[$dec_s - 1]);
$decrypted = substr($decrypted, 0, -$padding);
return $decrypted;
}
return $data;
c# 是 foreach (byte num in stream.ToArray())
{
builder.AppendFormat("{0:X2}", num);
}
return builder.ToString();一个返回的是 base64 编码的结果,一个返回的是十六进制表示的结果
要是会一样,那才是怪哉了