php 针对 89407737 ,3des之后的结果是 04cd1b1572ff6e02e70205bb633b6d70。 我c# 解密之后的结果是 ?6c89407737 ,前面一部分乱码,后面的正确。 换了几种方式,都是前部分乱码。后面的正确。 c# tripledescryptoserviceprovider des = new tripledescryp
php 针对 “89407737” ,3des之后的结果是 “04cd1b1572ff6e02e70205bb633b6d70”。 我c# 解密之后的结果是 “?6c89407737” ,前面一部分乱码,后面的正确。 换了几种方式,都是前部分乱码。后面的正确。
c#
tripledescryptoserviceprovider des = new tripledescryptoserviceprovider();
des.key = encoding.utf8.getbytes(txtkey);
des.iv = encoding.utf8.getbytes(txtiv);
des.mode = ciphermode.cfb;
des.padding = paddingmode.none;
icryptotransform tf = des.createdecryptor();
string hexstring = value;
byte[] bytes = new byte[hexstring.length / 2];
for (int i = 0; i {
bytes[i] = byte.parse(hexstring.substring(i * 2, 2),
system.globalization.numberstyles.hexnumber);
}
return utf8encoding.utf8.getstring(tf.transformfinalblock(bytes, 0, 16));
php:
load('encrypt')->$name;
if ( ! isset($config['key']))
{
// no default encryption key is provided!
throw new kohana_exception('no encryption key is defined in the encryption configuration group: :group',
array(':group' => $name));
}
if ( ! isset($config['mode']))
{
// add the default mode
$config['mode'] = mcrypt_mode_nofb;
}
if ( ! isset($config['cipher']))
{
// add the default cipher
$config['cipher'] = mcrypt_rijndael_128;
}
// create a new instance
encrypt::$instances[$name] = new encrypt($config['key'], $config['mode'], $config['cipher']);
}
return encrypt::$instances[$name];
}
/**
* creates a new mcrypt wrapper.
*
* @param string $key encryption key
* @param string $mode mcrypt mode
* @param string $cipher mcrypt cipher
*/
public function __construct($key, $mode, $cipher)
{
// find the max length of the key, based on cipher and mode
$size = mcrypt_get_key_size($cipher, $mode);
if (isset($key[$size]))
{
// shorten the key to the maximum size
$key = substr($key, 0, $size);
}
// store the key, mode, and cipher
$this->_key = $key;
$this->_mode = $mode;
$this->_cipher = $cipher;
// store the iv size
$this->_iv_size = mcrypt_get_iv_size($this->_cipher, $this->_mode);
}
/**
* encrypts a string and returns an encrypted string that can be decoded.
*
* $data = $encrypt->encode($data);
*
* the encrypted binary data is encoded using [base64](http://php.net/base64_encode)
* to convert it to a string. this string can be stored in a database,
* displayed, and passed using most other means without corruption.
*
* @param string $data data to be encrypted
* @return string
*/
public function encode($data)
{
// set the rand type if it has not already been set
if (encrypt::$_rand === null)
{
if (kohana::$is_windows)
{
// windows only supports the system random number generator
encrypt::$_rand = mcrypt_rand;
}
else
{
if (defined('mcrypt_dev_urandom'))
{
// use /dev/urandom
encrypt::$_rand = mcrypt_dev_urandom;
}
elseif (defined('mcrypt_dev_random'))
{
// use /dev/random
encrypt::$_rand = mcrypt_dev_random;
}
else
{
// use the system random number generator
encrypt::$_rand = mcrypt_rand;
}
}
}
if (encrypt::$_rand === mcrypt_rand)
{
// the system random number generator must always be seeded each
// time it is used, or it will not produce true random results
mt_srand();
}
// create a random initialization vector of the proper size for the current cipher
$iv = mcrypt_create_iv($this->_iv_size, encrypt::$_rand);
// encrypt the data using the configured options and generated iv
$data = mcrypt_encrypt($this->_cipher, $this->_key, $data, $this->_mode, $iv);
// use base64 encoding to convert to a string
//return base64_encode($iv.$data);
return str2hex($iv.$data);
}
/**
* decrypts an encoded string back to its original value.
*
* $data = $encrypt->decode($data);
*
* @param string $data encoded string to be decrypted
* @return false if decryption fails
* @return string
*/
public function decode($data)
{
$data = base64_encode(hex2str($data));
// convert the data back to binary
$data = base64_decode($data, true);
if ( ! $data)
{
// invalid base64 data
return false;
}
// extract the initialization vector from the data
$iv = substr($data, 0, $this->_iv_size);
if ($this->_iv_size !== strlen($iv))
{
// the iv is not the expected size
return false;
}
// remove the iv from the data
$data = substr($data, $this->_iv_size);
// return the decrypted data, trimming the \0 padding bytes from the end of the data
return rtrim(mcrypt_decrypt($this->_cipher, $this->_key, $data, $this->_mode, $iv), \0);
}
}
/**
* 16进制编码转字符串
*
* @param string 16进制编码
*
* @return string 字符串
*/
function hex2str($s)
{
$r = ;
for($i=0; $i {
$r .= chr(hexdec('0x'.$s{$i}.$s{$i+1}));
}
return $r;
}
/**
* 字符串转16进制编码
*
* @param string 字符串
*
* @return string 16进制编码
*/
function str2hex($s)
{
$r = ;
$hexes = array(0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f);
for($i=0; $i {
$r .= ($hexes [(ord($s{$i}) >> 4)] . $hexes [(ord($s{$i}) & 0xf)]);
}
return $r;
}