2013-02-20 2 views
0

J'essaye de crypter et décrypter une chaîne en utilisant TrippleDES algorythm et sans utiliser l'encodage Base64 (mon application parlera à une autre application qui a ces exigences). Tout fonctionnait à merveille lorsque je testais des choses en utilisant le codage/décodage Base64, mais quand je suis passé à le faire en mode texte brut (comme l'application que j'appelle exige), tout s'est cassé.TrippleDES sans encodage base 64: Bloc final donné pas correctement rembourré

J'ai lu ce post Given final block not properly padded qui affirme que la clé est erroné sur le décodage, mais qui ne peut être, parce que ces lignes passent effectivement dans les mêmes variables pour la clé et la transformation:

ecipher = Cipher.getInstance(transformation); 
dcipher = Cipher.getInstance(transformation); 
ecipher.init(Cipher.ENCRYPT_MODE, key, iv); 
dcipher.init(Cipher.DECRYPT_MODE, key, iv); 

de plus, je l'ai imprimé les longueurs des deux la chaîne codée et le tableau, leurs longueurs sont des multiples de 8.

Ma sortie avec je reçois:

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 

Mon code complet (version légèrement modifiée de ce tutoriel 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 

Merci à l'avance!

Répondre

1

Vous effectuez le codage hexadécimal dans le mauvais ordre. Vous devez décoder le texte chiffré au lieu d'encoder le texte brut dans votre méthode decrypt.

+0

OUI !!! Ça a marché! Merci beaucoup! – Creature

+0

@Creature heureux que cela a fonctionné. Notez que triple est orthographié avec un seul "p", même si cela ne sonne pas comme ça. –

Questions connexes