Vignary cipher 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import java.util.Scanner;
  2. /*
  3. * To change this license header, choose License Headers in Project Properties.
  4. * To change this template file, choose Tools | Templates
  5. * and open the template in the editor.
  6. */
  7. /**
  8. *
  9. * @author Mohit Lokhande
  10. */
  11. public class NS2 {
  12. public static void main(String []args) {
  13. @SuppressWarnings("resource")
  14. Scanner in = new Scanner(System.in);
  15. String input;
  16. //String key;
  17. int[] key;
  18. String lib = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  19. char[] stringtoarray = lib.toCharArray();
  20. System.out.println("ENTER THE STRING YOU WANT TO ENCRYPT:");
  21. input = in.nextLine();
  22. char[] cipher = input.toCharArray();
  23. System.out.println("\nENTER THE NUMBER OF KEYS:");
  24. // key = Integer.parseInt(in.nextLine());
  25. int size = Integer.parseInt(in.nextLine());
  26. key = new int [size];
  27. System.out.println("\nENTER KEYS:");
  28. for(int i=0;i<size;i++)
  29. {
  30. key[i] = Integer.parseInt(in.nextLine());
  31. }
  32. int keycount=0;
  33. //for encryption
  34. System.out.println("\nCIPHER TEXT IS:");
  35. for(int i = 0; i< cipher.length; i++)
  36. {
  37. for(int j = 0; j< stringtoarray.length; j++)
  38. {
  39. if (cipher[i] == stringtoarray[j])
  40. {
  41. cipher[i] = stringtoarray[(j + key[keycount++%size]) % 52];
  42. break;
  43. }
  44. }
  45. System.out.print(cipher[i]);
  46. }
  47. System.out.println("\n\nDECRYPTED TEXT IS:");
  48. int n=0 , l=0;
  49. keycount=0;
  50. for(int i = 0; i< cipher.length; i++)
  51. {
  52. for(int j = 0; j< stringtoarray.length; j++)
  53. {
  54. if (cipher[i] == stringtoarray[j])
  55. {
  56. cipher[i] = stringtoarray[(j - key[keycount++%size]) % 52];
  57. break;
  58. }
  59. }
  60. System.out.print(cipher[i]);
  61. }
  62. }
  63. }