0% found this document useful (0 votes)
3 views3 pages

6 Program

The document is a Java program that demonstrates the Rijndael (AES) encryption and decryption process. It generates a secret key, encrypts a plain text message, and then decrypts it back to the original message. The program also includes methods for key generation, encryption, decryption, and converting byte arrays to hexadecimal strings.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

6 Program

The document is a Java program that demonstrates the Rijndael (AES) encryption and decryption process. It generates a secret key, encrypts a plain text message, and then decrypts it back to the original message. The program also includes methods for key generation, encryption, decryption, and converting byte arrays to hexadecimal strings.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import [Link].

Cipher;

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link].Base64;

public class Rijndael {

public static void main(String[] args) throws Exception {

String plainText = "Hello, World!";

[Link]("Plain Text: " + plainText);

SecretKey secretKey = generateKey();

[Link]("Secret Key: " + bytesToHex([Link]()));

String encryptedText = encrypt(plainText, secretKey);

[Link]("Encrypted Text: " + encryptedText);

String decryptedText = decrypt(encryptedText, secretKey);

[Link]("Decrypted Text: " + decryptedText);

private static SecretKey generateKey() throws NoSuchAlgorithmException {

KeyGenerator keyGen = [Link]("AES");


[Link](128);

return [Link]();

private static String encrypt(String plainText, SecretKey secretKey) throws Exception {

Cipher cipher = [Link]("AES");

[Link](Cipher.ENCRYPT_MODE, secretKey);

byte[] encryptedBytes = [Link]([Link](StandardCharsets.UTF_8));

return [Link]().encodeToString(encryptedBytes);

private static String decrypt(String encryptedText, SecretKey secretKey) throws Exception {

Cipher cipher = [Link]("AES");

[Link](Cipher.DECRYPT_MODE, secretKey);

byte[] encryptedBytes = [Link]().decode(encryptedText);

byte[] decryptedBytes = [Link](encryptedBytes);

return new String(decryptedBytes, StandardCharsets.UTF_8);

private static String bytesToHex(byte[] bytes) {

StringBuilder hexString = new StringBuilder();

for (byte b : bytes) {

String hex = [Link](0xff & b);

if ([Link]() == 1) [Link]('0');

[Link](hex);

}
return [Link]();

You might also like