最近在做一個(gè)php版本的DES的加密算法,接口方只提供了java的demo,只能先把java的算法跑通,再去根據(jù)答案一步一步去對(duì)比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(); // 設(shè)置密鑰,略去
// 設(shè)置向量,略去
private AlgorithmParameterSpec iv = null; // 加密算法的參數(shù)接口,IvParameterSpec是它的一個(gè)實(shí)現(xiàn)
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);// 設(shè)置密鑰參數(shù)
iv = new IvParameterSpec(ivBytes);// 設(shè)置向量
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 獲得密鑰工廠
key = keyFactory.generateSecret(keySpec);// 得到密鑰對(duì)象
}
public String encode(String data) throws Exception {
Cipher enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");// 得到加密對(duì)象Cipher
enCipher.init(Cipher.ENCRYPT_MODE, key, iv);// 設(shè)置工作模式為加密模式,給出密鑰和向量
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
/**
* 字符串轉(zhuǎn)字節(jié)數(shù)組
* Class Str2BytesArr
*/
class Str2BytesArr
{
/**
* 轉(zhuǎn)換一個(gè)String字符串為byte數(shù)組
* @param $str 需要轉(zhuǎn)換的字符串
* @param $bytes 目標(biāo)byte數(shù)組
* @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;
}
/**
* 將字節(jié)數(shù)組轉(zhuǎn)化為String類型的數(shù)據(jù)
* @param $bytes 字節(jié)數(shù)組
* @param $str 目標(biāo)字符串
* @return 一個(gè)String類型的數(shù)據(jù)
*/
public static function toStr($bytes) {
$str = '';
foreach($bytes as $ch) {
$str .= chr($ch);
}
return $str;
}
/**
* 轉(zhuǎn)換一個(gè)int為byte數(shù)組
* @param $byt 目標(biāo)byte數(shù)組
* @param $val 需要轉(zhuǎn)換的字符串
*
*/
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;
}
/**
* 從字節(jié)數(shù)組中指定的位置讀取一個(gè)Integer類型的數(shù)據(jù)
* @param $bytes 字節(jié)數(shù)組
* @param $position 指定的開始位置
* @return 一個(gè)Integer類型的數(shù)據(jù)
*/
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;
}
/**
* 轉(zhuǎn)換一個(gè)shor字符串為byte數(shù)組
* @param $byt 目標(biāo)byte數(shù)組
* @param $val 需要轉(zhuǎn)換的字符串
*
*/
public static function shortToBytes($val) {
$byt = array();
$byt[0] = ($val & 0xff);
$byt[1] = ($val >> 8 & 0xff);
return $byt;
}
/**
* 從字節(jié)數(shù)組中指定的位置讀取一個(gè)Short類型的數(shù)據(jù)。
* @param $bytes 字節(jié)數(shù)組
* @param $position 指定的開始位置
* @return 一個(gè)Short類型的數(shù)據(jù)
*/
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')
{
// 計(jì)算 key 計(jì)算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分別實(shí)現(xiàn)DES加密的算法demo”的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注木子天禾科技其它相關(guān)文章!