AES加密分两大块来说吧(仅在客户端加解密和客户端加密服务端解密),
(一)针对于android客户端加解密来说,比如登陆时候将用户名和密码存储到sharedpreferences 中对于解密位数没有要求,8位、16位、32位都可以成功加解密代码如下(可以直接作为一个utils封装成一个jar来调用):(二)
public class AESUtil {
public static String encrypt(String seed, String cleartext)
throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] result = encrypt(rawKey, cleartext.getBytes());
return toHex(result);
}
public static String decrypt(String seed, String encrypted)
throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] enc = toByte(encrypted);
byte[] result = decrypt(rawKey, enc);
return new String(result);
}
private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
sr.setSeed(seed);
kgen.init(128, sr);
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
return raw;
}
private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(
new byte[cipher.getBlockSize()]));
byte[] encrypted = cipher.doFinal(clear);
return encrypted;
}
private static byte[] decrypt(byte[] raw, byte[] encrypted)
throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(
new byte[cipher.getBlockSize()]));
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}
@SuppressWarnings("unused")
private static String toHex(String txt) {
return toHex(txt.getBytes());
}
@SuppressWarnings("unused")
private static String fromHex(String hex) {
return new String(toByte(hex));
}
private static byte[] toByte(String hexString) {
int len = hexString.length() / 2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++)
result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2),
16).byteValue();
return result;
}
private static String toHex(byte[] buf) {
if (buf == null)
return "";
StringBuffer result = new StringBuffer(2 * buf.length);
for (int i = 0; i < buf.length; i++) {
appendHex(result, buf[i]);
}
return result.toString();
}
private final static String HEX = "0123456789ABCDEF";
private static void appendHex(StringBuffer sb, byte b) {
sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
}
}
代码解析如下:
主要看加密encryp()和解密decrypt()方法;参数一可以选用手机唯一的id作为key因为每个手机都有唯一的识别key通过此加密不容易被破解。参数二则为需要加密的内容。获取手机识别key的id方法如下:
public class GetTelphoneInfo {
public static String getImsi(Context context) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String _imsi = tm.getSubscriberId();
if(_imsi != null && !_imsi.equals("")){
return _imsi;
}
return "未知";
}
}
(二) android客户端加密,java服务端解密
和以上android客户端加解密唯一不同的是:加解密的内容需要16位的字符。因为这个问题,把8位,16位,32位的加解密都试了一遍,结果只有16位的可以(android客户端加密,java服务端解密内容一致)加密机制为16的倍数。客户端加密代码如下:
/** * * @param source 被加密的字符串 * @param key 加密key * @return 加密过后的字符串 */ public static String encrypt4AES(String source, String key) { try { byte[] iv = key.substring(0,16).getBytes("utf-8"); IvParameterSpec zeroIv = new IvParameterSpec(iv); SecretKeySpec key1 = new SecretKeySpec(iv, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, key1, zeroIv); byte[] encryptedData = cipher.doFinal(source.getBytes("utf-8")); String encryptResultStr = parseByte2HexStr(encryptedData); return encryptResultStr; // 加密 } catch (Exception e) { e.printStackTrace(); return ""; } }