日期:2014-05-20 浏览次数:21306 次
public static void main(String[] args) throws Exception { String key = "test"; String src = "111"; SecretKey deskey = null; String Algorithm = "DESede"; // 加密算法 KeyGenerator gen = KeyGenerator.getInstance(Algorithm); //实例化(对称加密)密钥生成器 //或用做"IBMSecureRandom",提供强加密随机数生成器 SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); secureRandom.setSeed(key.getBytes());// 重新设置此随机对象的种子。 deskey = gen.generateKey(); //生成 密钥 gen = null;//将密钥生成器置空,gc回收 System.out.println("密钥:" + deskey.toString()); //============================================= 生成密钥 //实例化(为加密和解密提供密码功能) Cipher c1 = Cipher.getInstance(Algorithm + "/ECB/PKCS5Padding"); c1.init(Cipher.ENCRYPT_MODE, deskey); //设置加密模式 byte[] encoded = c1.doFinal(src.getBytes("UTF-8")); //执行加密 //import org.bouncycastle.util.encoders.Base64; String destCode = new String(Base64.encode(encoded)); //Base64编码 密文 //============================================= 生成密文 System.out.println("密文:"+destCode.toString()); }