**In cryptography, RSA (which stands for Rivest, Shamir and Adleman who first publicly described it) is an algorithm for public-key cryptography.**It is the first algorithm known to be suitable for signing as well as encryption, and was one of the first great advances in public key cryptography. RSA is widely used in electronic commerce protocols, and is believed to be sufficiently secure given sufficiently long keys and the use of up-to-date implementations.

### Java Code Examples

### JAVA generate RSA key pair using bouncy castle Crypto APIs

### JAVA RSA encrypt string using bouncy castle Crypto APIs

### JAVA RSA decrypt string using bouncy castle Crypto APIs

### JAVA RSA encrypt file using bouncy castle Crypto APIs

### JAVA RSA decrypt file using bouncy castle Crypto APIs

### RPGLE iSeries Code Examples

### RPGLE generate RSA key pair using bouncy castle Java Crypto APIs

### RPGLE RSA encrypt string using bouncy castle Java Crypto APIs

### RPGLE RSA decrypt string using bouncy castle Java Crypto APIs

### RPGLE RSA encrypt file using bouncy castle Java Crypto APIs

### RPGLE RSA decrypt file using bouncy castle Java Crypto APIs

**Public-key cryptography refers to a cryptographic system requiring two separate keys, one to lock or encrypt the plaintext, and one to unlock or decrypt the cyphertext.**Neither key will do both functions. One of these keys is published or public and the other is kept private. If the lock/encryption key is the one published then the system enables private communication from the public to the unlocking key's owner.

### How does Public-Key encryption work?

Let's assume we have two parties, Bob and Alice, who wish to transmit confidential information to one another over the Internet. Alice would like to send Bob a corporate document using a Public Key encryption system. To accomplish a secure transmission, they will need to do the following :

- Alice and Bob agree on a public-key cryptosystem.
- Bob generates a pair of mathematically linked keys : one public, one private.
- Bob transmits his public key to Alice over any insecure medium.
- Bob keeps the private key a secret.
- Alice uses Bob's public key and the encryption algorithm to encrypt her message, creating a ciphertext.
- Alice transmits the ciphertext to Bob.
- Bob decrypts the ciphertext using the same algorithm and his private key.

### The Bouncy Castle Crypto APIs for Java consist of the following:

- A lightweight cryptography API.
- A provider for the Java Cryptography Extension and the Java Cryptography Architecture.
- A clean room implementation of the JCE 1.2.1.
- A library for reading and writing encoded ASN.1 objects.
- A lightweight client-side TLS API.
- Generators for Version 1 and Version 3 X.509 certificates, Version 2 CRLs, and PKCS12 files.
- Generators for Version 2 X.509 attribute certificates.
- Generators/Processors for S/MIME and CMS (PKCS7/RFC 3852).
- Generators/Processors for OCSP (RFC 2560).
- Generators/Processors for TSP (RFC 3161 & RFC 5544).
- Generators/Processors for CMP and CRMF (RFC 4210 & RFC 4211).
- Generators/Processors for OpenPGP (RFC 2440).
- A signed jar version suitable for JDK 1.4-1.6 and the Sun JCE.

The lightweight API works with everything from the J2ME to the JDK 1.6.

### The RSA algorithm involves three steps: key generation, encryption and decryption.

#### Key generation

RSA involves a public key and a private key. The public key can be known to everyone and is used for encrypting messages. Messages encrypted with the public key can only be decrypted using the private key. The keys for the RSA algorithm are generated the following way:

- Choose two distinct prime numbers p and q.
- For security purposes, the integers p and q should be chosen at random, and should be of similar bit-length. Prime integers can be efficiently found using a primality test.
- Compute n = pq.
- n is used as the modulus for both the public and private keys
- Compute φ(
*n*) = (*p*– 1)(*q*– 1), where φ is Euler's totient function. - Choose an integer e such that 1 <
*e*< φ(*n*) and gcd(*e*,φ(*n*)) = 1, i.e. e and φ(n) are coprime. - e is released as the public key exponent.
- e having a short bit-length and small Hamming weight results in more efficient encryption - most commonly 0x10001 = 65537. However, small values of e (such as 3) have been shown to be less secure in some settings.[4]
- Determine
*d*=*e*^{–1}mod φ(*n*); i.e. d is the multiplicative inverse of*e*mod φ(*n*). - This is more clearly stated as solve for d given (d*e)mod φ(
*n*) = 1 - This is often computed using the extended Euclidean algorithm.
- d is kept as the private key exponent.

The public key consists of the modulus n and the public (or encryption) exponent e. The private key consists of the private (or decryption) exponent d which must be kept secret.

#### Encryption

Alice transmits her public key (n,e) to Bob and keeps the private key secret. Bob then wishes to send message M to Alice.

He first turns M into an integer m, such that 0 < m < n by using an agreed-upon reversible protocol known as a padding scheme. He then computes the ciphertext c corresponding to

*c*=*m*^{e}(mod*n*).This can be done quickly using the method of exponentiation by squaring. Bob then transmits c to Alice.

#### Decryption

Alice can recover m from c by using her private key exponent d via computing

*m*=*c*^{d}(mod*n*).Given m, she can recover the original message M by reversing the padding scheme.