import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.KeyGenerator;
public class AESEncryptionDecryption {
public static void main(String[] args) {
try{
Cipher cipher = Cipher.getInstance("AES");
KeyGenerator kg = KeyGenerator.getInstance("AES");
Key key = kg.generateKey();
cipher.init(Cipher.ENCRYPT_MODE, key);
CipherInputStream cipt=new CipherInputStream(new FileInputStream(new File("D:\\PlainTextInput.txt")), cipher);
FileOutputStream fip=new FileOutputStream(new File("D:\\EncryptedText.txt"));
int i;
while((i=cipt.read())!=-1)
{
fip.write(i);
}
cipher.init(Cipher.DECRYPT_MODE, key);
CipherInputStream ciptt=new CipherInputStream(new FileInputStream(new File("D:\\EncryptedText.txt")), cipher);
FileOutputStream fop=new FileOutputStream(new File("D:\\DecryptedText.txt"));
int j;
while((j=ciptt.read())!=-1)
{
fop.write(j);
}
}
catch(Exception e) {
e.printStackTrace();
}
System.out.println("Encryption and Decryption of plain text file performed successfully.");
}
}
Output of the program:
Encryption and Decryption of plain text file performed successfully.
Content of PlainTextInput.txt file: HelloStudentimport java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.KeyGenerator;
public class AESEncryptionDecryption {
public static void main(String[] args) {
try{
Cipher cipher = Cipher.getInstance("AES");
KeyGenerator kg = KeyGenerator.getInstance("AES");
Key key = kg.generateKey();
cipher.init(Cipher.ENCRYPT_MODE, key);
CipherInputStream cipt=new CipherInputStream(new FileInputStream(new File("D:\\PlainTextInput.txt")), cipher);
FileOutputStream fip=new FileOutputStream(new File("D:\\EncryptedText.txt"));
int i;
while((i=cipt.read())!=-1)
{
fip.write(i);
}
cipher.init(Cipher.DECRYPT_MODE, key);
CipherInputStream ciptt=new CipherInputStream(new FileInputStream(new File("D:\\EncryptedText.txt")), cipher);
FileOutputStream fop=new FileOutputStream(new File("D:\\DecryptedText.txt"));
int j;
while((j=ciptt.read())!=-1)
{
fop.write(j);
}
}
catch(Exception e) {
e.printStackTrace();
}
System.out.println("Encryption and Decryption of plain text file performed successfully.");
}
}
Output of the program:
Encryption and Decryption of plain text file performed successfully.
Content of EncryptedText.txt file: ùóÃ ¬x9f¯—©c aá
Content of DecryptedText.txt file: HelloStudent
Note: You need to create PlainTextInput.txt file in D:\ drive of computer. Execution of program will create EncryptedText.txt and DecryptedText.txt file in D:\ drive.
You may also like to view following Computer Security Programs:
- Use of Username and Password (login id password) functionality.
- To print password as star ( ***** ).
- Encrypt password using Caesar cipher.
- Use of XOR operations on two numbers.
- Simple Hash function implementation on 8 bits using XOR operation.
- C program to implement sender side confidentiality.
- C program to implement receiver side confidentiality.
- Simple Railfense - Encryption
- Playfair Encryption
- Playfair Decryption
- Simulation of Simplified DES - Initial Permutation function
- Key Generation in Simplified DES
No comments:
Post a Comment