2013-02-20 58 views
0

我想加密和解密一個字符串使用TrippleDES algorythm,而不使用Base64編碼(我的應用程序將與另一個具有這些要求的應用程序交談)。當我測試使用Base64編碼/解碼的東西時,一切都運行良好,但是當我轉換爲純文本樣式(就像我打電話的應用程序所要求的那樣)時,一切都破裂了。TrippleDES沒有基地64編碼:鑑於最後一塊沒有正確填充

我讀過這篇文章Given final block not properly padded它說,關鍵是錯誤的解碼,但這是不可能的,因爲這些線路實際上是在同一個變量傳遞密鑰和改造兩種:

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 

我完整的代碼(本教程http://eternusuk.blogspot.com/2008/09/java-triple-des-example.html的稍作修改的版本):

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 = "123456789"; //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 

提前感謝!

回答

1

您正在以錯誤的順序執行十六進制編碼。您需要解密密文,而不是在您的decrypt方法中編碼純文本。

+0

是的!!!這工作!非常感謝你! – Creature 2013-02-21 01:55:58

+0

@Creature很高興它的工作。請注意,triple是拼寫爲單個「p」,即使它聽起來不那麼方便。 – 2013-02-21 01:59:16

相關問題