首页 > 编程知识 正文

des算法密钥,vue和java使用des加解密

时间:2023-05-06 18:50:25 阅读:163794 作者:3610

1、DES算法:

Java代码集合代码

//*

密码破译算法

@param data加解密数据

@param key私钥

@param mode模式

@return加解密结果

*/

公共静态字节[ ] descryt (byte [ ] data,byte[] key,int mode ) {

byte[] result=null;

try {

SecureRandom sr=new SecureRandom (;

安全密钥密钥;

eskeyspecdks=newdeskeyspec(key );

keyfactory=secret keyfactory.getinstance (' des );

secret key secret key=keyfactory.generate secret (dks;

创建Cipher对象

cipher cipher=cipher.getinstance (des/ECB/no padding );

初始化Cipher对象

cipher.init(mode,secretkey,sr );

//加解密

result=cipher.dofinal(data );

} catch (nosuchalgorithmexceptione ) )

e .打印任务跟踪(;

} catch (invalidkeyspecexceptione ) )

e .打印任务跟踪(;

}catch(nosuchpaddingexceptione ) )

e .打印任务跟踪(;

}catch(invalidkeyexceptione ) )

e .打印任务跟踪(;

} catch (illegalblocksizeexceptione ) ) )。

e .打印任务跟踪(;

}catch(badpaddingexceptione ) {

e .打印任务跟踪(;

}

返回结果;

}

2、将byte数组转换为十六进制字符串

Java代码集合代码

//*

将byte数组转换为十六进制字符串

@param b

@return

*/

ublicstaticstringbytes2hex string {

String ret=' ';

for(intI=0; i b.length; I ) {

string hex=integer.to hex string (b [ I ]0x ff );

if(hex.length ()==1) {

hex='0' hex;

}

ret =hex.toUpperCase (;

}

返回;

3、将十六进制字符串转换为byte数组

Java代码集合代码

//*

将十六进制字符串转换为byte数组

@param src

@return

/

ublicstaticbyte [ ] hex string2bytes {

byte[] ret=new byte[8];

byte[] tmp=src.getBytes (;

for(intI=0; i8; I ) {

ret[I]=unitebytes(tmp[I2],tmp[i*2 1];

}

返回;

}

Java代码集合代码

publicstaticbyteunitebytes (byte src 0,byte src1) {

byte _ B0=byte.decode ('0x ' new string ) newbyte[]{src0} ).byteValue );

_B0=(byte ) ) _b0 4;

byte _ B1=byte.decode ('0x ' new string ) newbyte[]{src1} ).byteValue );

byteret=(byte(_B0^_B1 );

返回;

}

的双曲正弦值

Java代码集合代码

publicstaticvoidmain (string [ ] args ) {

//解密模式

int mode=Cipher.ENCRYPT_MODE;

//解密的byte数组的十六进制字符串

string data hex string=' 1234567887654321 ';

//私钥byte数组十六进制字符串

string key hex string=' 9aa B1 d2ee 004 AAC3';

byte [ ] data=hex string2bytes (data hex string );

byte [ ] key=hex string2bytes (key hex string );

byte[]result=descryt(data,key,mode );

//打印结果

System.out.println (结果:“bytes2hexstring(result )”

}

结果: 7D592BF239849E76

的双曲正弦值

Java代码集合代码

publicstaticvoidmain (string [ ] args ) {

//解密模式

int mode=Cipher.DECRYPT_MODE;

//解密的byte数组的十六进制字符串

string data hex string=' 7d 592 BF 239849 e 76 ';

//私钥byte数组十六进制字符串

string key hex string=' 9aa B1 d2ee 004 AAC3';

byte [ ] data=hex string2bytes (data hex string );

byte [ ] key=hex string2bytes (key hex string );

byte[]result=descryt(data,key,mode );

//打印结果

System.out.println (结果:“bytes2hexstring(result )”

}

结果: 1234567887654321

PS :

获取Cipher对象时请务必写入

cipher cipher=cipher.getinstance (des/ECB/no padding );

不要写

cipher cipher=cipher.getinstance (' des );

否则,解密时会报告错误:

given final block not properly padded

原因是cipher cipher=cipher.getinstance (' des ); cipher cipher=cipher.getinstance (des/ECB/pkcs5padding ); 同样,如果填充方法错误,加密时会得到16个长度的字节数组。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。