Facebook
From csClass, 1 Month ago, written in Java.
Embed
Download Paste or View Raw
Hits: 147
  1. Exercise 1- Caesar Cipher:
  2. public class CaesarCipher {
  3. // Encryption method
  4. public static String encrypt(String plaintext, int shift) {
  5. String encryptedText = "";
  6. for (char ch : plaintext.toCharArray()) {
  7. if (Character.isLetter(ch)) {
  8. char base = Character.isUpperCase(ch) ? 'A' : 'a';
  9. encryptedText=encryptedText +((char) ((ch - base + shift) % 26
  10. + base));
  11. } else {
  12. encryptedText+=ch;
  13. }
  14. }
  15. return encryptedText;
  16. }
  17. // Decryption method
  18. public static String decrypt(String ciphertext, int shift) {
  19. // Decryption is the same as encryption with a negative shift
  20. return encrypt(ciphertext, -shift);
  21. }
  22.  public static void main(String[] args) {
  23. // Example Usage
  24. String plaintext = "Hello, World!";
  25. int shift = 3;
  26. // Encryption
  27. String encryptedText = encrypt(plaintext, shift);
  28. System.out.println("Encrypted: " + encryptedText);
  29. // Decryption
  30. String decryptedText = decrypt(encryptedText, shift);
  31. System.out.println("Decrypted: " + decryptedText);
  32. }
  33. }
  34. Exercise 2- Affine Cipher:
  35. import java.util.Random;
  36. public class AffineCipherExample {
  37. public static void main(String[] args) {
  38. // Example Usage
  39. String plaintext = "Hello, World!"; // Replace with your desired
  40. plaintext
  41. // Generate random values for a and b (ensuring a is coprime with
  42. 26)
  43.  int a = generateCoprimeWith26();
  44. int b = new Random().nextInt(26); // b can be any random integer
  45. // Encryption
  46. String ciphertext = encrypt(plaintext, a, b);
  47. System.out.println("Plaintext: " + plaintext);
  48. System.out.println("Encrypted: " + ciphertext);
  49. // Decryption
  50. String decryptedText = decrypt(ciphertext, a, b);
  51. System.out.println("Decrypted: " + decryptedText);
  52. }
  53. // Method to check if two numbers are coprime
  54. private static boolean areCoprime(int a, int b) {
  55. while (b != 0) {
  56. int temp = b;
  57. b = a % b;
  58. a = temp;
  59. }
  60. return a == 1;
  61. }
  62. // Method to generate a random integer coprime with 26
  63. private static int generateCoprimeWith26() {
  64.  Random random = new Random();
  65. int candidate;
  66. do {
  67. candidate = random.nextInt(25) + 1; // Generate a random
  68. integer between 1 and 25
  69. } while (!areCoprime(candidate, 26));
  70. return candidate;
  71. }
  72. // Affine Cipher Encryption
  73. private static String encrypt(String plaintext, int a, int b) {
  74. String encryptedText = "";
  75. for (char ch : plaintext.toCharArray()) {
  76. if (Character.isLetter(ch)) {
  77. char base = Character.isUpperCase(ch) ? 'A' : 'a';
  78. encryptedText+=((char) ((a * (ch - base) + b) % 26 + base));
  79. } else {
  80. encryptedText+=ch;
  81. }
  82. }
  83. return encryptedText;
  84. }
  85. // Affine Cipher Decryption
  86. private static String decrypt(String ciphertext, int a, int b) {
  87. // Calculate modular multiplicative inverse of a modulo 26
  88. int aInverse = 0;
  89. for (int i = 1; i < 26; i++) {
  90. if ((a * i) % 26 == 1) {
  91. System.out.println("a= "+ a);
  92. System.out.println("aInverse= "+ i);
  93. aInverse = i;
  94. break;
  95. }
  96. }
  97. String decryptedText = "";
  98. for (char ch : ciphertext.toCharArray()) {
  99. if (Character.isLetter(ch)) {
  100. char base = Character.isUpperCase(ch) ? 'A' : 'a';
  101. decryptedText+=((char) ((aInverse * (ch - base - b + 26) % 26) +
  102. base));
  103. } else {
  104. decryptedText+=ch;
  105. }
  106. }
  107.  return decryptedText;
  108. }
  109. }