标签:秘密密钥 secretkey 对称加密 des aes
在Java中,秘密密钥的抽象接口为javax.crypto.SecretKey,其算法类型为对称加密算法,对称加密算法的主要特点就是加密与解密用的是同一把密钥,对称加密算法主要有:DES,DESede,AES,Blowfish,RC2,RC4等。下面看一个使用例子:
package com.xtayfjpk.security; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.security.spec.KeySpec; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import org.junit.Test; public class SecretKeyTest { //对称加密算法,Java默认支持的算法有:AES、ARCFOUR、Blowfish、DES、DESede、HmacMD5、 //HmacSHA1 HmacSHA256 HmacSHA384 HmacSHA512、RC2 private static final String ALGORITHM = "DES"; @Test public void testEncrypt() throws Exception { //获取加/解密器 Cipher cipher = Cipher.getInstance(ALGORITHM); //生成秘密密钥 SecretKey key = KeyGenerator.getInstance(ALGORITHM).generateKey(); //将秘密写入文件中 writeSecretKey("xtayfjpk.key", key, false); //加密时,必须初始化为加密模式 cipher.init(Cipher.ENCRYPT_MODE, key); String myInfo = "我的明文"; //加密 byte[] results = cipher.doFinal(myInfo.getBytes()); writeFile("content.dat", results); } @Test public void testDecrypt() throws Exception { //读取秘密密钥 SecretKey key = readSecretKey("xtayfjpk.key", false); //读取加密后的数据 byte[] contents = readFile("content.dat"); Cipher cipher = Cipher.getInstance(ALGORITHM); //解密时,必须初始化为解密模式 cipher.init(Cipher.DECRYPT_MODE, key); //解密,得到明文 byte[] origins = cipher.doFinal(contents); System.out.println(new String(origins)); } /** 读取文件内容至字节数组中 **/ public byte[] readFile(String path) throws Exception { FileInputStream cntInput = new FileInputStream(path); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int b = -1; while((b=cntInput.read())!=-1) { baos.write(b); } cntInput.close(); byte[] contents = baos.toByteArray(); baos.close(); return contents; } /** 将二进制数据写入文件 **/ public void writeFile(String path, byte[] content) throws Exception { FileOutputStream fos = new FileOutputStream(path); fos.write(content); fos.close(); } public SecretKey readSecretKey(String path, boolean encoded) throws Exception { if(encoded) { byte[] keyData = readFile(path); KeySpec keySpec = new DESKeySpec(keyData); //当读取秘密密钥编码数据时须使用此方式生成秘密密钥 //Java文档中虽然明确说明SecretKeyFactory.getInstance支持AES算法,但事实上却不支持(JDK7) SecretKey key = SecretKeyFactory.getInstance(ALGORITHM).generateSecret(keySpec); return key; } else { FileInputStream fis = new FileInputStream(path); ObjectInputStream bis = new ObjectInputStream(fis); Object object = bis.readObject(); bis.close(); return (SecretKey) object; } } /** 秘密密钥写入文件有两种方式,一是利用Java对象的序列化机制,二是获取其编码数据(字节数据)写入文件 **/ public void writeSecretKey(String path, SecretKey secretKey, boolean encoded) throws Exception { FileOutputStream fos = new FileOutputStream(path); if(encoded) { fos.write(secretKey.getEncoded()); fos.close(); } else { ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(secretKey); oos.close(); } } }
标签:秘密密钥 secretkey 对称加密 des aes
原文地址:http://blog.csdn.net/xtayfjpk/article/details/46007887