import java.security.Key; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.SecureRandom; import java.security.spec.RSAKeyGenParameterSpec; import javax.crypto.Cipher; public class teste2P1 { public static void main( String[] args) throws Exception { // This is the plaintext message we wnat to encrypt byte[]input = new byte[] { (byte)0x00, (byte)0xAB, (byte)0xCD, (byte)0xEF , (byte)0x00, (byte)0xAB, (byte)0xCD, (byte)0xEF , (byte)0x00, (byte)0xAB, (byte)0xCD, (byte)0xEF , (byte)0x00, (byte)0xAB, (byte)0xCD, (byte)0xEF , (byte)0x00, (byte)0xAB, (byte)0xCD, (byte)0xEF , (byte)0x00, (byte)0xAB, (byte)0xCD, (byte)0xEF , (byte)0x00, (byte)0xAB, (byte)0xCD, (byte)0xEF , (byte)0x00, (byte)0xAB, (byte)0xCD, (byte)0xEF , (byte)0x00, (byte)0xAB, (byte)0xCD, (byte)0xEF , (byte)0x00, (byte)0xAB, (byte)0xCD, (byte)0xEF , (byte)0x00, (byte)0xAB, (byte)0xCD, (byte)0xEF , (byte)0x00, (byte)0xAB, (byte)0xCD, (byte)0xEF }; Cipher cipher = Cipher.getInstance("RSA/NONE/PKCS1Padding", "BC"); SecureRandom random = Utils3.createFixedRandom(); // Create keypair KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "BC"); generator.initialize(new RSAKeyGenParameterSpec(256,RSAKeyGenParameterSpec.F0),random); KeyPair pair = generator.generateKeyPair(); Key pubKey = pair.getPublic(); Key privKey = pair.getPrivate(); // This prints the input plaintext message System.out.println("input : " + Utils3.toHex(input)); // Encrypt with the public key cipher.init(Cipher.ENCRYPT_MODE, pubKey, random); byte[] cipherText = cipher.doFinal(input); // This prints the ciphertext message System.out.println("cipher: " + Utils3.toHex(cipherText)); // Decrypt with the private key cipher.init(Cipher.DECRYPT_MODE, privKey); byte[] plainText = cipher.doFinal(cipherText); // this prints the initial plaintext message System.out.println("plain : " + Utils3.toHex(plainText)); } }