Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Exer5
  3. {
  4. public static void main(String[] args)
  5. {
  6. Scanner sc=new Scanner(System.in);
  7. String str;
  8. String key;
  9. int keyLength;
  10.  
  11. System.out.println("Enter message:");
  12. str=sc.nextLine();
  13. System.out.println("Enter encryption key:");
  14. key=sc.next();
  15. keyLength=key.length();
  16. //This for loop is repeated use of 'Enrypt' and 'Decrypt' options
  17. for(;;)
  18. {
  19. System.out.println("1.Encrypt\n2.Decrypt\n3.Exit...");
  20. int choice=sc.nextInt();
  21. switch(choice)
  22. {
  23. case 1:
  24. /*send input string keyLength to encrypt() method to encrypt it returns 'Encrypted' string*/
  25. System.out.println("Encrypted message..."+encrypt(str,keyLength));
  26. break;
  27. case 2:
  28. //send retrived string from encrypt() method and keyLength to decrypt() method it returns 'Decrypted' string
  29. System.out.println("Decryptedmessage..."+decrypt(encrypt(str,keyLength),keyLength));
  30. break;
  31. case 3:
  32. //exit from the program
  33. System.exit(0);
  34. break;
  35. default:
  36. System.out.println("Invalid option..");
  37. }
  38. }
  39. }
  40. public static String encrypt(String str,int keyLength)
  41. {
  42. String encrypted="";
  43. for(int i=0;i<str.length();i++)
  44. {
  45. //stores ascii value of character in the string at index 'i'
  46. int c=str.charAt(i);
  47. //encryption logic for uppercase letters
  48. if(Character.isUpperCase(c))
  49. {
  50. c=c+(keyLength%26);
  51. //if c value exceeds the ascii value of 'Z' reduce it by subtracting 26(no.of alphabets) to keep in boundaries of ascii values of 'A' and 'Z'
  52. if(c>'Z')
  53. c=c-26;
  54. }
  55. //encryption logic for lowercase letters
  56. else if(Character.isLowerCase(c))
  57. {
  58. c=c+(keyLength%26);
  59. //if c value exceeds the ascii value of 'z' reduce it by subtracting 26(no.of alphabets) to keep in boundaries of ascii values of 'a' and 'z'
  60. if(c>'z')
  61. c=c-26;
  62. }
  63. //concatinate the encrypted characters/strings
  64. encrypted=encrypted+(char) c;
  65. }
  66. return encrypted;
  67. }
  68. public static String decrypt(String str,int keyLength)
  69. {
  70. String decrypted="";
  71. for(int i=0;i<str.length();i++)
  72. {
  73. //stores ascii value of character in the string at index 'i'
  74. int c=str.charAt(i);
  75. //decryption logic for uppercase letters
  76. if(Character.isUpperCase(c))
  77. {
  78. c=c-(keyLength%26);
  79. //if c value deceed the ascii value of 'A' increase it by adding 26(no.of alphabets) to keep in boundaries of ascii values of 'A' and 'Z'
  80. if(c<'A')
  81. c=c+26;
  82. }
  83. //decryption logic for uppercase letters
  84. else if(Character.isLowerCase(c))
  85. {
  86. c=c-(keyLength%26);
  87. //if c value deceed the ascii value of 'A' increase it by adding 26(no.of alphabets) to keep in boundaries of ascii values of 'A' and 'Z'
  88. if(c<'a')
  89. c=c+26;
  90. }
  91. //concatinate the decrypted characters/strings
  92. decrypted=decrypted+(char) c;
  93. }
  94. return decrypted;
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement