# Learn Aes and Rsa Encryption in Java

I. Introduction In this blog post, we will discuss how to encrypt a confidential message from the client to the backend application. An ...

https://www.czetsuyatech.com/2020/03/algorithms-aes-and-rsa-encryption-in-java.html

### I. Introduction

In this blog post, we will discuss how to encrypt a confidential message from the client to the backend application. An example would be a POS terminal sending the payment information of the user to the payment gateway.### II. Encryption Algorithms

There are two encryption algorithms that we would look at to secure our message:- AES - a successor to DES. It's a subset of the Rijndael block cipher. It's the symmetric key algorithm, which means we need the same key to encrypt and decrypt data. This algorithm is considered faster compared to RSA.
- RSA - one of the most widely used public/private key cryptosystems. It's an asymmetric key algorithm, which means we will use a public key to encrypt a text and private key to decrypt it. RSA has several modes
- Cipher Block Chaining(CBC) — Typically used for encrypting text files, e-mail messages, images, and so forth. It generates a random Initialization Vector (IV), then encrypts the data block-by-block in such a way that later blocks are randomized by the encryption of earlier blocks. Due to the randomly generated IV, repeated encryptions of the same plaintext will yield distinct ciphertexts.
- ECB-Mix-ECB (EME*) — If your application requires that data is encrypted so that every bit of the plaintext will affect every bit of the ciphertext, or an initialization vector cannot be used, then EME* should be used. EME* is less efficient than CBC mode but encrypts the data in a single block.

### III. Integrating AES and RSA Encryption to our POS Application

#### User Story

- A customer purchases a new laptop in the mall. He then enters his card and the POS reads this information.
- This information is encrypted using an AES Key. We will need the same key to decrypt the encrypted text afterward. So we need a secure way to send it.
- The AES key is encrypted using the RSA algorithm, which means our POS terminal should have a public key.
- The encrypted information plus the encrypted AES key is then saved into a single request object and send over the internet.
- Upon receive in the payment gateway side, we get the encrypted AES key and decrypt it using the RSA algorithm. Needless to say, we need the private key pair that we use during encryption.
- After the AES key is decrypted, we will use it to decrypt the private information which contains the payment details of the customer.
- The payment details are now decrypted and we can process the payment with the bank.

### IV. Running the Application

The source code is available at the repository mentioned in the reference section below.

- You must run App.java to generate both the AES key and RSA private and public keys.
- Refresh your project and key files should be generated in its root.
- Copy both the rsa_public and rsa_private into your src/test/resources directory.
- Run mvn clean test

I'm sure you are also a developer otherwise, you won't get here. Thus, I trust that you will understand how the project works. If you need help, I'm just one message away.

Post a Comment