经常使用加密算法:DES、3DES、RC4、AES,RSA等;
对称加密:des,3des,aes
非对称加密:rsa
不可逆加密:md5
加密模式:ECB、CBC、CFB、OFB等;
填充模式:NoPadding、PKCS1Padding、PKCS5Padding、PKCS7Padding
package com.changhong.settings.iptv.util;
import android.text.TextUtils;
import android.util.Base64;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/**
* Created by libeibei on 2018/3/9 0009.
*/
public class EncryptionTool {
/**
* MD5加密
* @param str
* @return
*/
public static final String getMD5String(String str) {
char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F'};
try {
byte[] btInput = str.getBytes();
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
messageDigest.update(btInput);
//获得密文
byte[] cipherbyte = messageDigest.digest();
int len = cipherbyte.length;
char string[] = new char[len * 2];
int k = 0;
for (int i = 0; i < len; i++) {
byte byte0 = cipherbyte[i];
string[k++] = hexDigits[byte0 >>> 4 & 0xf];
string[k++] = hexDigits[byte0 & 0xf];
}
return new String(string);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
// 定义加密算法,DESede即3DES
private static final String Algorithm = "DESede";
private static final String Key = "123456781234567812345678";
/**
* 3DES加密
* @param str
* @return
*/
public static String get3DesString(String str){
if(!TextUtils.isEmpty(str)){
byte[] enBytes = encryptMode(Key, str.getBytes());
return Base64.encodeToString(enBytes,Base64.DEFAULT);
}else{
return null;
}
}
/**
* 加密
* @param key
* @param src
* @return
*/
private static byte[] encryptMode(String key, byte[] src) {
try {
SecretKey deskey = new SecretKeySpec(build3DesKey(key), Algorithm);
Cipher cipher = Cipher.getInstance(Algorithm);
cipher.init(Cipher.ENCRYPT_MODE, deskey);
return cipher.doFinal(src);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 3DES解密
* @return
*/
public static String getDe3DesString(String str){
if(!TextUtils.isEmpty(str)){
byte[] enBytes = Base64.decode(str,Base64.DEFAULT);
byte[] deBytes = decryptMode(Key, enBytes);
return new String(deBytes);
}else{
return null;
}
}
/**
* 解密
* @param key
* @param src
* @return
*/
private static byte[] decryptMode(String key, byte[] src) {
try {
SecretKey deskey = new SecretKeySpec(build3DesKey(key), Algorithm);
Cipher cipher = Cipher.getInstance(Algorithm);
cipher.init(Cipher.DECRYPT_MODE, deskey);
return cipher.doFinal(src);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 根据字符串生成密钥24位的字节数组
* @param keyStr
* @return
* @throws UnsupportedEncodingException
*/
private static byte[] build3DesKey(String keyStr) throws UnsupportedEncodingException {
byte[] key = new byte[24];
byte[] temp = keyStr.getBytes("UTF-8");
if (key.length > temp.length) {
System.arraycopy(temp, 0, key, 0, temp.length);
} else {
System.arraycopy(temp, 0, key, 0, key.length);
}
return key;
}
String authenticator = null;
try {
authenticator = byte2hex(des3EncodeECB(password.getBytes(), authStr.getBytes()));
sb = new StringBuffer(authenticator);
} catch (Exception e) {
e.printStackTrace();
}
Log.i(TAG, "----->>>>>-----加密后的 authenticator string = " + sb.toString());
private String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp;
if (n < b.length - 1)
hs = hs + "";
}
return hs.toUpperCase();
}
/**
* 3DES加密,湖北移动项目IPTV平台账号密码加密使用
* @param key data
* @return
*/
private byte[] des3EncodeECB(byte[] key, byte[] data) throws Exception {
Key deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(key);
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
deskey = keyfactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, deskey);
byte[] bOut = cipher.doFinal(data);
return bOut;
}
}