最近在做一个php版本的DES的加密算法,接口方只提供了java的demo,只能先把java的算法跑通,再去根据答案一步一步去对比php的算法。
java代码如下:
package test;
import java.security.Key;
import java.security.MessageDigest;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
/**
* Author: of3175
* Date : 2020 - 06 - 22 15:31
*/
public class DesUtils12306 {
private final byte[] DESkey = "rereerererew".getBytes(); // 设置密钥,略去
// 设置向量,略去
private AlgorithmParameterSpec iv = null; // 加密算法的参数接口,IvParameterSpec是它的一个实现
private Key key = null;
public DesUtils12306() throws Exception {
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] digest = md.digest(DESkey);
System.out.println(digest.length);
for (int i = 0; i < digest.length; i++) {
System.out.println(digest[i]);
}
byte[] keyBytes = new byte[8];
byte[] ivBytes = new byte[8];
for (int i = 0; i < 8; i++) {
keyBytes[i] = digest[i];
// System.out.println(digest[i]);
}
for (int i = 8; i < 16; i++) {
ivBytes[i - 8] = digest[i];
// System.out.println(digest[i]);
}
DESKeySpec keySpec = new DESKeySpec(keyBytes);// 设置密钥参数
iv = new IvParameterSpec(ivBytes);// 设置向量
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 获得密钥工厂
key = keyFactory.generateSecret(keySpec);// 得到密钥对象
}
public String encode(String data) throws Exception {
Cipher enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");// 得到加密对象Cipher
enCipher.init(Cipher.ENCRYPT_MODE, key, iv);// 设置工作模式为加密模式,给出密钥和向量
byte[] pasByte = enCipher.doFinal(data.getBytes());
return toHexString(pasByte).toUpperCase();
}
public static String toHexString(byte b[]) {
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < b.length; i++) {
String plainText = Integer.toHexString(0xff & b[i]);
if (plainText.length() < 2)
plainText = "0" + plainText;
hexString.append(plainText);
}
return hexString.toString();
}
public static void main(String[] args) throws Exception {
String str = "LK_8077l";
DesUtils12306 util = new DesUtils12306();
String re = util.encode(str);
System.out.println(re);
}
}
最终的php代码如下:
Str2BytesArr.class.php
最终的php代码如下:
Str2BytesArr.class.php
/**
* 字符串转字节数组
* Class Str2BytesArr
*/
class Str2BytesArr
{
/**
* 转换一个String字符串为byte数组
* @param $str 需要转换的字符串
* @param $bytes 目标byte数组
* @author Zikie
*/
public static function getbytes($str)
{
$len = strlen($str);
$bytes = array();
for($i=0;$i<$len;$i++) {
if(ord($str[$i]) >= 128){
$byte = ord($str[$i]) - 256;
}else{
$byte = ord($str[$i]);
}
$bytes[] = $byte ;
}
return $bytes;
}
/**
* 将字节数组转化为String类型的数据
* @param $bytes 字节数组
* @param $str 目标字符串
* @return 一个String类型的数据
*/
public static function toStr($bytes) {
$str = '';
foreach($bytes as $ch) {
$str .= chr($ch);
}
return $str;
}
/**
* 转换一个int为byte数组
* @param $byt 目标byte数组
* @param $val 需要转换的字符串
*
*/
public static function integerToBytes($val) {
$byt = array();
$byt[0] = ($val & 0xff);
$byt[1] = ($val >> 8 & 0xff);
$byt[2] = ($val >> 16 & 0xff);
$byt[3] = ($val >> 24 & 0xff);
return $byt;
}
/**
* 从字节数组中指定的位置读取一个Integer类型的数据
* @param $bytes 字节数组
* @param $position 指定的开始位置
* @return 一个Integer类型的数据
*/
public static function bytesToInteger($bytes, $position) {
$val = 0;
$val = $bytes[$position + 3] & 0xff;
$val <<= 8;
$val |= $bytes[$position + 2] & 0xff;
$val <<= 8;
$val |= $bytes[$position + 1] & 0xff;
$val <<= 8;
$val |= $bytes[$position] & 0xff;
return $val;
}
/**
* 转换一个shor字符串为byte数组
* @param $byt 目标byte数组
* @param $val 需要转换的字符串
*
*/
public static function shortToBytes($val) {
$byt = array();
$byt[0] = ($val & 0xff);
$byt[1] = ($val >> 8 & 0xff);
return $byt;
}
/**
* 从字节数组中指定的位置读取一个Short类型的数据。
* @param $bytes 字节数组
* @param $position 指定的开始位置
* @return 一个Short类型的数据
*/
public static function bytesToShort($bytes, $position) {
$val = 0;
$val = $bytes[$position + 1] & 0xFF;
$val = $val << 8;
$val |= $bytes[$position] & 0xFF;
return $val;
}
}
DesTicket.class.php
DesTicket.class.php
/**
* des加解密
* Class DesTicket
*/
class DesTicket
{
/**
* 生成key和iv
* @param $key
* @return array
*/
public static function getKeyAndIv($key = 'L82V6ZVD6J')
{
// 计算 key 计算iv
$sha1 = sha1($key,true);
$arr = Str2BytesArr::getbytes($sha1);
for ($i = 0;$i<8;$i++) {
$key_bytes[] = $arr[$i];
}
for ($i = 8;$i<16;$i++) {
$iv_bytes[] = $arr[$i];
}
$key = Str2BytesArr::toStr($key_bytes);
$iv = Str2BytesArr::toStr($iv_bytes);
return [
'key'=> $key,
'iv' => $iv,
];
}
// 加密
public static function encrypt($str)
{
$arr = self::getKeyAndIv();
$size = mcrypt_get_block_size(MCRYPT_DES, MCRYPT_MODE_CBC);
$str = self::Pkcs5Pad($str, $size);
$data = mcrypt_encrypt(MCRYPT_DES, $arr['key'], $str, "cbc", $arr['iv']);
return strtoupper(bin2hex($data));
}
// 解密
public static function decrypt($str)
{
$arr = self::getKeyAndIv();
$str = hex2bin($str);
$str = mcrypt_decrypt(MCRYPT_DES, $arr['key'], $str, "cbc", $arr['iv']);
$str = self::pkcs5Unpad($str);
return $str;
}
public static function hex2bin($hexData)
{
$binData = "";
for ($i = 0; $i < strlen($hexData); $i += 2) {
$binData .= chr(hexdec(substr($hexData, $i, 2)));
}
return $binData;
}
public static function pkcs5Pad($text, $blocksize)
{
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
public static function pkcs5Unpad($text)
{
$pad = ord($text{strlen($text) - 1});
if ($pad > strlen($text))
return false;
if (strspn($text, chr($pad), strlen($text) - $pad) != $pad)
return false;
return substr($text, 0, -1 * $pad);
}
}
以上就是“java和php分别实现DES加密的算法demo”的详细内容,更多请关注木子天禾科技其它相关文章!