加密解密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 Instance("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 Instance("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;
}
String();
}
/**
* 将⼗六进制字符串表⽰的字节数据还原成字节数组* @param text 被还原的字符串
* @return 还原之后的字节数组
*/
public static byte[] fromHexString(String text){
if(text == null)
return new byte[0];
byte[] result = new byte[text.length() / 2];
text = LowerCase();
int pb = 0;generated
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;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论