Guest User

Untitled

a guest
Apr 23rd, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | None | 0 0
  1. import java.nio.file.Files;
  2. import java.nio.file.Paths;
  3. import java.security.Key;
  4.  
  5. import javax.crypto.*;
  6. import javax.crypto.spec.SecretKeySpec;
  7.  
  8. import java.util.*;
  9.  
  10. public class Encryptor {
  11.  
  12. public static void main(String[] args) throws Exception {
  13.     String FileName = "encryptedtext.txt";
  14.     String FileName2 = "decryptedtext.txt";
  15.  
  16.     Scanner input = new Scanner(System.in);
  17.  
  18.    System.out.println("Enter your 16 character key here:");
  19.    String EncryptionKey = input.next();
  20.  
  21.  
  22.  
  23.     KeyGenerator KeyGen = KeyGenerator.getInstance("AES");
  24.     KeyGen.init(128);
  25.  
  26.  
  27.     Cipher AesCipher = Cipher.getInstance("AES/CFB/NoPadding");
  28.     System.out.println("Enter text to encrypt or decrypt:");
  29.     String Text = input.next();
  30.  
  31.     System.out.println("Do you want to encrypt or decrypt (e/d)");
  32.     String answer = input.next();
  33.     if (answer.equalsIgnoreCase("e")){
  34.  
  35.         byte[] byteKey = (EncryptionKey.getBytes());
  36.         byte[] byteText = (Text).getBytes();
  37.         SecretKeySpec secretKeySpec = new SecretKeySpec(byteKey, "AES");
  38.         AesCipher.init(Cipher.ENCRYPT_MODE, secretKeySpec );
  39.         byte[] byteCipherText = AesCipher.doFinal(byteText);
  40.         Files.write(Paths.get(FileName), byteCipherText);
  41.         System.out.println(byteCipherText);
  42.  
  43.  
  44.           }
  45.     else if (answer.equalsIgnoreCase("d")){
  46.  
  47.         byte[] byteKey = (EncryptionKey.getBytes());
  48.         byte[] byteText = (Text).getBytes();
  49.         byte[] cipherText = Files.readAllBytes(Paths.get(FileName));
  50.  
  51.         SecretKeySpec secretKeySpec = new SecretKeySpec(byteKey, "AES");
  52.         AesCipher.init(Cipher.DECRYPT_MODE, secretKeySpec); // ERROR LINE
  53.         byte[] bytePlainText = AesCipher.doFinal(cipherText);
  54.         Files.write(Paths.get(FileName2), bytePlainText);
  55.         System.out.println(bytePlainText);
  56.     }
  57. }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment