ceaser cipher 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package NS1;
  2. import java.util.Scanner;
  3. /*
  4. * To change this license header, choose License Headers in Project Properties.
  5. * To change this template file, choose Tools | Templates
  6. * and open the template in the editor.
  7. */
  8. /**
  9. *
  10. * @author Mohit Lokhande
  11. */
  12. public class NS1 {
  13. public static void main(String []args) {
  14. @SuppressWarnings("resource")
  15. Scanner in = new Scanner(System.in);
  16. String input;
  17. int key;
  18. String lib = "0123456789";
  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 KEY:");
  24. key = Integer.parseInt(in.nextLine());
  25. //for encryption
  26. System.out.println("\nENCRYPTED TEXT IS:");
  27. for(int i = 0; i< cipher.length; i++)
  28. {
  29. for(int j = 0; j< stringtoarray.length; j++)
  30. {
  31. if (cipher[i] == stringtoarray[j])
  32. {
  33. cipher[i] = stringtoarray[(j + key) % 10];
  34. break;
  35. }
  36. }
  37. System.out.print(cipher[i]);
  38. }
  39. //for decryption
  40. System.out.println("\n\nDECRYPTED TEXT OR COMBINATION IS:");
  41. for(int k=0;k<36;k++)
  42. {
  43. for(int i=0;i<cipher.length;i++)
  44. for (int j = 0; j < stringtoarray.length; j++)
  45. if(cipher[i]==stringtoarray[j])
  46. {
  47. System.out.print(stringtoarray[(j+k)%10]);
  48. break;
  49. }
  50. System.out.println("\t");
  51. }
  52. }
  53. }