我使用一个类来加密/解密 PHP 中的字符串。
如何在 Go 中加密/解密字符串?
PHP 类:
class Crypto {
private $encryptKey = 'xxxxxxxxxxxxxxxx';
private $iv = 'xxxxxxxxxxxxxxxx';
private $blocksize = 16;
public function decrypt($data)
{
return $this->unpad(mcrypt_decrypt(MCRYPT_RIJNDAEL_128,
$this->encryptKey,
hex2bin($data),
MCRYPT_MODE_CBC, $this->iv), $this->blocksize);
}
public function encrypt($data)
{
//don't use default php padding which is '\0'
$pad = $this->blocksize - (strlen($data) % $this->blocksize);
$data = $data . str_repeat(chr($pad), $pad);
return bin2hex(mcrypt_encrypt(MCRYPT_RIJNDAEL_128,
$this->encryptKey,
$data, MCRYPT_MODE_CBC, $this->iv));
}
private function unpad($str, $blocksize)
{
$len = strlen($str);
$pad = ord($str[$len - 1]);
if ($pad && $pad <= $blocksize) {
if (substr($str, -$pad) === str_repeat(chr($pad), $pad)) {
return substr($str, 0, $len - $pad);
}
}
return $str;
}
}
什么能够在 PHP 和 Go 中加密/解密相同的字符串。
Cats萌萌
相关分类