import java.security.*; import javax.crypto.*;
/* Use of Private Key in Encryption*/
public class PrivateKeyDemo
{
static Key key; static Cipher cipher;
static String algorithm = “AES”;
public static void main(String args[]) throws Exception
{
key = KeyGenerator.getInstance(algorithm).generateKey(); cipher = Cipher.getInstance(algorithm);
String text = “FYMCA 2020 Batch Hiray College”;
byte[] encryptionBytes = encrypt(text);
System.out.println(“Original Text: “+text); System.out.println(“Key: “+key.toString()); System.out.println(“Encrypted Text: “+encrypt(text));
System.out.println(“Decrypted Text: “+decrypt(encryptionBytes));
} // End of Main Method
public static byte[] encrypt(String input) throws Exception
{
cipher.init(Cipher.ENCRYPT_MODE, key); byte [] inputBytes = input.getBytes(); return cipher.doFinal(inputBytes);
} // close of encrypt method
public static String decrypt(byte[] encryptionBytes) throws Exception
{
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] convertDecrypt = cipher.doFinal(encryptionBytes); String convertString = new String(convertDecrypt);
return convertString;
} // close decrypt method
}
Output: