Encrypt and Decrypt String File Using Java
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
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
public class GFG {
public static void main(String[] args)
{
try {
KeyGenerator keygenerator
= KeyGenerator.getInstance( "DES" );
SecretKey myDesKey = keygenerator.generateKey();
Cipher desCipher;
desCipher = Cipher.getInstance( "DES" );
byte [] text
= "No body can see me." .getBytes( "UTF8" );
desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
byte [] textEncrypted = desCipher.doFinal(text);
String s = new String(textEncrypted);
System.out.println(s);
desCipher.init(Cipher.DECRYPT_MODE, myDesKey);
byte [] textDecrypted
= desCipher.doFinal(textEncrypted);
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
Share your thoughts in the comments
Please Login to comment...