不多说,直接上代码类
然后自行依据实际情况调用即可
<?php
class STD3Des {
public static function encrypt($str, $key='xxx') {
$message = $str;
$iv = '';
$message_padded = $message;
$message_padded = self::pkcs5_pad($message_padded, 8);
if (strlen($message_padded) % 8) {
$message_padded = str_pad($message_padded, strlen($message_padded) + 8 - strlen($message_padded) % 8, "\0");
}
//$message_padded = self::pkcs5_pad($message_padded, 8);//原来是在这打包的,运行了一下发现密文不一样,就改到上面了
// $encrypted_mcrypt = mcrypt_encrypt(MCRYPT_3DES, $key,
// self::pkcs5_pad($message, 8), MCRYPT_MODE_ECB, $iv);
$encrypted_openssl = openssl_encrypt($message_padded, "DES-EDE3", $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING);
//return strtoupper(bin2hex($encrypted_mcrypt));
return bin2hex($encrypted_openssl);
}
public static function encrypt_ECB($str, $key) {
$message = $str;
$iv = '';
$message_padded = $message;
$message_padded = self::pkcs5_pad($message_padded, 8);
if (strlen($message_padded) % 8) {
$message_padded = str_pad($message_padded, strlen($message_padded) + 8 - strlen($message_padded) % 8, "\0");
}
//$message_padded = self::pkcs5_pad($message_padded, 8);//原来是在这打包的,运行了一下发现密文不一样,就改到上面了
// $encrypted_mcrypt = mcrypt_encrypt(MCRYPT_3DES, $key,
// self::pkcs5_pad($message, 8), MCRYPT_MODE_ECB, $iv);
$encrypted_openssl = openssl_encrypt($message_padded, "DES-EDE3", $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING);
//return strtoupper(bin2hex($encrypted_mcrypt));
//return bin2hex($encrypted_openssl);
return base64_encode($encrypted_openssl);
}
public static function decrypt_ECB($encrypted, $key) {//数据解密
$data = base64_decode($encrypted);
// $data = $encrypted;
$decrypted_openssl = openssl_decrypt($data, 'DES-EDE3', $key, OPENSSL_RAW_DATA);
return $decrypted_openssl;
}
public static function encrypt_CFB8($str, $key, $iv) {
$message = $str;
$message_padded = $message;
$message_padded = self::pkcs5_pad($message_padded, 8);
if (strlen($message_padded) % 8) {
$message_padded = str_pad($message_padded, strlen($message_padded) + 8 - strlen($message_padded) % 8, "\0");
}
$encrypted_openssl = openssl_encrypt($message_padded, "DES-EDE3-CFB8", $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $iv);
return base64_encode($encrypted_openssl);
}
public static function decrypt($encrypted, $key) {//数据解密
$data = hex2bin($encrypted);
// $data = $encrypted;
$decrypted_openssl = openssl_decrypt($data, 'DES-EDE3', $key, OPENSSL_RAW_DATA);
return $decrypted_openssl;
}
private static function pkcs5_pad($text, $blocksize) {
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
}
//$encrypted_openssl =STD3Des::encrypt('0123456789', 'test0123456789');
//echo 'encrypted_openssl=='.$encrypted_openssl.'<br />';
//034dcda761d12373f82b025355c6870d
//echo STD3Des::decrypt($encrypted_openssl, 'test0123456789');
/**
*
(1)进行3DES加密(ECB模式)。注:加密密钥不足24位长度时后面补0,超过24位长度时需截取前24位字符。
(2)将加密后的密文进行hex转换。
3DES测试结果比对:
密钥:test0123456789
测试数据:0123456789
加密结果:034dcda761d12373f82b025355c6870d
*/
?>
自行了解引用,有测试示例在代码中
1.
2.
3.
4.
5.
6.文章来源:https://www.toymoban.com/news/detail-537148.html
7.文章来源地址https://www.toymoban.com/news/detail-537148.html
到了这里,关于PHP 3DES加密的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!