RSA Encryption Code Sample

RSA Encryption Code Samples are given below for Java, Phyton and GO

Java



import javax.crypto.Cipher;
import java.security.*;
import java.util.Base64;

public class AsymmetricEncryptionExample {

    private static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048); 
        return keyPairGenerator.generateKeyPair();
    }

    private static String encrypt(String plainText, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    private static String decrypt(String encryptedText, PrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
        return new String(decryptedBytes);
    }

    public static void main(String[] args) {
        try {
           
            KeyPair keyPair = generateKeyPair();
            PublicKey publicKey = keyPair.getPublic();
            PrivateKey privateKey = keyPair.getPrivate();

            
            String originalMessage = "Hello, Asymmetric Encryption!";
            String encryptedMessage = encrypt(originalMessage, publicKey);
            System.out.println("Şifrelenmiş Mesaj: " + encryptedMessage);

            
            String decryptedMessage = decrypt(encryptedMessage, privateKey);
            System.out.println("Çözülmüş Mesaj: " + decryptedMessage);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

      

Python



from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
import base64

class AsymmetricEncryption:
    def __init__(self):
       
        self.key_pair = RSA.generate(2048)
        self.private_key = self.key_pair.export_key()
        self.public_key = self.key_pair.publickey().export_key()

    def encrypt(self, plain_text):
        
        public_key = RSA.import_key(self.public_key)
        cipher = PKCS1_OAEP.new(public_key)
        encrypted_bytes = cipher.encrypt(plain_text.encode())
        return base64.b64encode(encrypted_bytes).decode()

    def decrypt(self, encrypted_text):
        
        private_key = RSA.import_key(self.private_key)
        cipher = PKCS1_OAEP.new(private_key)
        decrypted_bytes = cipher.decrypt(base64.b64decode(encrypted_text))
        return decrypted_bytes.decode()

# Test
encryption_instance = AsymmetricEncryption()
original_message = "Hello, Asymmetric Encryption!"
encrypted_message = encryption_instance.encrypt(original_message)
decrypted_message = encryption_instance.decrypt(encrypted_message)

print("original message :", original_message)
print("encrypted message :", encrypted_message)
print("decrypted message:", decrypted_message)

      

GO



from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
import base64

class AsymmetricEncryption:
    def __init__(self):
        # Anahtar çifti oluştur
        self.key_pair = RSA.generate(2048)
        self.private_key = self.key_pair.export_key()
        self.public_key = self.key_pair.publickey().export_key()

    def encrypt(self, plain_text):
        # Public Key ile şifreleme
        public_key = RSA.import_key(self.public_key)
        cipher = PKCS1_OAEP.new(public_key)
        encrypted_bytes = cipher.encrypt(plain_text.encode())
        return base64.b64encode(encrypted_bytes).decode()

    def decrypt(self, encrypted_text):
        # Private Key ile çözme
        private_key = RSA.import_key(self.private_key)
        cipher = PKCS1_OAEP.new(private_key)
        decrypted_bytes = cipher.decrypt(base64.b64decode(encrypted_text))
        return decrypted_bytes.decode()

# Test
encryption_instance = AsymmetricEncryption()
original_message = "Hello, Asymmetric Encryption!"
encrypted_message = encryption_instance.encrypt(original_message)
decrypted_message = encryption_instance.decrypt(encrypted_message)

print("original message:", original_message)
print("encrypted message:", encrypted_message)
print("decrypted message:", decrypted_message)