package aosa.safemedia.util; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; import java.security.Key; import java.security.NoSuchAlgorithmException; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.KeyGenerator; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class Encryption { private void createKey() { try { // 得到密钥的实例 以什么方式加密。加密的方式比较多。 KeyGenerator kg = KeyGenerator.getInstance("DES"); kg.init(56); SecretKey key = kg.generateKey(); // 将生成的密钥对象写入文件。 ObjectOutputStream objectOutputStream = new ObjectOutputStream( new FileOutputStream(new File("e:\\key.obj"))); objectOutputStream.writeObject(key); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private static Key getKey(String KeyFilePath) { Key key = null; try { // 将生成的密钥对象从文件中读取出来,然后再强制转换成一个密钥对象。 ObjectInputStream objectInputStream = new ObjectInputStream( new FileInputStream(new File(KeyFilePath))); key = (Key) objectInputStream.readObject(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return key; } public byte[] encrypt(String source) { byte[] target = null; try { byte[] center = source.getBytes("UTF-8"); Key key = getKey("e:\\key.obj"); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, key); target = cipher.doFinal(center); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return target; } public byte[] decrypt(byte[] source) { byte[] dissect = null; try { Key key = getKey("e:\\key.obj"); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE, key);// 使用私钥解密 dissect = cipher.doFinal(source); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return dissect; } public void encodeByteToFile(byte[] bytes){ BASE64Encoder base64encoder = new BASE64Encoder(); try { base64encoder.encode(bytes,new FileOutputStream(new File("D:\\t.txt"))); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public byte[] getByteFromFile(){ BASE64Decoder base64decoder = new BASE64Decoder(); byte[] encodeByte = null; try { encodeByte = base64decoder.decodeBuffer(new FileInputStream(new File("D:\\t.txt"))); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return encodeByte; } public void writeByteToFile(byte[] b, String filePath) { File file = new File(filePath); if (!file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } FileOutputStream fileOutputStream; try { fileOutputStream = new FileOutputStream(file); fileOutputStream.write(b); fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } //测试使用。 // public static void main(String[] args) { // Encryption encryption = new Encryption(); // //encryption.dateEncrypt(encryption.encrypt("yangtao")); // System.out.println(new String(encryption.decrypt(encryption.getByteFromFile()))); // } }