JAVA SP T


Lets dive into snapshot of my programs!💻

10. Write a Java program to create an interface Encryptable with methods encrypt (String data) and decrypt (String encryptedData) that define encryption and decryption operations. Create two classes AES and RSA that implement the Encryptable interface and provide their own encryption and decryption algorithms.

💡Code:

// Encryptable interface
              interface Encryptable {
                  String encrypt(String data);
                  String decrypt(String encryptedData);
              }
              
              // AES class implementing Encryptable
              class AES implements Encryptable {
                  @Override
                  public String encrypt(String data) {
                      // Implement AES encryption algorithm
                      // Example: Just reversing the string for demonstration purposes
                      return new StringBuilder(data).reverse().toString();
                  }
              
                  @Override
                  public String decrypt(String encryptedData) {
                      // Implement AES decryption algorithm
                      // Example: Just reversing the string back for demonstration purposes
                      return new StringBuilder(encryptedData).reverse().toString();
                  }
              }
              
              // RSA class implementing Encryptable
              class RSA implements Encryptable {
                  @Override
                  public String encrypt(String data) {
                      // Implement RSA encryption algorithm
                      // Example: Adding a prefix "RSA:" for demonstration purposes
                      return "RSA:" + data;
                  }
              
                  @Override
                  public String decrypt(String encryptedData) {
                      // Implement RSA decryption algorithm
                      // Example: Removing the "RSA:" prefix for demonstration purposes
                      if (encryptedData.startsWith("RSA:")) {
                          return encryptedData.substring(4);
                      } else {
                          // Handle invalid or unrecognized format
                          return "Invalid format for RSA decryption";
                      }
                  }
              }
              
              // Main class to test the implementation
              public class Encryption7078 {
                  public static void main(String[] args) {
                      // Test AES encryption and decryption
                      Encryptable aesEncryptor = new AES();
                      String aesData = "Hello, AES!";
                      String aesEncrypted = aesEncryptor.encrypt(aesData);
                      String aesDecrypted = aesEncryptor.decrypt(aesEncrypted);
              
                      System.out.println("AES Encrypted: " + aesEncrypted);
                      System.out.println("AES Decrypted: " + aesDecrypted);
              
                      // Test RSA encryption and decryption
                      Encryptable rsaEncryptor = new RSA();
                      String rsaData = "Hello, RSA!";
                      String rsaEncrypted = rsaEncryptor.encrypt(rsaData);
                      String rsaDecrypted = rsaEncryptor.decrypt(rsaEncrypted);
              
                      System.out.println("RSA Encrypted: " + rsaEncrypted);
                      System.out.println("RSA Decrypted: " + rsaDecrypted);
                  }
              }
                

📸Output :

output