最近对接一大堆接口,找了各种des加解密,很多都是php7以下的,要知道mcrypt_decrypt
此类的方法再php7以上已经过时或废除了
Warning
This function has been DEPRECATED as of PHP 7.1.0 and REMOVED as of PHP 7.2.0. Relying on this function is highly discouraged.
搞了好久,好不容易看到了这个 https://blog.csdn.net/qq43599939/article/details/80226482 ,终于将C#、Java的代码转换成了php7的代码:
<?php
/**
* Des加解密,兼容java、C#
*/
class Des
{
/**
* 加密
* @author TechLee
* @param string $input
* @param string $key
* @param string $method
* @param string $iv
* @return string
*/
public static function encrypt($input, $key, $method = 'DES-ECB', $iv = null)
{
$iv = $iv ? $iv : self::createIv();
return base64_encode(openssl_encrypt($input, $method, $key, OPENSSL_RAW_DATA, $method == 'DES-ECB' ? '' : $iv));
}
/**
* 解密
* @author TechLee
* @param string $input
* @param string $key
* @param string $method
* @param string $iv
* @return string
*/
public static function decrypt($input, $key, $method = 'DES-ECB', $iv = null)
{
$iv = $iv ? $iv : self::createIv();
return openssl_decrypt(base64_decode($input), $method, $key, OPENSSL_RAW_DATA, $method == 'DES-ECB' ? '' : $iv);
}
/**
* 这个玩意相当于java里的
* byte[] iv = { 0, 0, 0, 0, 0,0, 0, 0 }
* 也相当于C#里的
* IV = new byte[8];
* @author TechLee
* @return [type] [description]
*/
public static function createIv()
{
return self::hexToStr("0000000000000000");
}
public static function hexToStr($hex)
{
$string = '';
for ($i = 0; $i < strlen($hex) - 1; $i += 2) {
$string .= chr(hexdec($hex[$i] . $hex[$i + 1]));
}
return $string;
}
}
关于des的模式有很多种,只有DES-ECB
不需要iv
DES
DES-CBC
DES-CFB
DES-CFB1
DES-CFB8
DES-ECB
DES-EDE
DES-EDE-CBC
DES-EDE-CFB
DES-EDE-ECB
DES-EDE-OFB
DES-EDE3
DES-EDE3-CBC
DES-EDE3-CFB
DES-EDE3-CFB1
DES-EDE3-CFB8
DES-EDE3-ECB
DES-EDE3-OFB
DES-OFB
DES3
DESX
DESX-CBC
可以在线加解密验证
http://tool.chacuo.net/cryptdes
同时这里附上C#和java的代码
C#解密:
public static string DESDecrypt(string input, string key)
{
System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider();
des.Key = Encoding.UTF8.GetBytes(key);
des.IV = new byte[8];
System.Security.Cryptography.ICryptoTransform cTransform = des.CreateDecryptor(des.Key, des.IV);
System.IO.MemoryStream mStream = new System.IO.MemoryStream();
System.Security.Cryptography.CryptoStream cStream = new System.Security.Cryptography.CryptoStream(mStream, cTransform, System.Security.Cryptography.CryptoStreamMode.Write);
byte[] byt = Convert.FromBase64String(input);
cStream.Write(byt, 0, byt.Length);
cStream.FlushFinalBlock();
cStream.Close();
return Encoding.UTF8.GetString(mStream.ToArray());
}
Java解密代码:
public static String decode(byte[] DESkey, String data) throws Exception {
DESKeySpec keySpec = new DESKeySpec(DESkey);// 设置密钥参数
byte[] DESIV = { 0, 0, 0, 0, 0,0, 0, 0 };// 设置向量
AlgorithmParameterSpec iv = new IvParameterSpec(DESIV);// 加密算法的参数接口,IvParameterSpec是它的一个实现
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 获得密钥工厂
SecretKey key = keyFactory.generateSecret(keySpec);// 得到密钥对象
Cipher deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
deCipher.init(Cipher.DECRYPT_MODE, key, iv);
BASE64Decoder base64Decoder = new BASE64Decoder();
byte[] pasByte = deCipher.doFinal(base64Decoder.decodeBuffer(data));
return new String(pasByte, "UTF-8");
}