1.Base64做用
編碼和解碼,
兼容一些老系統 好比郵件發送算法
//按鈕事件
public void base64EncodeDemo(View view) {
String content = mEt.getText().toString();
//使用base64進行編碼
String encode_content = Base64.encodeToString(content.getBytes(),Base64.DEFAULT);
mEt.setText(encode_content);
}
public void base64DecodeDemo(View view) {
String content = mEt.getText().toString();
//使用base64進行解碼
byte[] decodeContent = Base64.decode(content.getBytes(),Base64.DEFAULT);
mEt.setText(new String(decodeContent));
}
2.md5
單項加密
16進制字符串的個數32
校驗傳輸內容
public void md5MessageDigest(View view) {
try {
//獲取對應算法的加密實體
MessageDigest md = MessageDigest.getInstance("MD5");
//設置要加密的文本
md.update(mEt.getText().toString().getBytes());
//生成摘要
byte[] digest = md.digest();
bytes2Hex(digest);
//10010101
//00000000 11111111
//00000000 10010101
//255
// byte & byte[2] 00000000 11111111
//00000000 10010101
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
private String bytes2Hex(byte[] data){
StringBuffer buffer = new StringBuffer();
int v = 0xa;
for (int i = 0; i < data.length; i++) {
// 10101010
//00000000 00000000 00000000 11111111
//00000000 00000000 00000000 10101010
int value = data[i] & 255;
//
//value =17 11
//value =10 a;
//10
//16進制的表現形式 0a
System.out.println("value="+value);
String temp = "";
if (value<16){
temp = "0"+Integer.toHexString(value);
}else {
temp = Integer.toHexString(value);
}
buffer.append(temp);
}
System.out.println("hexString="+buffer.toString());
System.out.println("hexStringLen="+buffer.toString().length());
return buffer.toString();
}
3.sha 高級的單項加密算法
sha-1 40位
sha-224 56位
sha-256 64位
sha-384 96位
sha-512 128位
try {
MessageDigest messageDigest =MessageDigest.getInstance("SHA-1");
messageDigest =MessageDigest.getInstance("SHA-224");
messageDigest =MessageDigest.getInstance("SHA-256");
messageDigest =MessageDigest.getInstance("SHA-384");
messageDigest =MessageDigest.getInstance("SHA-512");
messageDigest.update(mEt.getText().toString().getBytes());
byte[] result = messageDigest.digest();
String hexString = bytes2Hex(result);
((TextView)findViewById(R.id.tv_digest)).setText(hexString);app