Home >Java >javaTutorial >Why does 3DES encryption and decryption in Java usually fail to decrypt the ciphertext back to the original string, and how can this issue be resolved?

Why does 3DES encryption and decryption in Java usually fail to decrypt the ciphertext back to the original string, and how can this issue be resolved?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-11 03:21:02313browse

Why does 3DES encryption and decryption in Java usually fail to decrypt the ciphertext back to the original string, and how can this issue be resolved?

3DES Encryption and Decryption in Java

Usually, a common problem occurs when using 3DES to encrypt and decrypt strings in Java, resulting in the inability to successfully decrypt the ciphertext. Return the original string. This article provides a simple code example that can encrypt and decrypt a string and restore it to the original string.

In the original question, the code given has the following error:

  • In the encryption method, the encryption result is returned directly instead of Base64 encoding it.
  • In the decryption method, the encryption result is directly used as the input of the decrypted data instead of Base64 decoding first.

The corrected code is as follows:

import java.security.MessageDigest;
import java.util.Arrays;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class TripleDESTest {

    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(codedtext); // this is a byte array, you'll just see a reference to an array
        System.out.println(decodedtext); // This correctly shows "kyle boon"
    }

    public byte[] encrypt(String message) throws Exception {
        final MessageDigest md = MessageDigest.getInstance("md5");
        final byte[] digestOfPassword = md.digest("HG58YZ3CR9"
                .getBytes("utf-8"));
        final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        for (int j = 0, k = 16; j < 8;) {
            keyBytes[k++] = keyBytes[j++];
        }

        final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
        final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
        final Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key, iv);

        final byte[] plainTextBytes = message.getBytes("utf-8");
        final byte[] cipherText = cipher.doFinal(plainTextBytes);
        final String encodedCipherText = new sun.misc.BASE64Encoder()
                .encode(cipherText);

        return cipherText;
    }

    public String decrypt(byte[] message) throws Exception {
        final MessageDigest md = MessageDigest.getInstance("md5");
        final byte[] digestOfPassword = md.digest("HG58YZ3CR9"
                .getBytes("utf-8"));
        final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        for (int j = 0, k = 16; j < 8;) {
            keyBytes[k++] = keyBytes[j++];
        }

        final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
        final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
        final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
        decipher.init(Cipher.DECRYPT_MODE, key, iv);

        final byte[] encData = new sun.misc.BASE64Decoder().decodeBuffer(message);
        final byte[] plainText = decipher.doFinal(encData);

        return new String(plainText, "UTF-8");
    }
}

After using the correct Base64 encoding and decoding steps, the code can successfully 3DES encrypt and decrypt the string and ensure The decrypted result is correctly restored to the original string.

The above is the detailed content of Why does 3DES encryption and decryption in Java usually fail to decrypt the ciphertext back to the original string, and how can this issue be resolved?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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