Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. package encryption;
  2.  
  3. import jave.util.Scenner;
  4.  
  5. import javax.crypto.Cipher;
  6.  
  7. import javax.crypto. spec. IvParameterSpec;
  8. inport javax.crypto.spec.SecretKeySpec ;
  9.  
  10. public class TextEncryption {]
  11.  
  12. static Scanner console=new Scanner (System. tn);
  13.  
  14. public static void main (String[] ares) {
  15.  
  16. try {
  17.  
  18. /iget input text to be encrypted
  19. Systen.out.println("Please enter your message to be encrypted:");
  20. byte[] input~ console.nextLine().getBytes();
  21.  
  22. //specify the key and parameter used for encryption and decryption
  23. byte[] keyBytes=new byte[] {OxO1, 0x23, 0x45, 0x67, (byte)OxB9, (byte) @xab, (byte) Oxcd, (byte) Gxef};
  24. byte[] iveytes=new byte[] (0x00, 0x01,0x02, 0x3, 0x84, 0x05, 0x05, 0x07};
  25. SecretKeySpec key=new SecretKeySpec(keySytes ,"DES™);
  26.  
  27. IWarameterSpec ivSpec= new IvParemeterSpec( ivOytes);
  28. ffinitialisetion of the cipher paranrcters
  29. Cipher cipher=Cipher.getInstonce("DES/CBC/PKCS5Padding™ );
  30. //Encryption
  31. cipher. init(Cipher .ENCRYPT_MODE, key, ivSpec);
  32. byte[] encrypted-cipher .doFinal(input);
  33. Systen.out.println("Encrypted:"+ encrypted);
  34. //Peceyption
  35. cipher. init( Cipher . DECRYPT_MODE, key, ivSpec);
  36. byte[] decrypted=cipher. doF inal(encrypted);
  37. Systen.out.println("Decrypted:” + new String(decrypted));
  38.  
  39. }
  40. catch (Exception ex) {
  41.  
  42. System.out.print ("Error catched" + ex.getMessage());
  43. return;
  44.  
  45. }
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement