之前PHP5上常使用的mcrypt库在PHP7.1+上已经被移除,故我们采用openssl对数据进行加解密。
加密方式采用DES-EDE-CBC方式。
密钥填充方式为:采用24位密钥,先将key进行MD5校验取值,得出16位字串,再取key MD5校验值前8位追加到先前的取值后面。由此组装出24位的密钥。
<?php
class DesEdeCbc {
private $cipher, $key, $iv;
/**
* DesEdeCbc constructor.
* @param $cipher
* @param $key
* @param $iv
*/
public function __construct($cipher, $key, $iv) {
$this->cipher = $cipher;
$this->key= $this->getFormatKey($key);
$this->iv = $iv;
}
/**
* @func 加密
* @param $msg
* @return string
*/
public function encrypt($msg) {
$des = @openssl_encrypt($msg, $this->cipher, $this->key, OPENSSL_RAW_DATA, $this->iv);
return base64_encode($des);
}
/**
* @func 解密
* @param $msg
* @return string
*/
public function decrypt($msg) {
return @openssl_decrypt(base64_decode($msg), $this->cipher, $this->key, OPENSSL_RAW_DATA, $this->iv);
}
/**
* @func 生成24位长度的key
* @param $skey
* @return bool|string
*/
private function getFormatKey($skey) {
$md5Value= md5($skey);
$md5ValueLen = strlen($md5Value);
$key = $md5Value . substr($md5Value, 0, $md5ValueLen / 2);
return hex2bin($key);
}
}
$cipher = 'DES-EDE-CBC';
$msg = 'HelloWorld';
$key = '12345678';
$iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
$des = new DesEdeCbc($cipher, $key, $iv);
// 加密
$msg = $des->encrypt($msg);
echo '加密后: ' . $msg . PHP_EOL;
// 解密
$src = $des->decrypt($msg);
echo '解密后: ' . $src . PHP_EOL;
3. 一点说明
可以根据实际情况调整加密方式、key的填充方式、及iv向量来满足不同的需求。
支持的算法有:。非对称加密:。RSA:。公钥加密,私钥解密。私钥加密,公钥解密。对称加密:。AES:。AES-128-ECB,AES-128-CBC,AES-128-OFB,AES-128-CFB,AES-128-CTR。AES-192-ECB,AES-192-CBC,AES-192-OFB,AES-192-CFB,AES-192-CTR。AES-256-ECB,AES-256-CBC,AES-256-OFB,AES-256-CFB,AES-256-CTR。DES:。DES-ECB,DES-CBC,DES-OFB,DES-CFB,。3DES:。3DES-ECB,3DES-CBC,3DES-OFB,3DES-CFB,。数据摘要算法:。md5,sha,sha1,sha256,sha512。Hamc算法:。Hamc-md5,Hamc-sha,Hamc-sha1,Hamc-sha256,Hamc-sha512。签名:。RSA签名, 验签。另外还有 PEM文件读取,写出等操作。@落款hMZ。Tags:OPENSSL加密解密。
PHP实现DES、ECB、PKCS5Padding加密兼容Java SHA1PRNG算法
额外:
public static final String ALGORITHM = "DES";
public static final String TRANSFORMATION = "DES/ECB/PKCS5Padding";
public static String encrypt(String contents, String password) throws Exception {
try
{
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, generateKey(password));
byte[] output = cipher.doFinal(contents.getBytes("utf-8"));
return new String(Base64.encodeBase64(output), "utf-8");
}
catch (Exception e)
{
logger.error("加密失败:{}",e);
}
return null;
}
public static Key generateKey(String password) throws Exception {
return generateKey(password.getBytes());
}
public static Key generateKey(byte[] seed) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(seed);
keyGenerator.init(56,secureRandom);
Key key = keyGenerator.generateKey();
return key;
}
php加密
$key = "012345";
$data = "1866666345";
$key = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16);
$out = openssl_encrypt($data, 'DES-ECB', $key, OPENSSL_RAW_DATA);
echo base64_encode($out);