PHP和C#通用的DES加密解密

因为一个项目的需要,需要PHP与C#之间的DES加密解密实现,网上一顿找例子,大部分都是坑爹的啊,即使题目上写着能互通的都不行,后来经过自己的一些改进,终于把这个PHP和C#的DES加密解密给调通了,不容易啊,有需要的朋友,可以直接拿来用,绝不坑爹!

关于DES的原理以后有时间再慢慢的研究,研究完到时候跟大家汇报一下~

PHP代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class DesCrypt {
var $key ;
var $iv ;
function DES($key) {
// key长度8例如:1234abcd
$this->key = $key;
$this->iv = $key;
}
function encrypt($str) {
// 加密,返回大写十六进制字符串
$size = mcrypt_get_block_size ( MCRYPT_DES, MCRYPT_MODE_CBC );
$str = $this->pkcs5Pad ( $str, $size );
return strtoupper ( bin2hex ( mcrypt_cbc ( MCRYPT_DES, $this->key, $str, MCRYPT_ENCRYPT, $this->iv ) ) );
}
function decrypt($str) {
// 解密
$strBin = $this->hex2bin ( strtolower ( $str ) );
$str = mcrypt_cbc ( MCRYPT_DES, $this->key, $strBin, MCRYPT_DECRYPT, $this->iv );
$str = $this->pkcs5Unpad ( $str );
return $str;
}
function hex2bin($hexData) {
$binData = "";
for($i = 0; $i < strlen ( $hexData ); $i += 2) { $binData .= chr ( hexdec ( substr ( $hexData, $i, 2 ) ) ); } return $binData; } function pkcs5Pad($text, $blocksize) { $pad = $blocksize - (strlen ( $text ) % $blocksize); return $text . str_repeat ( chr ( $pad ), $pad ); } function pkcs5Unpad($text) { $pad = ord ( $text {strlen ( $text ) - 1} ); if ($pad > strlen ( $text ))
return false;
if (strspn ( $text, chr ( $pad ), strlen ( $text ) - $pad ) != $pad)
return false;
return substr ( $text, 0, - 1 * $pad );
}
}

C#代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class MYDES
{
public static string Encrypt(string source, string _DESKey)
{
byte[] inputByteArray = Encoding.UTF8.GetBytes(source);
byte[] k = ASCIIEncoding.ASCII.GetBytes(_DESKey);
byte[] result = null;
StringBuilder sb = new StringBuilder();
string encrypt = "";
using (DES des = new DESCryptoServiceProvider() { Key = k, IV = k })
{
using (MemoryStream outStream = new MemoryStream())
{
using (CryptoStream encStream = new CryptoStream(outStream, des.CreateEncryptor(), CryptoStreamMode.Write))
{
encStream.Write(inputByteArray, 0, inputByteArray.Length);
encStream.Close();
result = outStream.ToArray();
foreach (byte b in outStream.ToArray())
{
sb.AppendFormat("{0:X2}", b);
}
encrypt = sb.ToString();
}
}
}
//string encrypt;
return MYDES.byteToHexStr(result);
}
public static byte[] strToToHexByte(string hexString)
{
hexString = hexString.Replace(" ", ""); if ((hexString.Length % 2) != 0) hexString += " ";
byte[] returnBytes = new byte[hexString.Length / 2]; for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
return returnBytes;
}
public static string byteToHexStr(byte[] bytes)
{
string returnStr = ""; if (bytes != null)
{
for (int i = 0; i < bytes.Length; i++)
{
returnStr += bytes[i].ToString("X2");
}
}
return returnStr;
}
public static string Decrypt(string source, string sKey)
{
try
{
byte[] s = MYDES.strToToHexByte(source);
byte[] k = ASCIIEncoding.ASCII.GetBytes(sKey);
byte[] result = null;
using (DES des = new DESCryptoServiceProvider() { Key = k, IV = k })
using (MemoryStream inStream = new MemoryStream(s))
{
using (CryptoStream encStream = new CryptoStream(inStream, des.CreateDecryptor(), CryptoStreamMode.Read))
{
List byteList = new List();
int i;
while ((i = encStream.ReadByte()) != -1)
{
byteList.Add((byte)i);
}
result = byteList.ToArray();
}
}
string str = Encoding.UTF8.GetString(result);
return str;
}
catch (Exception e) { return e.ToString(); }
}
}