Code Examples – En-/Decryption with RSA

Introduction  |  Message Digest  |  Symmetric ciphers  |  RSA  |  ECIES  |  Digital signatures (S/MIME)

Download code

En-/decryption with RSA: We want to use RSA to encrypt and decrypt a file. RSA is an algorithm for public-key encryption. In contrast to symmetric key cryptography, public key cryptography generally allows users to communicate securely without having prior access to a shared secret key, by using a pair of cryptographic keys, called the public key and private key, which are mathematically related, but it is infeasible to compute the private key from the public key. (Another term for public-key cryptography is asymmetric key cryptography.) The private key is usually kept secret, whereas the public key may be widely distributed.

In this example, we will generate such a keypair and encrypt and decrypt a file stored on the local disk with RSA.

First, we do the necessary imports:

01 import;
02 import;
03 import;
04 import;
05 import;
06 import;
07 import;
09 import javax.crypto.Cipher;
10 import javax.crypto.CipherInputStream;
11 import javax.crypto.CipherOutputStream;
13 import de.flexiprovider.core.FlexiCoreProvider;
15 public class ExampleRSA {
17 public static void main(String[] args) throws Exception {
19 Security.addProvider(new FlexiCoreProvider());

Next, we create an instance of the JCE class Cipher and the JCA class KeyPairGenerator. The Cipher class provides the functionality of a cryptographic cipher used for encryption and decryption and the KeyPairGenerator class can be used to generate pairs of private and public keys specific to a certain public-key algorithm.

21 KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "FlexiCore");
22 Cipher cipher = Cipher.getInstance("RSA", "FlexiCore");
24 kpg.initialize(1024);
25 KeyPair keyPair = kpg.generateKeyPair();
26 PrivateKey privKey = keyPair.getPrivate();
27 PublicKey pubKey = keyPair.getPublic();

In this example, we create a cipher object to operate with the RSA algorithm (line 22, for more information about the Cipher class please look at the code example for symmetric ciphers) and an instance of the KeyPairGenerator class for creating RSA keypairs (line 21).

In lines 24–27, we do the keypair generation. First, we initialize the key pair generator for a certain keysize with the initialize(int keysize) method (line 24). Next, we create an instance of the KeyPair class which is a simple holder for key pairs (public and private key) and has two methods for obtaining these keys: getPrivate() and getPublic(). The keys finally are obtained in lines 26 and 27.

Now we are ready for encryption. We have to initialize the cipher in encryption mode, using the public key:

31 cipher.init(Cipher.ENCRYPT_MODE, pubKey);

Now cipher streams are used to encrypt the file on the local disk (for more information about cipher streams please look at the code example for symmetric ciphers):

33 String cleartextFile = "cleartext.txt";
34 String ciphertextFile = "ciphertextRSA.txt";
36 FileInputStream fis = new FileInputStream(cleartextFile);
37 FileOutputStream fos = new FileOutputStream(ciphertextFile);
38 CipherOutputStream cos = new CipherOutputStream(fos, cipher);
40 byte[] block = new byte[32];
41 int i;
42 while ((i = != -1) {
43 cos.write(block, 0, i);
44 }
45 cos.close();

The ciphertext is now contained in the file "ciphertextRSA.txt".

Next, we want to decrypt the data to restore our cleartext. We have to initialize the cipher in decryption mode, using the private key:

49 String cleartextAgainFile = "cleartextAgainRSA.txt";
51 cipher.init(Cipher.DECRYPT_MODE, privKey);

We use the same cipher as for encryption but we initialize it for decryption with the previously generated private key in line 51.

53 fis = new FileInputStream(ciphertextFile);
54 CipherInputStream cis = new CipherInputStream(fis, cipher);
55 fos = new FileOutputStream(cleartextAgainFile);
57 while ((i = != -1) {
58 fos.write(block, 0, i);
59 }
60 fos.close();
61 }
63 }

Again, cipher streams are used to write the cleartext to the file "cleartextAgainRSA.txt".

Druckerenglisch deutsche Flagge   Impressum