首页 >Java >java教程 >为什么Java中的3DES加解密通常无法将密文解密回原始字符串,如何解决这个问题?

为什么Java中的3DES加解密通常无法将密文解密回原始字符串,如何解决这个问题?

Patricia Arquette
Patricia Arquette原创
2024-11-11 03:21:02311浏览

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?

Java中的3DES加密解密

通常,使用3DES加密和解密Java中的字符串时会出现一个常见问题,导致无法将密文成功解密回原始字符串。本文提供了一个简单的代码示例,可以对字符串进行加密和解密,并将其还原为原始字符串。

在原问题中,给出的代码存在以下错误:

  • 在加密方法中,直接返回加密结果,而不是将其进行Base64编码。
  • 在解密方法中,直接将加密结果作为解密数据的输入,而不是先进行Base64解码。

已更正的代码如下:

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");
    }
}

使用了正确的Base64编码和解码步骤后,代码就可以成功对字符串进行3DES加密和解密,并确保解密结果正确还原为原始字符串。

以上是为什么Java中的3DES加解密通常无法将密文解密回原始字符串,如何解决这个问题?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn