求大侠把如下两个加解密的函数转换成PHP语言
public void ENCODE()
{
      byte[] byKey = null;
       byte[] IV = { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18 };
             try
             { 
         byKey = System.Text.Encoding.UTF8.GetBytes(this.encryptKey.Length > 8 ? this.encryptKey.Substring(0, 8) : this.encryptKey);
         DESCryptoServiceProvider des = new DESCryptoServiceProvider();
         byte[] inputByteArray = Encoding.UTF8.GetBytes(this.inputString);
                MemoryStream ms = new MemoryStream();
         CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
this.outString = Convert.ToBase64String(ms.ToArray());
}
                catch (System.Exception error)
                {}
}public void DECODE()
{
byte[] byKey = null;
byte[] IV = { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18 };
byte[] inputByteArray = new Byte[this.inputString.Length];
try
{
                   byKey = System.Text.Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
                   DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(this.inputString);
                   MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = new System.Text.UTF8Encoding();
this.outString = encoding.GetString(ms.ToArray());
}
                  catch (System.Exception error){}
}

解决方案 »

  1.   


     define("IV","12345678")
    function encrypt($str, $key) { $block = mcrypt_get_block_size('des', 'ecb');
    $pad = $block - (strlen($str) % $block);
    $str .= str_repeat(chr($pad), $pad);
    return base64_encode(mcrypt_encrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_CBC, IV));
    } function decrypt($str, $key) {
    $str = base64_decode($str);
    $str = mcrypt_decrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_CBC, IV);
    $block = mcrypt_get_block_size('des', 'ecb');
    $pad = ord($str[($len = strlen($str)) - 1]);
    return substr($str, 0, strlen($str) - $pad);