標籤:
1.Base64 : 26個英文字母大小寫+10個阿拉伯數字+“+ =” 一共64個;
2.3DES:
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;
}
加密解密16進位用法: