1. import java.util.Scanner;
  2. 
  3. /**
  4. * RandomCypher.java
  5. * @author Jose M Vidal <jmvidal@gmail.com>
  6. * Created on Feb 16, 2012
  7. *
  8. */
  9. 
  10. public class RandomCypher {
  11. 
  12. /**
  13. * @param args
  14. */
  15. public static void main(String[] args) {
  16. char c = 'a';
  17. final int LEN = 26; //26 letters in English
  18. char[] alphabet = new char[LEN]; //just for printing
  19. char[] cypher = new char[LEN];
  20. //first, put the alphabet in
  21. for (int i = 0; i < cypher.length; i++) {
  22. cypher[i] = (char)('a' + i);
  23. alphabet[i] = (char)('a' + i);
  24. }
  25. //now shuffle
  26. for (int i = 0; i < 100; i++) {
  27. int a = (int)(Math.random() * LEN);
  28. int b = (int)(Math.random() * LEN);
  29. char temp = cypher[a];
  30. cypher[a] = cypher[b];
  31. cypher[b] = temp;
  32. }
  33. System.out.println("Our cypher is:");
  34. System.out.println(alphabet);
  35. System.out.println(cypher);
  36. Scanner keyboard = new Scanner(System.in);
  37. String message = "";
  38. do {
  39. System.out.println("Enter your message:");
  40. message = keyboard.nextLine();
  41. char[] msg = message.toLowerCase().toCharArray();
  42. System.out.println("The encrypted message is:");
  43. for (int i = 0; i < msg.length; i++) {
  44. char letter = msg[i];
  45. if (letter < 'a' || letter > 'z')
  46. System.out.print(letter);
  47. else
  48. System.out.print(cypher[(int)(letter - 'a')]);
  49. }
  50. System.out.println("");
  51. } while (! message.equals(""));
  52. 
  53. }
  54. 
  55. }