Encrypt and Decrypt String File Using Java
Last Updated :
28 Apr, 2025
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.
Similar Reads
Encrypt and Decrypt Using Rijndael Key in C# To keep data secure and protected it is necessary to keep the data encrypted. As we know that in C# and in other languages too there are many ways for encrypting data. The Data Encryption Standard method used for encryption was not promising good security that led to the invention of a highly secure
7 min read
Encrypt/Decrypt Files in Linux using Ccrypt Ccrypt is a command line tool for encryption and decryption of data. Ccrypt is based on the Rijndael cipher, the same cipher used in the AES standard. On the other hand, in the AES standard, a 128-bit block size is used, whereas ccrypt uses a 256-bit block size. Ccrypt commonly uses the .cpt file ex
3 min read
Encrypting and Decrypting the Files Using GnuPG in Linux GnuPG is an encryption module that uses OpenPGP at its core. PGP stands for Pretty Good Privacy. It is an encryption program that provides authentication and cryptographic privacy for data communication. In the age where data is the new oil, the modern age thieves won't intrude in through doors, win
3 min read
Convert byte[] array to File using Java As we know whenever it comes to writing over a file, write() method of the File class comes into play but here we can not use it in order to convert that byte into a file. In order to convert a byte array to a file, we will be using a method named the getBytes() method of String class. Implementatio
3 min read
Blockchain - Encrypt & Decrypt Files With Password Using OpenSSL Encryption is the process of encoding information or data in such a way that only authorized parties can access it. The process of encrypting information involves using a mathematical algorithm to transform the original information, known as plaintext, into a form that is unreadable to anyone who do
5 min read