php与java的des加密解密
与第三⽅接⼝对接des加密、解密,第三⽅提供java的des加密解密demo,特记录PHP与java加密解密。
import*;
import DESKeySpec;
import SecretKeySpec;
import IOException;
import Key;
import NoSuchAlgorithmException;
import SecureRandom;
import Date;
import Locale;
import BASE64Decoder;
public class DesUtil {
/**
* DES解密
*
* @param secretData 密码字符串
* @param secretKey  解密密钥
* @return 原始字符串
* @throws Exception
*/
private static final String DES_ALGORITHM ="DES";
public static String decryption(String secretData, String secretKey)throws Exception {
if(secretData ==null)
return null;
//表⽰为ios加密
if(secretData.startsWith("ios:")){
secretData = secretData.split(":")[1];
return decrypt(secretData,secretKey);
}else{
Cipher cipher =null;
try{
cipher = Instance("DES/ECB/NoPadding");
cipher.init(Cipher.DECRYPT_MODE,generateKey(secretKey));
java加密方式有哪些}catch(NoSuchAlgorithmException e){
e.printStackTrace();
throw new Exception("NoSuchAlgorithmException", e);
}catch(NoSuchPaddingException e){
e.printStackTrace();
throw new Exception("NoSuchPaddingException", e);
}catch(Exception e){
e.printStackTrace();
}
try{
byte[] buf = cipher.doFinal(hexStr2Bytes(secretData));
int num =0;
for(byte b: buf){
String name = b+"";
if(name.length()==1){
num++;
}
}
byte[] bytes =new byte[buf.length-num];
for(int i =0; i < buf.length; i++){
String name = buf[i]+"";
if(name.length()!=1){
bytes[i]= buf[i];
}
}
return new String(bytes,"utf-8");
}catch(Exception e){
e.printStackTrace();
throw new Exception("IllegalBlockSizeException", e);
throw new Exception("IllegalBlockSizeException", e);
}
}
}
public static byte[]hexStr2Bytes(String src){
src = im().replace(" ","").toUpperCase(Locale.US);
int m =0, n =0;
int iLen = src.length()/2;//计算长度
byte[] ret =new byte[iLen];//分配存储空间
for(int i =0; i < iLen; i++){
m = i *2+1;
n = m +1;
ret[i]=(byte)(Integer.decode("0X"+ src.substring(i *2, m)+ src.substring(m, n))&0xFF);
}
return ret;
}
/**
* 获得秘密密钥
*
* @param secretKey
* @return
* @throws NoSuchAlgorithmException
*/
private static SecretKey generateKey(String secretKey)
throws Exception {
SecretKeyFactory keyFactory = Instance(DES_ALGORITHM);
DESKeySpec keySpec =new Bytes());
ateSecret(keySpec);
}
public static String decrypt(String data, String key)throws IOException,
Exception {
BASE64Decoder decoder =new BASE64Decoder();
byte[] buf = decoder.decodeBuffer(data);
byte[] bt =decrypt(Bytes());
return new String(bt);
}
/**
* Description 根据键值进⾏解密
* @param data
* @param key  加密键byte数组
* @return
* @throws Exception
*/
private static byte[]decrypt(byte[] data,byte[] key)throws Exception {
// ⽣成⼀个可信任的随机数源
SecureRandom sr =new SecureRandom();
/
/ 从原始密钥数据创建DESKeySpec对象
DESKeySpec dks =new DESKeySpec(key);
// 创建⼀个密钥⼯⼚,然后⽤它把DESKeySpec转换成SecretKey对象
SecretKeyFactory keyFactory = Instance(DES_ALGORITHM);
SecretKey securekey = ateSecret(dks);
// Cipher对象实际完成解密操作
Cipher cipher = Instance(DES_ALGORITHM);
// ⽤密钥初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
return cipher.doFinal(data);
}
/
**
* DES算法,加密
*
* @param data 待加密字符串
* @param key  加密私钥,长度不能够⼩于8位
* @return 加密后的字节数组,⼀般结合Base64编码使⽤
*/
public static String encode(String data,String key)throws Exception{
SecretKeySpec key0 =new SecretKeySpec(getKey(key),"DES");
SecretKeySpec key0 =new SecretKeySpec(getKey(key),"DES");
Cipher cipher = Instance("DES/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key0);
byte[] encryptedData = cipher.doFinal(padding(data));
return bytesToHexString(encryptedData);
}
public static byte[]getKey(String keyRule){
Key key =null;
byte[] keyByte = Bytes();
// 创建⼀个空的⼋位数组,默认情况下为0
byte[] byteTemp =new byte[8];
for(int i =0; i < byteTemp.length && i < keyByte.length; i++){
byteTemp[i]= keyByte[i];
}
key =new SecretKeySpec(byteTemp,"DES");
Encoded();
}
public static byte[]padding(String arg_text){
byte[] encrypt = Bytes();
if(encrypt.length %8!=0){//not a multiple of 8
byte[] padded =new byte[encrypt.length +8-(encrypt.length %8)];
System.arraycopy(encrypt,0, padded,0, encrypt.length);
encrypt = padded;
}
return encrypt;
}
/**
* 把字节数组转换成16进制字符串
* @param bArray
* @return
*/
public static final String bytesToHexString(byte[] bArray){
if(bArray ==null)
{
return"";
}
StringBuffer sb =new StringBuffer(bArray.length);
String sTemp;
for(int i =0; i < bArray.length; i++){
sTemp = HexString(0xFF& bArray[i]);
if(sTemp.length()<2)
sb.append(0);
sb.UpperCase());
}
String();
}
public static void main(String[] args)throws Exception {
String key="qwertyuiop123456";
String data ="1638265429000qazxswedc951753";
String signatures =encode(data, key);
System.out.println("java加密结果:"+signatures);
String decryptData =decryption(signatures , key);
System.out.println("java解密结果:"+decryptData );
String phpDes ="56f9e0db4850341b1c734b93b5138eea8f7a9432d8c5680cc5ac9a286aceff04";        String decryptDataForPhp =decryption(phpDes,key);
System.out.println("解密php的des加密结果:"+decryptDataForPhp );
}
}
执⾏得到的结果
java加密结果:56F9E0D B4850341B1C734B93B5138EEA8F7A9432D8C5680CAE949C8C1F69B8A0 java解密结果:1638265429000qazxswedc951753
解密php的des加密结果:1638265429000qazxswedc951753
PHP加密Des的加密解密
$str="1638265429000qazxswedc951753";
$key="qwertyuiop123456";
//加密
$data=openssl_encrypt(str,'DES-ECB',$key,OPENSSL_RAW_DATA,'');
$encryptString=bin2hex($data);
var_dump("php加密结果:".encryptString );
$decryptString=openssl_decrypt(hex2bin(encryptString),'DES-ECB',$key,OPENSSL_RAW_DATA,''); var_dump("php解密结果:".decryptString );
$javaDes="56F9E0DB4850341B1C734B93B5138EEA8F7A9432D8C5680CAE949C8C1F69B8A0"; $ret=openssl_decrypt(hex2bin($javaDes),'DES-ECB',$key,OPENSSL_NO_PADDING,'');
var_dump("解密java的des结果:".$ret);
执⾏得到的结果
php加密结果:string(64)"56f9e0db4850341b1c734b93b5138eea8f7a9432d8c5680cc5ac9a286aceff04" php解密结果:string(28)"1638265429000qazxswedc951753"
解密java的des结果:string(32)"1638265429000qazxswedc951753"

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。