Guest User

Untitled

a guest
Jan 21st, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Encry {
  4. public static final String ALPHABET="abcdefghijklmnopqrstuvwxyz";
  5. public static String encrypt(String plainText,int shiftKey)
  6. {
  7. plainText=plainText.toLowerCase();
  8. String cipherText="";
  9. for(int i=0;i<plainText.length();i++)
  10. {
  11. int charPosition=ALPHABET.indexOf(plainText.charAt(i));
  12. int keyVal=(shiftKey + charPosition)%26;
  13. char replaceVal=ALPHABET.charAt(keyVal);
  14. cipherText +=replaceVal;
  15. }
  16. return cipherText;
  17. }
  18.  
  19. public static String decrypt(String cipherText, int shiftKey)
  20. {
  21. cipherText=cipherText.toLowerCase();
  22. String plainText="";
  23. for(int i=0;i<cipherText.length();i++)
  24. {
  25. int charPosition=ALPHABET.indexOf(cipherText.charAt(i));
  26. int keyVal=(charPosition-shiftKey)%26;
  27. if(keyVal<0)
  28. {
  29. keyVal=ALPHABET.length() + keyVal;
  30. }
  31. char replaceVal=ALPHABET.charAt(keyVal);
  32. plainText+=replaceVal;
  33. }
  34. return plainText;
  35. }
  36.  
  37.  
  38. public static void main(String args[])
  39. {
  40. Scanner sc=new Scanner(System.in);
  41. System.out.println("enter the string for encryption");
  42. String message=" ";
  43. message=sc.next();
  44. System.out.println("encrypted messege is:");
  45. System.out.println(encrypt(message,3));
  46. System.out.println("decrypted messege is:");
  47. System.out.println(decrypt(encrypt(message,3),3));
  48. sc.close();
  49. }
  50. }
Add Comment
Please, Sign In to add comment