Open In App

Encrypt and Decrypt String File Using Java

Improve
Improve
Like Article
Like
Save
Share
Report

In the field of cryptography, encryption is the process of turning plain text or information into ciphertext, or text that can only be deciphered by the intended recipient. A cipher is a term used to describe the encryption algorithm. It secures communication networks and aids in preventing illegal access to customer information, emails, and other critical data. There are various solutions available right now, so we can pick the best safe algorithm that satisfies our needs.

Encryption: Encryption is converting plaintext into ciphertext, which can only be accessed by authorized users with the correct cryptographic key, encryption is a computer process. Encryption, which is an essential part of the digital revolution, simply changes readable data into another form that only individuals with the appropriate password can decode and view.

Decryption: The process of returning a meaningless communication (Ciphertext) to its original format is known as decryption (Plaintext). The reverse conversion algorithm from the one used to encrypt the data is applied in order for it to function. The information must be decrypted using the same key to restore it to its original state.

In symmetric encryption, a key is used by both sender and receiver for the purpose of encryption and decryption. The key used by both sender and receiver is the same in the case of symmetric encryption and decryption. Now let’s see an example of symmetric encryption and decryption.

Example

Let’s assume that the key used here is 7

Sender side: 

Clear text :  G E E K
Key:          7 7 7 7
Cipher text:  N L L R

Receiver side:

Cipher text:   N L L R
Key:           7 7 7 7 
Decipher text: G E E K

Java




/*package whatever //do not write package name here */
 
// Java code of above approach
// Importing required packages
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
 
public class GFG {
    public static void main(String[] args)
    {
 
        try {
            // Generating objects of KeyGenerator &
            // SecretKey
            KeyGenerator keygenerator
                = KeyGenerator.getInstance("DES");
            SecretKey myDesKey = keygenerator.generateKey();
 
            // Creating object of Cipher
            Cipher desCipher;
            desCipher = Cipher.getInstance("DES");
 
            // Creating byte array to store string
            byte[] text
                = "No body can see me.".getBytes("UTF8");
 
            // Encrypting text
            desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
            byte[] textEncrypted = desCipher.doFinal(text);
 
            // Converting encrypted byte array to string
            String s = new String(textEncrypted);
            System.out.println(s);
 
            // Decrypting text
            desCipher.init(Cipher.DECRYPT_MODE, myDesKey);
            byte[] textDecrypted
                = desCipher.doFinal(textEncrypted);
 
            // Converting decrypted byte array to string
            s = new String(textDecrypted);
            System.out.println(s);
        }
        catch (Exception e) {
            System.out.println("Exception");
        }
    }
}


Output

�1g��^�(����/�J��K:
No body can see me.


Last Updated : 11 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads