Problem:
Unable to successfully encrypt and decrypt a string using 3DES encryption due to recurring errors in code implementation.
Solution:
To address the issue, let's analyze the provided code:
public class TripleDESTest { // ... public byte[] encrypt(String message) { // ... final byte[] plainTextBytes = message.getBytes("utf-8"); final byte[] cipherText = cipher.doFinal(plainTextBytes); final String encodedCipherText = new sun.misc.BASE64Encoder().encode(cipherText); return cipherText; } // ... }
Base64 Encoding Misconception:
Initially, the code included the line final String encodedCipherText = new sun.misc.BASE64Encoder().encode(cipherText); for Base64 encoding the ciphertext before returning it. However, since Base64 encoding wasn't being used in the decryption method, the ciphertext should be returned directly as a byte array.
Printing Raw Byte Arrays:
The code prints both the encrypted and decrypted data as byte arrays: System.out.println(codedtext); and System.out.println(decodedtext);. This doesn't provide meaningful output as byte arrays don't render as human-readable values. To display the actual decrypted text, convert it to a string using new String(plainText, "UTF-8").
Corrected Code:
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(decodedtext); } 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); 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[] plainText = decipher.doFinal(message); return new String(plainText, "UTF-8"); } }
The above is the detailed content of Why Is My Java 3DES Encryption/Decryption Failing?. For more information, please follow other related articles on the PHP Chinese website!