1.對稱加密
AES 加密
對稱密鑰加密中最流行的算法之一,Advanced Encryption Standard,安全級別不高的自動登錄一般用AES加密,AES加密是比較快的。
動畫演示:http://coolshell.cn/articles/3161.html
public class AES {
/**
* 填充字符串,總長爲16的倍數,AES加密算法要求 -- 此方法設爲私有即可
* @param str
* @return
*/
private static String paddingString(String str) {
int len = str.length();
if (0 == len % 16) {
return str;
}
byte[] bytes = new byte[(16 * (len / 16)) + 16];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = '\0';
}
try {
byte[] strbytes = str.getBytes("UTF-8");
for (int i = 0; i < len; i++) {
bytes[i] = strbytes[i];
}
return new String(bytes);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
/**
* 加密
* @param sSrc
* @param sKey
* @return
* @throws Exception
*/
public static byte[] encrypt(String sSrc, String sKey) throws Exception {
if (sKey == null) {
return null;
}
if (sKey.length() != 16) {
return null;
}
String encryptstr = paddingString(sSrc);
byte[] raw = sKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(encryptstr.getBytes());
return encrypted;
}
/**
* 解密
* @param sSrc
* @param sKey
* @return
* @throws Exception
*/
public static String decrypt(byte[] sSrc, String sKey) throws Exception {
try {
if (sKey == null) {
return null;
}
if (sKey.length() != 16) {
return null;
}
byte[] raw = sKey.getBytes("UTF-8");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
try {
byte[] original = cipher.doFinal(sSrc);
String originalString = new String(original);
return originalString;
} catch (Exception e) {
return null;
}
} catch (Exception ex) {
return null;
}
}
}
1). RSA 非對稱加密,在公開密鑰加密和電子商業中RSA被廣泛使用,如github 傳輸到服務器的代碼用的ssl 就是 rsa加密,RSA加密是比較慢的。
公鑰:加密
私鑰:解密
http://blog.csdn.net/bbld_/article/details/38777491
2). MD5 散列碼
public static String md5(String string) {
byte[] hash;
try {
hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Huh, MD5 should be supported?", e);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Huh, UTF-8 should be supported?", e);
}
StringBuilder hex = new StringBuilder(hash.length * 2);
for (byte b : hash) {
if ((b & 0xFF) < 0x10) hex.append("0");
hex.append(Integer.toHexString(b & 0xFF));
}
return hex.toString();
}
3) BASE 64
android Base64加密解密,androidbase64
// 加密傳入的數據是byte類型的,並非使用decode方法將原始數據轉二進制,String類型的數據 使用 str.getBytes()即可
String str = "Hello!";
// 在這裏使用的是encode方式,返回的是byte類型加密數據,可使用new String轉爲String類型
String strBase64 = new String(Base64.encode(str.getBytes(), Base64.DEFAULT));
Log.i("Test", "encode >>>" + strBase64);
// 這裏 encodeToString 則直接將返回String類型的加密數據
String enToStr = Base64.encodeToString(str.getBytes(), Base64.DEFAULT);
Log.i("Test", "encodeToString >>> " + enToStr);
// 對base64加密後的數據進行解密
Log.i("Test", "decode >>>" + new String(Base64.decode(strBase64.getBytes(), Base64.DEFAULT)));
java base64解密亂碼問題 代碼如下:
private String getPictureString() { String upload = ""; try { FileInputStream in = new FileInputStream(fileName); byte[] ba = new byte[in.available()]; in.read(ba); in.close(); upload = new String(android.util.Base64.encode(ba, android.util.Base64.DEFAULT)); } catch (FileNotFoundException e) { LogUtil.e(e.getMessage(), e); } catch (IOException e) { LogUtil.e(e.getMessage(), e); } return upload; }
這個是加密
解密就是
encode換成decode
upload = new String(android.util.Base64.decode(ba,
android.util.Base64.DEFAULT));
對於base64、md5等加密解密問題
base64的作用不是加密,而是用來避免“字節”中不能轉換成可顯示字符的數值。
比如0-32的控制字符,空格,製表符都不能被打印在紙上,base64只使用大寫小寫數字標點。
可以打印在紙上,數據可以在傳統平面媒介上攜帶。
md5是散列函數,提取數據的特徵,輸出是不可逆的散列值,用於代表某信息A而又不暴露信息A的內容。不直接用於加密文件。