// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;

import javax.crypto.Cipher;

public class RSA {
    private static final String ALGO = "RSA";
    private static final int BITS = 1024;
    public static void main(String[] args) throws Exception {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        String s = "123456789012345678901234567890123456789012345678";
        byte[] plain = s.getBytes();
        System.out.println(s);
        System.out.println(plain.length);
        KeyPairGenerator kpg = KeyPairGenerator.getInstance(ALGO);
        kpg.initialize(BITS);
        KeyPair kp = kpg.generateKeyPair();
        PrivateKey priv = kp.getPrivate();
        PublicKey pub = kp.getPublic();
        Cipher cipher = Cipher.getInstance(ALGO);
        cipher.init(Cipher.ENCRYPT_MODE, priv);
        byte[] code = cipher.doFinal(plain);
        //code[23] = (byte) (code[23] + 1);
        System.out.println(code.length);
        cipher.init(Cipher.DECRYPT_MODE, pub);
        byte[] plain2 = cipher.doFinal(code);
        System.out.println(plain2.length);
        String s2 = new String(plain2);
        System.out.println(s2);
    }
}
