java 文件加密解密
时间:2014-04-29 11:35:47
收藏:0
阅读:520
1 package com.test; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileOutputStream; 6 import java.io.InputStream; 7 import java.io.OutputStream; 8 import java.security.KeyPair; 9 import java.security.KeyPairGenerator; 10 import java.security.NoSuchAlgorithmException; 11 import java.security.interfaces.RSAPrivateKey; 12 import java.security.interfaces.RSAPublicKey; 13 import javax.crypto.Cipher; 14 15 public class RSAEncrypt { 16 17 static KeyPairGenerator keyPairGen; 18 19 static KeyPair keyPair; 20 21 static RSAPrivateKey privateKey; 22 23 static RSAPublicKey publicKey; 24 25 static { 26 try { 27 keyPairGen = KeyPairGenerator.getInstance("RSA"); 28 keyPairGen.initialize(512); 29 keyPair = keyPairGen.generateKeyPair(); 30 // Generate keys 31 privateKey = (RSAPrivateKey) keyPair.getPrivate(); 32 publicKey = (RSAPublicKey) keyPair.getPublic(); 33 } catch (NoSuchAlgorithmException e) { 34 // TODO Auto-generated catch block 35 e.printStackTrace(); 36 } 37 } 38 39 public static void main(String[] args) { 40 RSAEncrypt encrypt = new RSAEncrypt(); 41 File file = new File("C:\\Users\\张洋\\Desktop\\logo1.png"); 42 File newFile = new File("C:\\Users\\张洋\\Desktop\\logo已加密"); 43 encrypt.encryptFile(encrypt, file, newFile); 44 File file1 = new File("C:\\Users\\张洋\\Desktop\\logo已加密"); 45 File newFile1 = new File("C:\\Users\\张洋\\Desktop\\logo已解密.png"); 46 encrypt.decryptFile(encrypt, file1, newFile1); 47 } 48 49 public void encryptFile(RSAEncrypt encrypt, File file, File newFile) { 50 try { 51 InputStream is = new FileInputStream(file); 52 OutputStream os = new FileOutputStream(newFile); 53 54 byte[] bytes = new byte[53]; 55 while (is.read(bytes) > 0) { 56 byte[] e = encrypt.encrypt(RSAEncrypt.publicKey, bytes); 57 bytes = new byte[53]; 58 os.write(e, 0, e.length); 59 } 60 os.close(); 61 is.close(); 62 System.out.println("write success"); 63 } catch (Exception e) { 64 e.printStackTrace(); 65 } 66 } 67 68 public void decryptFile(RSAEncrypt encrypt, File file, File newFile) { 69 try { 70 InputStream is = new FileInputStream(file); 71 OutputStream os = new FileOutputStream(newFile); 72 byte[] bytes1 = new byte[64]; 73 while (is.read(bytes1) > 0) { 74 byte[] de = encrypt.decrypt(RSAEncrypt.privateKey, bytes1); 75 bytes1 = new byte[64]; 76 os.write(de, 0, de.length); 77 } 78 os.close(); 79 is.close(); 80 System.out.println("write success"); 81 82 } catch (Exception e) { 83 e.printStackTrace(); 84 } 85 } 86 87 /** */ 88 /** 89 * * Encrypt String. * 90 * 91 * @return byte[] 92 */ 93 protected byte[] encrypt(RSAPublicKey publicKey, byte[] obj) { 94 if (publicKey != null) { 95 try { 96 Cipher cipher = Cipher.getInstance("RSA"); 97 cipher.init(Cipher.ENCRYPT_MODE, publicKey); 98 return cipher.doFinal(obj); 99 } catch (Exception e) { 100 e.printStackTrace(); 101 } 102 } 103 return null; 104 } 105 106 /** */ 107 /** 108 * * Basic decrypt method * 109 * 110 * @return byte[] 111 */ 112 protected byte[] decrypt(RSAPrivateKey privateKey, byte[] obj) { 113 if (privateKey != null) { 114 try { 115 Cipher cipher = Cipher.getInstance("RSA"); 116 cipher.init(Cipher.DECRYPT_MODE, privateKey); 117 return cipher.doFinal(obj); 118 } catch (Exception e) { 119 e.printStackTrace(); 120 } 121 } 122 return null; 123 } 124 }
评论(0)