Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package encrpyionproject;
  7.  
  8. import java.io.FileInputStream;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.io.OutputStream;
  13. import javax.crypto.Cipher;
  14. import javax.crypto.CipherInputStream;
  15. import javax.crypto.CipherOutputStream;
  16. import javax.crypto.SecretKey;
  17. import javax.crypto.SecretKeyFactory;
  18. import javax.crypto.spec.DESKeySpec;
  19.  
  20. /**
  21. *
  22. * @author mahmudul.hasan
  23. */
  24.  
  25. public class EncrpyionProject {
  26.  
  27. /**
  28. * @param args the command line arguments
  29. */
  30.  
  31. public static void main(String[] args) {
  32. // TODO code application logic here
  33. try {
  34. String key = "XXXXXXXXXXX"; // needs to be at least 8 characters for DES
  35.  
  36. /* FileInputStream fis = new FileInputStream("D:\\MedicineD\\keys.txt");
  37. FileOutputStream fos = new FileOutputStream("D:\\MedicineD\\empty.jpg");
  38. encrypt(key, fis, fos);*/
  39. //
  40. FileInputStream fis2 = new FileInputStream("D:\\XXXX\\empty.jpg");
  41. FileOutputStream fos2 = new FileOutputStream("D:\\XXXXX\\keys_new.txt");
  42. decrypt(key, fis2, fos2);
  43. } catch (Throwable e) {
  44. e.printStackTrace();
  45. }
  46. }
  47.  
  48. public static void encrypt(String key, InputStream is, OutputStream os) throws Throwable {
  49. encryptOrDecrypt(key, Cipher.ENCRYPT_MODE, is, os);
  50. }
  51.  
  52. public static void decrypt(String key, InputStream is, OutputStream os) throws Throwable {
  53. encryptOrDecrypt(key, Cipher.DECRYPT_MODE, is, os);
  54. }
  55.  
  56. public static void encryptOrDecrypt(String key, int mode, InputStream is, OutputStream os) throws Throwable {
  57.  
  58. DESKeySpec dks = new DESKeySpec(key.getBytes());
  59. SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
  60. SecretKey desKey = skf.generateSecret(dks);
  61. Cipher cipher = Cipher.getInstance("DES"); // DES/ECB/PKCS5Padding for SunJCE
  62.  
  63. if (mode == Cipher.ENCRYPT_MODE) {
  64. cipher.init(Cipher.ENCRYPT_MODE, desKey);
  65. CipherInputStream cis = new CipherInputStream(is, cipher);
  66. doCopy(cis, os);
  67. } else if (mode == Cipher.DECRYPT_MODE) {
  68. cipher.init(Cipher.DECRYPT_MODE, desKey);
  69. CipherOutputStream cos = new CipherOutputStream(os, cipher);
  70. doCopy(is, cos);
  71. }
  72. }
  73.  
  74. public static void doCopy(InputStream is, OutputStream os) throws IOException {
  75. byte[] bytes = new byte[64];
  76. int numBytes;
  77. while ((numBytes = is.read(bytes)) != -1) {
  78. os.write(bytes, 0, numBytes);
  79. }
  80. os.flush();
  81. os.close();
  82. is.close();
  83. }
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement