学习笔记,JAVA 3DES 加解密学习
引入 bouncycastle-jce-jdk13-112.jar [ 在我的资源文件中可以进行下载 ]
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class Double3DES {
public static byte[] strToBCDBytes(String str,boolean isLeft) {
if( str == null || "".equals(str.replaceAll(" "," ").trim())){
return null;
}
int len = str.length();
int mod = len % 2;
if (mod != 0) {
if(isLeft){
str = str + "0";
}else{
str = "0" + str;
}
len = str.length();
}
byte abt[] = new byte[len];
if (len >= 2) {
len = len / 2;
}
byte bbt[] = new byte[len];
abt = str.getBytes();
int j, k;
for (int p = 0; p < str.length()/2; p++) {
if ( (abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
j = abt[2 * p] - '0';
} else if ( (abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
j = abt[2 * p] - 'a' + 0x0a;
} else {
j = abt[2 * p] - 'A' + 0x0a;
}
if ( (abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
k = abt[2 * p + 1] - '0';
} else if ( (abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
k = abt[2 * p + 1] - 'a' + 0x0a;
}else {
k = abt[2 * p + 1] - 'A' + 0x0a;
}
int a = (j << 4) + k;
byte b = (byte) a;
bbt[p] = b;
}
return bbt;
}
private static byte uniteBytes(String src0, String src1) {
byte b0 = Byte.decode("0x" + src0).byteValue();
b0 = (byte) (b0 << 4);
byte b1 = Byte.decode("0x" + src1).byteValue();
byte ret = (byte) (b0 | b1);
return ret;
}
/**
* 十六进制字符串转换成bytes
*
* @param src
* @return
*/
public static byte[] hexStr2Bytes(String src) {
int m = 0, n = 0;
int l = src.length() / 2;
byte[] ret = new byte[l];
for (int i = 0; i < l; i++) {
m = i * 2 + 1;
n = m + 1;
ret[i] = uniteBytes(src.substring(i * 2, m), src.substring(m, n));
}
return ret;
}
public static String byte2HexStr(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp;
}
return hs.toUpperCase();
}
/**
*
* @param mainKey 主密钥
* @param plainKey
* @param isEncry true 表示加密 false 表示解密
* @return
*/
public static String desOrEncryptKey(String mainKey,String plainKey,boolean isEncry){
String descryptKey = "";
try{
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
String Algorithm = "DESede/ECB/NoPadding";
byte[] hb = strToBCDBytes(mainKey, true);
byte[] k = new byte[24];
System.arraycopy(hb,0,k,0,16);
System.arraycopy(hb,0,k,16,8);
SecretKey deskey = new SecretKeySpec(k, Algorithm);
Cipher c1 = Cipher.getInstance(Algorithm);
if(isEncry){
c1.init(Cipher.ENCRYPT_MODE, deskey);
}else{
c1.init(Cipher.DECRYPT_MODE, deskey);
}
descryptKey = byte2HexStr((c1.doFinal(hexStr2Bytes(plainKey))));
}catch(Exception e){
e.printStackTrace();
}
return descryptKey;
}
/**
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
//加密
System.out.println("\tencryp:::: "+desOrEncryptKey("11111113111111710123456789ABCDEF","ADF55D08B48B18E3BDF55D08D48B18E3",true));
//解密
System.out.println("\tdescryp:::: "+desOrEncryptKey("11111113111111710123456789ABCDEF","ADF55D08B48B18E3BDF55D08D48B18E3",false));
}
}