admin 管理员组

文章数量: 887021


2024年2月19日发(作者:商业分析认证)

Triple Des 加密解密 封装了byte数组和16进制字符串互相转换的操纵 使用起来更方便。

import ;

import Key;

import KeySpec;

public class TriDES {

private static final String Algorithm = "DESede";

public static String encryptMode(byte[] keybyte, String src) {

try{

SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);

Cipher c1 = tance(Algorithm);

(T_MODE, deskey);

return byte2Hex(l(es()));

}catch (Exception e) {

tackTrace();

//sage();

}

return null;

}

public static String decryptMode(byte[] keybyte, String src) {

try{

SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);

Cipher c1 = tance(Algorithm);

(T_MODE, deskey);

return new String(l(hex2Byte(src)));

}catch (Exception e) {

tackTrace();

//sage();

}

return null;

}

public static String byte2Hex(byte[] b) {

String hs = "";

String stmp = "";

for (int n = 0; n < ; n++) {

stmp = (tring(b[n] & 0XFF));

if (() == 1) {

hs = hs + "0" + stmp;

} else {

hs = hs + stmp;

}

}

return rCase();

}

public static byte[] hex2Byte(String str) {

if (str == null)

return null;

str = ();

int len = ();

if (len == 0 || len % 2 == 1)

return null;

byte[] b = new byte[len / 2];

try {

for (int i = 0; i < (); i += 2) {

b[i / 2] = (byte) Integer

.decode("0x" + ing(i, i + 2)).intValue();

}

return b;

} catch (Exception e) {

return null;

}

}

public static void main(String[] args) {

final byte[] keyBytes = { 0x11, 0x22, 0x4F, 0x58, (byte) 0x88, 0x10,

0x40, 0x38, 0x28, 0x25, 0x79, 0x51, (byte) 0xCB, (byte) 0xDD,

0x55, 0x66, 0x77, 0x29, 0x74, (byte) 0x98, 0x30, 0x40, 0x36,

(byte) 0xE2 };

String szSrc = "This is a 3DES test. 测试";

n("加密前的字符串:" + szSrc);

String encoded = encryptMode(keyBytes, szSrc);

n("加密后的字符串:" + encoded);

String srcBytes = decryptMode(keyBytes, encoded);

n("解密后的字符串:" + srcBytes);

srcBytes = decryptMode(keyBytes,

"46872E6C5EDA9142D7C73208D2ACD0CB7183D0BA542DA373");

n("加密字符串直接解密后的字符串:" + srcBytes);

}

}


本文标签: 字符串 操纵 转换 加密 认证