我必须使用TDES将字符串加密为8字节的十六进制。以下是值(修改以供参考)
key = 636948778095358323114731
pin=1234
加密代码:
function encryptText_3des($plainText, $key) {
$key = hash("md5", $key, TRUE);
for ($x=0;$x<8;$x++) {
$key = $key.substr($key, $x, 1);
}
$padded = pkcs5_pad($plainText,mcrypt_get_block_size(MCRYPT_3DES, MCRYPT_MODE_CBC));
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_3DES, $key, $padded, MCRYPT_MODE_CBC));
return $encrypted;
}
function pkcs5_pad ($text, $blocksize) {
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
然而,当我这样做时:
//outputs 3des encrypted data
echo encryptText_3des($data, $encryption_key);
我收到此错误:
Warning: mcrypt_encrypt(): Encryption mode requires an initialization
vector of size 8
我怎样才能得到这个值?..(我还需要它作为一个8字节的十六进制..) 感谢
答案 0 :(得分:1)
您错过了加密函数的初始化向量:
"lint": [
{
"project": "src/tsconfig.app.json",
"exclude": ["**/node_modules/**","**/path/**"]
},
{
"project": "src/tsconfig.spec.json",
"exclude": ["**/node_modules/**","**/path/**"]
},
{
"project": "e2e/tsconfig.e2e.json",
"exclude": ["**/node_modules/**","**/path/**"]
}
],
另外,不要忘记在某处保存function encryptText_3des($plainText, $key)
{
$key = hash("md5", $key, TRUE);
for ($x = 0; $x < 8; $x++) {
$key = $key . substr($key, $x, 1);
}
$padded = pkcs5_pad($plainText,mcrypt_get_block_size(MCRYPT_3DES, MCRYPT_MODE_CBC));
// CBC initialization vector
$iv_size = mcrypt_get_iv_size(MCRYPT_3DES, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_3DES, $key, $padded, MCRYPT_MODE_CBC, $iv));
return $encrypted;
}
字符串(例如包含在加密字符串中),因为稍后TDES解密过程需要IV字节。