Advertisement
nex036ara

myCrypto

Mar 25th, 2014
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.87 KB | None | 0 0
  1. package rs.ac.uns.ftn.informatika.ib.crypto;
  2.  
  3. import java.io.BufferedWriter;
  4. import java.io.File;
  5. import java.io.FileOutputStream;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.nio.file.Files;
  9. import java.nio.file.Paths;
  10. import java.security.AlgorithmParameters;
  11. import java.security.InvalidAlgorithmParameterException;
  12. import java.security.InvalidKeyException;
  13. import java.security.NoSuchAlgorithmException;
  14. import java.security.spec.InvalidParameterSpecException;
  15.  
  16. import javax.crypto.BadPaddingException;
  17. import javax.crypto.Cipher;
  18. import javax.crypto.IllegalBlockSizeException;
  19. import javax.crypto.KeyGenerator;
  20. import javax.crypto.NoSuchPaddingException;
  21. import javax.crypto.SecretKey;
  22. import javax.crypto.spec.IvParameterSpec;
  23.  
  24. import rs.ac.uns.ftn.informatika.ib.util.Base64;
  25.  
  26. public class MyCrypto {
  27.  
  28.     /**
  29.      * @param args
  30.      */
  31.     private SecretKey secretKey;
  32.     private byte []data;
  33.     private BufferedWriter bw;
  34.    
  35.     public MyCrypto() {
  36.         //Security.addProvider(new BouncyCastleProvider());
  37.     }
  38.    
  39.     public void getByteFromFile() {
  40.         try {
  41.             data =Files.readAllBytes(Paths.get("C:\\Users\\Branislav\\Desktop\\bp2\\Uputstvo_za_Oracle_XE.txt"));
  42.             System.out.println("length in bytes: "+ data.length);
  43.         } catch (IOException e) {
  44.             // TODO Auto-generated catch block
  45.             e.printStackTrace();
  46.         }
  47.     }
  48.    
  49.     private void generateKey() {
  50.         try {
  51.             //generator para kljuceva za DES algoritam
  52.             KeyGenerator   keyGen = KeyGenerator.getInstance("AES");
  53.             //generise kljuc za DES
  54.             secretKey = keyGen.generateKey();
  55.  
  56.         } catch (NoSuchAlgorithmException e) {
  57.             e.printStackTrace();
  58.         }
  59.     }
  60.    
  61.    
  62.     public void saveKey(byte[] key) {
  63.        
  64.         try {
  65.                 /*bw= new BufferedWriter(new FileWriter("kljucevi.txt",true));
  66.                 bw.write(key.hashCode());
  67.                 bw.newLine();
  68.                 bw.close();*/
  69.                
  70.            
  71.              File kekFile = new File("kljucevi.txt");
  72.              FileOutputStream f = new FileOutputStream(kekFile);
  73.              f.write(key);
  74.              f.close();
  75.            
  76.         } catch (IOException e) {
  77.             e.printStackTrace();
  78.            
  79.             }
  80.        
  81.        
  82.     }
  83.    
  84.     private void transfer() {
  85.    
  86.         try {
  87.            
  88.             Cipher chipEnc = Cipher.getInstance("AES/CBC/PKCS5Padding");
  89.             chipEnc.init(Cipher.ENCRYPT_MODE, secretKey);
  90.             byte[] ciphertext = chipEnc.doFinal(data);
  91.             System.out.println("Kriptovan text: " + Base64.encodeToString(ciphertext));
  92.            
  93.             saveKey(secretKey.getEncoded());
  94.        
  95.             //The basic purpose of an IV is to ensure
  96.             //that the encryption function works differently every time
  97.             //it adds an element of randomness, or at least unpredictability
  98.             //to your ciphertexts.
  99.             AlgorithmParameters params = chipEnc.getParameters();
  100.             byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
  101.            
  102.            
  103.            
  104.             Cipher chipDec = Cipher.getInstance("AES/CBC/PKCS5Padding");
  105.             chipDec.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv));
  106.             byte[] plainText = chipDec.doFinal(ciphertext);
  107.             System.out.println("Primljeni text: " + new String(plainText));
  108.        
  109.         }
  110.         catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
  111.             // TODO Auto-generated catch block
  112.             e.printStackTrace();
  113.         } catch (InvalidKeyException e) {
  114.             // TODO Auto-generated catch block
  115.             e.printStackTrace();
  116.         } catch (IllegalBlockSizeException e) {
  117.             // TODO Auto-generated catch block
  118.             e.printStackTrace();
  119.         } catch (BadPaddingException e) {
  120.             // TODO Auto-generated catch block
  121.             e.printStackTrace();
  122.         } catch (InvalidAlgorithmParameterException e) {
  123.             // TODO Auto-generated catch block
  124.             e.printStackTrace();
  125.         } catch (InvalidParameterSpecException e) {
  126.             // TODO Auto-generated catch block
  127.             e.printStackTrace();
  128.         }
  129.        
  130.        
  131.     }
  132.    
  133.    
  134.     public static void main(String[] args) {
  135.         // TODO Auto-generated method stub
  136.         MyCrypto c = new MyCrypto();
  137.         //use symetric AES algorithm
  138.         c.getByteFromFile();
  139.         c.generateKey();
  140.         c.transfer();
  141.        
  142.     }
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement