Home > Java > javaTutorial > How to Ensure Successful Encryption and Decryption with 3DES in Java?

How to Ensure Successful Encryption and Decryption with 3DES in Java?

Susan Sarandon
Release: 2024-11-16 05:50:02
Original
378 people have browsed it

How to Ensure Successful Encryption and Decryption with 3DES in Java?

How to Encrypt and Decrypt Text Using 3DES in Java

When encrypting and decrypting text using the Triple DES (3DES) algorithm in Java, verifying the accuracy of the process is crucial. If you encounter issues where the decrypted text does not match the original string, it's likely due to minor errors in the code. Here is a concise solution that ensures both encryption and decryption work as expected:

The code below uses Message Digest 5 (MD5) for key generation and initializes the Cipher object with specific algorithm parameters:

public static void main(String[] args) throws Exception {
    String text = "kyle boon";

    byte[] codedtext = new TripleDESTest().encrypt(text);
    String decodedtext = new TripleDESTest().decrypt(codedtext);

    System.out.println(decodedtext); // Prints "kyle boon"
}
Copy after login
public byte[] encrypt(String message) throws Exception {
    // Generate key bytes using MD5 digest
    MessageDigest md = MessageDigest.getInstance("md5");
    byte[] digestOfPassword = md.digest("HG58YZ3CR9".getBytes("utf-8"));
    byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
    for (int j = 0, k = 16; j < 8;) {
        keyBytes[k++] = keyBytes[j++];
    }

    // Create SecretKey and IvParameterSpec
    SecretKey key = new SecretKeySpec(keyBytes, "DESede");
    IvParameterSpec iv = new IvParameterSpec(new byte[8]);

    // Initialize Cipher object
    Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key, iv);

    // Encrypt plain text
    byte[] plainTextBytes = message.getBytes("utf-8");
    return cipher.doFinal(plainTextBytes);
}
Copy after login
public String decrypt(byte[] message) throws Exception {
    // Generate key bytes using MD5 digest
    MessageDigest md = MessageDigest.getInstance("md5");
    byte[] digestOfPassword = md.digest("HG58YZ3CR9".getBytes("utf-8"));
    byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
    for (int j = 0, k = 16; j < 8;) {
        keyBytes[k++] = keyBytes[j++];
    }

    // Create SecretKey and IvParameterSpec
    SecretKey key = new SecretKeySpec(keyBytes, "DESede");
    IvParameterSpec iv = new IvParameterSpec(new byte[8]);

    // Initialize Cipher object
    Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
    decipher.init(Cipher.DECRYPT_MODE, key, iv);

    // Decrypt cipher text
    return new String(decipher.doFinal(message), "UTF-8");
}
Copy after login

Ensure that the key used for encryption matches the key used for decryption. Verify that the input string is accurately encrypted and decrypted by comparing it to the original string. If you follow these guidelines, you should be able to successfully encrypt and decrypt text using 3DES in Java.

The above is the detailed content of How to Ensure Successful Encryption and Decryption with 3DES in Java?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template