使用过可行的
工具一:只能AES加解密,key要16位
package com.rujian.passwordmanager.util;
import android.util.Base64;import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class AESEncryptUtil {
/*
* 加密用的Key 可以用26个字母和数字组成
* 此处使用AES-128-CBC加密模式,key需要为16位。
*/
private String sKey="0123456789abcdef";
private String ivParameter="0123456789abcdef";
private static AESEncryptUtil aesEncryptUtil = null;
private AESEncryptUtil(){}
public static AESEncryptUtil getInstance(){
if(aesEncryptUtil == null){
aesEncryptUtil = new AESEncryptUtil();
return aesEncryptUtil;
}else{
return aesEncryptUtil;
}
}
// 加密
public String encrypt(String sSrc) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] raw = sKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());//使用CBC模式,需要一个向量iv,可增加加密算法的强度
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
return Base64.encodeToString(encrypted, Base64.DEFAULT);//此处使用BASE64做转码。
}
// 解密
public String decrypt(String sSrc) throws Exception {
try {
byte[] raw = sKey.getBytes("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] encrypted1 = Base64.decode(sSrc, Base64.DEFAULT);//先用base64解密
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original,"utf-8");
return originalString;
} catch (Exception ex) {
return null;
}
}
}
工具二:可以选择DES、DESede 或 AES 方式
package com.rujian.passwordmanager.util;
import android.util.Base64;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
public class SymEncrypt {
public static Key getKey(byte[] arrBTmp, String alg) {
if (!(alg.equals("DES") || alg.equals("DESede") || alg.equals("AES"))) {
System.out.println("alg type not find: " + alg);
return null;
}
byte[] arrB;
if (alg.equals("DES")) {
arrB = new byte[8];
} else if (alg.equals("DESede")) {
arrB = new byte[24];
} else {
arrB = new byte[16];
}
int i = 0;
int j = 0;
while (i < arrB.length) {
if (j > arrBTmp.length - 1) {
j = 0;
}
arrB[i] = arrBTmp[j];
i++;
j++;
}
Key key = new javax.crypto.spec.SecretKeySpec(arrB, alg);
return key;
}
/**
* 加密
*
* @param s 待加密的字符串
* @param strKey key
* @param alg 加密类型 (DES,DESede,AES)
* @return
*/
public static String encrypt(String s, String strKey, String alg) {
if (!(alg.equals("DES") || alg.equals("DESede") || alg.equals("AES"))) {
System.out.println("alg type not find: " + alg);
return null;
}
byte[] r = null;
try {
Key key = getKey(strKey.getBytes(), alg);
Cipher c;
c = Cipher.getInstance(alg);
c.init(Cipher.ENCRYPT_MODE, key);
r = c.doFinal(s.getBytes("utf-8"));
//System.out.println("加密后的二进串:" + FileDigest.byte2Str(r));
} 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();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return Base64.encodeToString(r, Base64.DEFAULT);//此处使用BASE64做转码。;
}
/**
* 解密
*
* @param code 待解密的byte数组
* @param strKey key
* @param alg 加密类型 (DES,DESede,AES)
* @return
*/
public static String decrypt(String code, String strKey, String alg) {
if (!(alg.equals("DES") || alg.equals("DESede") || alg.equals("AES"))) {
System.out.println("alg type not find: " + alg);
return null;
}
String r = null;
try {
Key key = getKey(strKey.getBytes(), alg);
Cipher c;
c = Cipher.getInstance(alg);
c.init(Cipher.DECRYPT_MODE, key);
byte[] encrypted1 = Base64.decode(code, Base64.DEFAULT);//先用base64解密
byte[] clearByte = c.doFinal(encrypted1);
r = new String(clearByte,"utf-8");
} 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
System.out.println("not padding");
r = null;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//System.out.println("解密后的信息:"+r);
return r;
}
}