加密解密16进制用法:

  1.Base64 :  26个英文字母大小写+10个阿拉伯数字+“+ =”  一共64个;

  2.3DES:要求秘钥 为24位的

      

public static byte[] encrypt(byte[] key,byte[] data){
SecretKey secretKey=new SecretKeySpec(key, "DESede");
try {
Cipher cipher=Cipher.getInstance("DESede");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return cipher.doFinal(data);

} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static byte[]decrypt(byte[]key,byte[]data){
SecretKey secretKey=new SecretKeySpec(key, "DESede");
try {
Cipher cipher=Cipher.getInstance("DESede");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return cipher.doFinal(data);

} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

    16进制转换:

public static String toHexString(byte[] data, String seperator){
StringBuilder sb = new StringBuilder(data.length * (2 + seperator.length()));

int pb = 0;
String pre = "";
while(pb < data.length){
byte b = data[pb++];

int h = ((0xF0 & b) >> 4);
int l = (0x0F & b);

sb.append(pre);
if(h < 10){
sb.append((char)('0' + h));
}else{
sb.append((char)('a' + h - 10));
}
if(l < 10){
sb.append((char)('0' + l));
}else{
sb.append((char)('a' + l - 10));
}
pre = seperator;

}

return sb.toString();
}

/**
* 将十六进制字符串表示的字节数据还原成字节数组
* @param text 被还原的字符串
* @return 还原之后的字节数组
*/
public static byte[] fromHexString(String text){
if(text == null)
return new byte[0];

byte[] result = new byte[text.length() / 2];

text = text.toLowerCase();
int pb = 0;
int ps = 0;
while(pb < result.length && ps < text.length()){
char ch = text.charAt(ps++);
char cl = text.charAt(ps++);

int a = 0;
int b = 0;
if('0' <= ch && ch <= '9'){
a = ch - '0';
}else{
a = ch - 'a' + 10;
}
if('0' <= cl && cl <= '9'){
b = cl - '0';
}else{
b = cl - 'a' + 10;
}

result[pb++] = (byte)((a << 4) + b);
}

return result;
}

     

原文地址:https://www.cnblogs.com/xxwn/p/4633792.html