How to Encrypt and Decrypt the Text File using RSA (Rivest–Shamir–Adleman) Algorithm in Java?
Share
Condition for Encrypt and Decrypt Text File using RSA Algorithm in Java
Description:
RSA (Rivest–Shamir–Adleman) is a widely used asymmetric encryption algorithm that secures data through a pair of keys: a public key for encryption and a private key for decryption. RSA encryption provides robust security, making it a common choice for securing sensitive data during transmission or storage. The algorithm relies on the mathematical principles of large prime numbers and modular exponentiation, ensuring that the decryption process is computationally infeasible without the corresponding private key. The KeyPairGenerator class generates the RSA key pair, consisting of a public key for encrypting the file and a private key for decrypting it. The Cipher class, initialized with the RSA algorithm, handles the actual encryption and decryption of the file content. For encryption, the sender uses the recipient's public key, while the recipient decrypts the file using their private key. While RSA excels at securing data, it is generally not used for encrypting large files directly due to performance limitations. Instead, RSA is often combined with symmetric encryption algorithms like AES. In this hybrid encryption approach, RSA encrypts a symmetric key, which then encrypts the actual file content. This combination provides both strong security and efficient processing for large files. Proper management of keys is crucial for ensuring the integrity and security of encrypted data. Additionally, implementing secure key storage and handling is essential to prevent unauthorized access or key compromise. By combining RSA with modern encryption practices, developers can create a secure framework for protecting files and messages in Java applications.
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jTextField2 = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
// Write decrypted data back to a text file
try (FileOutputStream fos = new FileOutputStream(outputFilePath)) {
fos.write(decryptedData);
}
System.out.println("Decryption complete. Decrypted data saved to " + outputFilePath);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
private byte[] readFile(String encryptFilePath)throws Exception {
File file = new File(encryptFilePath);
byte[] data = new byte[(int) file.length()];
try (FileInputStream fis = new FileInputStream(file)) {
fis.read(data);
}
return data;
}
private void savePublicKeyToFile(PublicKey publicKey)throws Exception {
try (FileOutputStream fos = new FileOutputStream("public.key")) {
fos.write(publicKey.getEncoded());
System.out.println("Server public key saved to file.");
}
}
}
Step 1
To start the encryption process, select the input file path after setting up the RSA encryption GUI.
Step 2
The chosen file location is indicated by the selected file path that shows up in the text field.
Step 3
Select the path of the 'Encrypt.txt' file to begin the encryption process.
Step 4
The path of the 'Encrypt.txt' file has been selected for encryption.
Step 5
The files input.txt and Encrypt.txt are chosen. When the Encrypt button is clicked, encryption is successfully finished.
Step 6
To start the decryption process, select the input file path after setting up the RSA decryption GUI
Step 7
The chosen file location is indicated by the selected file path that shows up in the text field.
Step 8
Select the path of the 'decrypt.txt' file to begin the decryption process.
Step 9
The path of the 'decrypt.txt' file has been selected for decryption.
Step 10
The files encrypt.txt and decrypt.txt are chosen. When the Decrypt button is clicked, decryption is successfully finished.
Step 11
Extract the private key from the generated key pair and use it as the RSA secret key.
Step 12
Encryption applies to the input text file, with the result saved in the encrypt text file. Decryption restores the original content from the decrypt text file.