Encryption 无base 64编码的TrippleDES:给定的最终块未正确填充

Encryption 无base 64编码的TrippleDES:给定的最终块未正确填充,encryption,encoding,tripledes,Encryption,Encoding,Tripledes,我正在尝试使用TrippleDES算法加密和解密字符串,而不使用Base64编码(我的应用程序将与另一个具有这些要求的应用程序进行对话)。当我使用Base64编码/解码测试东西时,一切都运行得很好,但当我切换到纯文本风格(就像我调用的应用程序所要求的那样)时,一切都崩溃了 我读过这篇文章,上面说密钥在解码时是错误的,但这不可能,因为这些行实际上为密钥和转换传递了相同的变量: ecipher = Cipher.getInstance(transformation); dcipher = Ciphe

我正在尝试使用TrippleDES算法加密和解密字符串,而不使用Base64编码(我的应用程序将与另一个具有这些要求的应用程序进行对话)。当我使用Base64编码/解码测试东西时,一切都运行得很好,但当我切换到纯文本风格(就像我调用的应用程序所要求的那样)时,一切都崩溃了

我读过这篇文章,上面说密钥在解码时是错误的,但这不可能,因为这些行实际上为密钥和转换传递了相同的变量:

ecipher = Cipher.getInstance(transformation);
dcipher = Cipher.getInstance(transformation);
ecipher.init(Cipher.ENCRYPT_MODE, key, iv);
dcipher.init(Cipher.DECRYPT_MODE, key, iv);
另外,我已经打印出了编码字符串和数组的长度,它们的长度是8的倍数

我的输出与我得到:

originalText: Abcdefgh
number of bites: 16
cryptText: d4167d9e2b3b1b2d1f940bc45099da0a
cryptText.length: 32
cryptText.getBytes().length: 32
Exception in thread "main" javax.crypto.BadPaddingException: Given final block not properly padded
    at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
    at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
    at com.sun.crypto.provider.DESedeCipher.engineDoFinal(DashoA13*..)
    at javax.crypto.Cipher.doFinal(DashoA13*..)
Java Result: 1
我的完整代码(本教程稍作修改的版本):


提前谢谢

您正在以错误的顺序执行十六进制编码。您需要对密文进行解码,而不是在
decrypt
方法中对纯文本进行编码。

@。请注意,triple用一个“p”拼写,尽管听起来不是这样。
package com.test.encrypt;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import org.apache.commons.codec.binary.Hex;

public class TrippleDESTest
{
    private Cipher ecipher;
    private Cipher dcipher;
    private String algorithm = "DESede";
    private String transformation = "DESede/CBC/PKCS5Padding";
    private String keyPhrase = "123456789012345678901234"; //your keyphrase 24 bit
    private SecretKey key;
    private IvParameterSpec iv;
    private static TrippleDESTest cryptoUtil;
    private String ENCODING = "UTF-8";

    public static TrippleDESTest getInstance() throws Exception
    {
        if (cryptoUtil == null)
        {
            cryptoUtil = new TrippleDESTest();
        }

        return cryptoUtil;
    }

    private TrippleDESTest() throws Exception
    {
            DESedeKeySpec keySpec = new DESedeKeySpec(keyPhrase.getBytes());
            key = SecretKeyFactory.getInstance(algorithm).generateSecret(keySpec);
            iv = new IvParameterSpec(new byte[8]);
            ecipher = Cipher.getInstance(transformation);
            dcipher = Cipher.getInstance(transformation);
            ecipher.init(Cipher.ENCRYPT_MODE, key, iv);
            dcipher.init(Cipher.DECRYPT_MODE, key, iv);
    }

    public String encrypt(String str) throws Exception
    {
            byte[] utf8 = str.getBytes(ENCODING);    
            byte[] enc = ecipher.doFinal(utf8);                
            System.out.println("number of bites: " + enc.length);    
            return Hex.encodeHexString(enc);
    }

    public String decrypt(String str) throws Exception
    {
            byte[] dec = str.getBytes();
            byte[] utf8 = dcipher.doFinal(dec);    
            return Hex.encodeHexString(utf8);
    }

    public static void main(String[] args) throws Exception
    {
        TrippleDESTest test = TrippleDESTest.getInstance();        
        String originalText = "Abcdefgh";        
        System.out.println("originalText: " + originalText);        
        String cryptText = test.encrypt(originalText);        
        System.out.println("cryptText: " + cryptText);        
        System.out.println("cryptText.length: " + cryptText.length());
        System.out.println("cryptText.getBytes().length: " + cryptText.getBytes().length);        
        System.out.println("decote text: " + test.decrypt(cryptText));

    }
}// end class TrippleDESTest