List of Topics:
Location Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

How to Encrypt and Decrypt the Text File using Digital Signature Algorithm(DSA) in Java?

Encrypt and Decrypt Text File using DSA in Java

Condition for Encrypt and Decrypt Text File using DSA in Java

  • Description:
    The Digital Signature Algorithm (DSA) is a widely used cryptographic algorithm designed for signing and verifying the authenticity of messages or files, not for traditional encryption and decryption. DSA ensures data integrity and authenticity by generating a unique signature for a file or message. This signature can later be verified using the sender's public key, confirming that the data has not been altered and verifying the identity of the sender.
    Unlike encryption algorithms, which conceal data, DSA does not hide the content of the message or file. Instead, it focuses on ensuring that the data remains intact and unchanged during transmission. DSA is commonly used in applications such as secure communications, software distribution, and digital contracts, where verifying the authenticity of data is critical.
    The DSA algorithm relies on mathematical principles of modular exponentiation and the difficulty of discrete logarithms, offering a high level of security. It is often used in conjunction with other cryptographic methods like RSA or elliptic curve cryptography (ECC) for broader security frameworks. While DSA can guarantee the integrity and origin of data, it does not provide confidentiality by itself, making it an essential component of secure digital systems when combined with encryption techniques for complete data protection.
Sample Code
  • package Asymmetric;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.security.KeyPair;
    import java.security.KeyPairGenerator;
    import java.security.PrivateKey;
    import java.security.PublicKey;
    import java.security.Signature;
    import javax.crypto.Cipher;
    import javax.crypto.CipherInputStream;
    import javax.crypto.CipherOutputStream;
    import javax.crypto.KeyGenerator;
    import javax.crypto.SecretKey;
    import javax.swing.JFileChooser;
    import javax.swing.JOptionPane;
    public class DSA2 extends javax.swing.JFrame {
    private String originalFilePath;
    private String encryptedFilePath;
    private String decryptedFilePath;
    private String signatureFilePath;
    private PrivateKey privateKey;
    private PublicKey publicKey;
    private SecretKey sKey;
    public DSA2() {
    initComponents();
    try {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
    keyGen.initialize(2048);
    KeyPair dsaKeyPair = keyGen.generateKeyPair();
    privateKey = dsaKeyPair.getPrivate();
    publicKey = dsaKeyPair.getPublic();
    KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
    keyGenerator.init(256);
    sKey = keyGenerator.generateKey();
    encryptedFilePath = "/home/soft20/NetBeansProjects/DSA/Encrypt.txt";
    signatureFilePath = "/home/soft20/NetBeansProjects/DSA/Signature.txt";
    } catch (Exception e) {
    System.out.println(e.getMessage());
    }
    }
    @SuppressWarnings("unchecked")
    private void initComponents() {
    // UI components initialization
    }
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    try {
    originalFilePath = jTextField1.getText();
    encryptFile(originalFilePath, encryptedFilePath, sKey);
    JOptionPane.showMessageDialog(null, "File Encrypted successfully");
    } catch (Exception e) {
    System.out.println(e.getMessage());
    }
    }
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    JFileChooser fileChooser = new JFileChooser();
    int result = fileChooser.showOpenDialog(this);
    if (result == JFileChooser.APPROVE_OPTION) {
    File selectedFile = fileChooser.getSelectedFile();
    jTextField1.setText(selectedFile.getAbsolutePath());
    }
    }
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
    try {
    decryptedFilePath = "/home/soft20/NetBeansProjects/DSA//decrypt.txt";
    decryptFile(encryptedFilePath, decryptedFilePath, sKey);
    } catch (Exception e) {
    System.out.println(e.getMessage());
    }
    }
    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
    try {
    byte[] signature = signFile(encryptedFilePath, privateKey);
    saveSignature(signatureFilePath, signature);
    boolean isVerified = verifySignature(encryptedFilePath, signatureFilePath, publicKey);
    jTextField2.setText(" " + isVerified);
    } catch (Exception e) {
    System.out.println(e.getMessage());
    }
    }
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(() -> {
    new DSA2().setVisible(true);
    });
    }
    private void encryptFile(String originalFilePath, String encryptedFilePath, SecretKey sKey) throws Exception {
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, sKey);
    try (FileInputStream fis = new FileInputStream(originalFilePath);
    FileOutputStream fos = new FileOutputStream(encryptedFilePath);
    CipherOutputStream cos = new CipherOutputStream(fos, cipher)) {
    byte[] buffer = new byte[1024];
    int bytesRead;
    while ((bytesRead = fis.read(buffer)) != -1) {
    cos.write(buffer, 0, bytesRead);
    }
    cos.flush();
    }
    }
    private byte[] signFile(String encryptedFilePath, PrivateKey privateKey) throws Exception {
    Signature dsaSigner = Signature.getInstance("SHA256withDSA");
    dsaSigner.initSign(privateKey);
    try (FileInputStream fis = new FileInputStream(encryptedFilePath)) {
    byte[] buffer = new byte[1024];
    int bytesRead;
    while ((bytesRead = fis.read(buffer)) != -1) {
    dsaSigner.update(buffer, 0, bytesRead);
    }
    }
    return dsaSigner.sign();
    }
    private void saveSignature(String signatureFilePath, byte[] signature) throws Exception {
    try (FileOutputStream fos = new FileOutputStream(signatureFilePath)) {
    fos.write(signature);
    }
    }
    private void decryptFile(String encryptedFilePath, String decryptedFilePath, SecretKey sKey) throws Exception {
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, sKey);
    try (FileInputStream fis = new FileInputStream(encryptedFilePath);
    CipherInputStream cis = new CipherInputStream(fis, cipher);
    FileOutputStream fos = new FileOutputStream(decryptedFilePath)) {
    byte[] buffer = new byte[1024];
    int bytesRead;
    while ((bytesRead = cis.read(buffer)) != -1) {
    fos.write(buffer, 0, bytesRead);
    }
    JOptionPane.showMessageDialog(null, "File Decrypted successfully");
    fos.flush();
    }
    }
    private boolean verifySignature(String encryptedFilePath, String signatureFilePath, PublicKey publicKey) throws Exception {
    byte[] signature;
    try (FileInputStream fis = new FileInputStream(signatureFilePath)) {
    signature = new byte[fis.available()];
    fis.read(signature);
    }
    Signature dsaVerifier = Signature.getInstance("SHA256withDSA");
    dsaVerifier.initVerify(publicKey);
    try (FileInputStream fis = new FileInputStream(encryptedFilePath)) {
    byte[] buffer = new byte[1024];
    int bytesRead;
    while ((bytesRead = fis.read(buffer)) != -1) {
    dsaVerifier.update(buffer, 0, bytesRead);
    }
    }
    return dsaVerifier.verify(signature);
    }
    }
Step 1
  • To start the encryption process, select the input file path after setting up the DSA encryption GUI.
  • Encrypt and Decrypt data using digital-signature-algorithm1
  • Encrypt and Decrypt data using digital-signature-algorithm2
Step 2
  • The chosen file location is indicated by the selected file path that shows up in the text field.
  • Encrypt and Decrypt data using digital-signature-algorithm3
Step 3
  • The files input.txt and Encrypt.txt are chosen. When the Encrypt button is clicked, encryption is successfully finished.
  • Encrypt and Decrypt data using digital-signature-algorithm4
Step 4
  • 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.
  • Encrypt and Decrypt data using digital-signature-algorithm5
Step 5
  • 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.
  • Encrypt and Decrypt data using digital-signature-algorithm6
  • Encrypt and Decrypt data using digital-signature-algorithm7
  • Encrypt and Decrypt data using digital-signature-algorithm8
Step 6
  • If the signature is valid, display a confirmation message on the GUI indicating the authenticity of the public keys. This message assures the user that the keys come from the intended party and that the key exchange process is secure.
  • Encrypt and Decrypt data using digital-signature-algorithm9
  • Encrypt and Decrypt data using digital-signature-algorithm10