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);
}
}
版权声明:本文标题:Java中3DES加密解密示例(封装byte数组16进制互转) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/free/1708342101h520576.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论