资源简介
RSA与AES混合加密算法的实现http://blog.csdn.net/jkxqj/article/details/25228707
代码片段和文件信息
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 javax.crypto.spec.SecretKeySpec;
public abstract class AESCoder extends Coder {
/**
* ALGORITHM 算法
* 可替换为以下任意一种算法,同时key值的size相应改变。
* DES key size must be equal to 56
* DESede(TripleDES) key size must be equal to 112 or 168
* AES key size must be equal to 128 192 or 256but 192 and 256 bits may not be available
* Blowfish key size must be multiple of 8 and can only range from 32 to 448 (inclusive)
* RC2 key size must be between 40 and 1024 bits
* RC4(ARCFOUR) key size must be between 40 and 1024 bits
* 在Key toKey(byte[] key)方法中使用下述代码
* SecretKey secretKey = new SecretKeySpec(key ALGORITHM);替换
* DESKeySpec dks = new DESKeySpec(key);
* SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
* SecretKey secretKey = keyFactory.generateSecret(dks);
*/
public static final String ALGORITHM = “AES“;
/**
* 转换密钥
*
* @param key
* @return
* @throws Exception
*/
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;
}
/**
* 解密
*
* @param data
* @param key
* @return
* @throws Exception
*/
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);
}
/**
* 加密
*
* @param data
* @param key
* @return
* @throws Exception
*/
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);
}
/**
* 生成密钥
*
* @return
* @throws Exception
*/
public static String initKey() throws Exception {
return initKey(null);
}
/**
* 生成密钥
*
* @param seed
* @return
* @throws Exception
*/
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());
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 301 2014-05-07 16:32 RSAwithAES\.classpath
文件 386 2014-05-07 16:32 RSAwithAES\.project
文件 629 2014-05-07 16:32 RSAwithAES\.settings\org.eclipse.jdt.core.prefs
文件 2002 2014-05-07 16:55 RSAwithAES\bin\AESCoder.class
文件 2121 2014-05-07 16:54 RSAwithAES\bin\Coder.class
文件 4949 2014-05-07 16:54 RSAwithAES\bin\RSACoder.class
文件 2353 2014-05-07 16:55 RSAwithAES\bin\RSAwithAESTest.class
文件 3055 2014-05-07 16:55 RSAwithAES\src\AESCoder.java
文件 2162 2014-05-07 15:46 RSAwithAES\src\Coder.java
文件 6324 2014-05-07 16:24 RSAwithAES\src\RSACoder.java
文件 1580 2014-05-07 16:55 RSAwithAES\src\RSAwithAESTest.java
目录 0 2014-05-07 16:56 RSAwithAES\.settings
目录 0 2014-05-07 16:56 RSAwithAES\bin
目录 0 2014-05-07 16:56 RSAwithAES\src
目录 0 2014-05-07 16:56 RSAwithAES
----------- --------- ---------- ----- ----
25862 15
- 上一篇:Java web 大作业
- 下一篇:Java加密与解密的艺术.rar 完整源代码
评论
共有 条评论