thieumao

Encrypt File with DES

Aug 13th, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.23 KB | None | 0 0
  1. package com.javacodegeeks.snippets.core;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import java.security.InvalidAlgorithmParameterException;
  10. import java.security.InvalidKeyException;
  11. import java.security.NoSuchAlgorithmException;
  12. import java.security.spec.AlgorithmParameterSpec;
  13.  
  14. import javax.crypto.Cipher;
  15. import javax.crypto.CipherInputStream;
  16. import javax.crypto.CipherOutputStream;
  17. import javax.crypto.KeyGenerator;
  18. import javax.crypto.NoSuchPaddingException;
  19. import javax.crypto.SecretKey;
  20. import javax.crypto.spec.IvParameterSpec;
  21.  
  22. public class EncryptDecryptFileStreamWithDES {
  23.  
  24.     private static Cipher ecipher;
  25.     private static Cipher dcipher;
  26.  
  27.     // 8-byte initialization vector
  28.     private static byte[] iv = {
  29.  
  30.   (byte)0xB2, (byte)0x12, (byte)0xD5, (byte)0xB2,
  31.  
  32.   (byte)0x44, (byte)0x21, (byte)0xC3, (byte)0xC3
  33.     };
  34.  
  35.     public static void main(String[] args) {
  36.  
  37.         try {
  38.  
  39.             SecretKey key = KeyGenerator.getInstance("DES").generateKey();
  40.  
  41.             AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
  42.  
  43.             ecipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
  44.  
  45. dcipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
  46.  
  47. ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
  48.  
  49. dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
  50.  
  51. encrypt(new FileInputStream("cleartext.txt"), new FileOutputStream("encrypted.dat"));
  52.  
  53. decrypt(new FileInputStream("encrypted.dat"), new FileOutputStream("cleartext-reversed.txt"));
  54.  
  55.         }
  56.         catch (FileNotFoundException e) {
  57.             System.out.println("File Not Found:" + e.getMessage());
  58.             return;
  59.         }
  60.         catch (InvalidAlgorithmParameterException e) {
  61.             System.out.println("Invalid Alogorithm Parameter:" + e.getMessage());
  62.             return;
  63.         }
  64.         catch (NoSuchAlgorithmException e) {
  65.             System.out.println("No Such Algorithm:" + e.getMessage());
  66.             return;
  67.         }
  68.         catch (NoSuchPaddingException e) {
  69.             System.out.println("No Such Padding:" + e.getMessage());
  70.             return;
  71.         }
  72.         catch (InvalidKeyException e) {
  73.             System.out.println("Invalid Key:" + e.getMessage());
  74.             return;
  75.         }
  76.  
  77.     }
  78.  
  79.     private static void encrypt(InputStream is, OutputStream os) {
  80.  
  81.   try {
  82.  
  83.     byte[] buf = new byte[1024];
  84.  
  85. // bytes at this stream are first encoded
  86.  
  87.     os = new CipherOutputStream(os, ecipher);
  88.  
  89. // read in the clear text and write to out to encrypt
  90.  
  91. int numRead = 0;
  92.  
  93. while ((numRead = is.read(buf)) >= 0) {
  94.  
  95.     os.write(buf, 0, numRead);
  96.  
  97. }
  98.  
  99. // close all streams
  100.  
  101. os.close();
  102.  
  103.   }
  104.  
  105.   catch (IOException e) {
  106.  
  107.     System.out.println("I/O Error:" + e.getMessage());
  108.  
  109.   }
  110.  
  111.     }
  112.  
  113.     private static void decrypt(InputStream is, OutputStream os) {
  114.  
  115.   try {
  116.  
  117.     byte[] buf = new byte[1024];
  118.  
  119. // bytes read from stream will be decrypted
  120.  
  121.     CipherInputStream cis = new CipherInputStream(is, dcipher);
  122.  
  123. // read in the decrypted bytes and write the clear text to out
  124.  
  125. int numRead = 0;
  126.  
  127. while ((numRead = cis.read(buf)) >= 0) {
  128.  
  129.     os.write(buf, 0, numRead);
  130.  
  131. }
  132.  
  133. // close all streams
  134.  
  135. cis.close();
  136.  
  137. is.close();
  138.  
  139. os.close();
  140.  
  141.   }
  142.  
  143.   catch (IOException e) {
  144.  
  145.     System.out.println("I/O Error:" + e.getMessage());
  146.  
  147.   }
  148.  
  149.     }
  150.  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment