加密解密16進位用法:

來源:互聯網
上載者:User

標籤:

  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進位用法:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.