其實加密算法很多種,在開發過程中并不能全部用到,到目前,還沒用過復雜的對稱加密。基本上加密算法就下面一些,雖然還有好多沒研究過,也貼下來,當參考吧。
基本的單向加密算法:
復雜的對稱加密(DES、PBE)、非對稱加密算法:
package com.Gavin.tools.util;

import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
*
* @descripte DES coder
* @author Gavin.lee
* @date 2009-6-1 22:19:57
* @version 1.0
*/
public abstract class DESCoder{
public static final String ALGORITHM = "DES"; //指定算法為 DES
/**
* 轉換密鑰
*/
private static Key toKey(byte[] key) throws Exception {
DESKeySpec dks = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey secretKey = keyFactory.generateSecret(dks);
// 當使用其他對稱加密算法時,如AES、Blowfish等算法時,用下述代碼替換上述三行代碼
// SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);
return secretKey;
}
/**
* 解密
*/
public static byte[] decrypt(byte[] data, String key) throws Exception {
Key k = toKey(decryptBASE64(key));
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, k);
return cipher.doFinal(data);
}
/**
* 加密
*/
public static byte[] encrypt(byte[] data, String key) throws Exception {
Key k = toKey(decryptBASE64(key));
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, k);
return cipher.doFinal(data);
}
/**
* 生成密鑰
*/
public static String initKey() throws Exception {
return initKey(null);
}
/**
* 生成密鑰
*/
public static String initKey(String seed) throws Exception {
SecureRandom secureRandom = null;
if (seed != null) {
secureRandom = new SecureRandom(decryptBASE64(seed));
} else {
secureRandom = new SecureRandom();
}
KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM);
kg.init(secureRandom);
SecretKey secretKey = kg.generateKey();
return encryptBASE64(secretKey.getEncoded());
}
public static void main(String args[]) {
try {
String inputString = "doingjava"; //data
String key = DESCoder.initKey(); //key
byte[] inputData = inputString.getBytes();
inputData = DESCoder.encrypt(inputData, key); //encrypt
System.out.println(DESCoder.encryptBASE64(inputData));
byte[] outputData = DESCoder.decrypt(inputData, key); //decrypt
System.out.println(new String(outputData));
System.out.println(DESCoder.toKey(key.getBytes()).getAlgorithm()); //轉義算法密鑰
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* BASE64解密
*/
public static byte[] decryptBASE64(String key) throws Exception {
return (new BASE64Decoder()).decodeBuffer(key);
}
/**
* BASE64加密
*/
public static String encryptBASE64(byte[] key) throws Exception {
return (new BASE64Encoder()).encodeBuffer(key);
}
}
基本的單向加密算法:
- BASE64 嚴格地說,屬于編碼格式,而非加密算法
- MD5(Message Digest algorithm 5,信息摘要算法) ----非可逆加密
- SHA(Secure Hash Algorithm,安全散列算法) ----非可逆加密
- HMAC(Hash Message Authentication Code,散列消息鑒別碼) ----非可逆加密
復雜的對稱加密(DES、PBE)、非對稱加密算法:
- DES(Data Encryption Standard,數據加密算法)
- PBE(Password-based encryption,基于密碼驗證)
- RSA(算法的名字以發明者的名字命名:Ron Rivest, AdiShamir 和Leonard Adleman)
- DH(Diffie-Hellman算法,密鑰一致協議)
- DSA(Digital Signature Algorithm,數字簽名)
- ECC(Elliptic Curves Cryptography,橢圓曲線密碼編碼學)
非可逆加密就是不能解密,其實是不可靠加密。
DES加密:“ DES算法的入口參數有三個:Key、Data、Mode。其中Key為8個字節共64位,是DES算法的工作密鑰;Data也為8個字節64位,是要被加密或被解密的數據;Mode為DES的工作方式,有兩種:加密或解密,如果Mode為加密,則用Key去把數據Data進行加密,生成Data的密碼形式作為DES的輸出結果;如Mode為解密,則用Key去把密碼形式的數據Data解密,還原為Data的明碼形式作為DES的輸出結果。在使用DES時,雙方預先約定使用的”密碼”即Key,然后用Key去加密數據;接收方得到密文后使用同樣的Key解密得到原數據,這樣便實現了安全性較高的數據傳輸。
DES的工作原理為:將明文分割成許多64位大小的塊,每個塊用64位密鑰進行加密”
























































































































其實這個來自網上,這個比較適合我用,原址:http://snowolf.javaeye.com/blog/380034