2012-05-16 1 views
1

Je n'arrive pas à déchiffrer une valeur provenant d'un tiers utilisant des images 3D. Ils m'ont donné une clé, une valeur cryptée, le mode utilisé, et quelle devrait être la valeur décryptée, mais jusqu'à présent, je n'ai pas réussi à trouver comment passer du point a au point b. Je crois que le problème a à voir avec la clé qu'ils m'ont donnée - ils disent que c'est la clé du texte, mais je pense que cela doit encore être transformé d'une manière ou d'une autre.Décryptage d'une valeur en 3D à l'aide de Java

Le code ci-dessous est un exemple de ma première tentative de comprendre comment décrypter la valeur (AC9C5A46A63FC9EA dans ce cas)

Toute autre idée serait apprécié.

import java.security.spec.KeySpec; 
import javax.crypto.Cipher; 
import javax.crypto.SecretKey; 
import javax.crypto.SecretKeyFactory; 
import javax.crypto.spec.DESedeKeySpec; 

public class TripleDes2 { 

private static final String UNICODE_FORMAT = "UTF8"; 
private static final String DESEDE_ENCRYPTION_SCHEME = "DESede"; 
private static final String CIPHER_ALG = "DESede/ECB/Nopadding"; //assuming no padding 
private KeySpec ks; 
private SecretKeyFactory skf; 
private Cipher cipher; 
private byte[] arrayBytes; 
private String myEncryptionKey; 
private SecretKey key; 

public static void main(String args []) throws Exception { 
    TripleDes2 td= new TripleDes2(); 

    String decrypted = td.decrypt("AC9C5A46A63FC9EA"); 
    System.out.println("expecting: 04286EDDFDEA6BD7"); 
    System.out.println("found: " + decrypted); 
} 

public TripleDes2() throws Exception { 
    myEncryptionKey = "1032FD2CD64A9D7FA4D061F76B04BFEA"; 
    arrayBytes = myEncryptionKey.getBytes(UNICODE_FORMAT); 
    ks = new DESedeKeySpec(arrayBytes); 
    skf = SecretKeyFactory.getInstance(DESEDE_ENCRYPTION_SCHEME); 

    cipher = Cipher.getInstance(CIPHER_ALG); 
    key = skf.generateSecret(ks); 
} 

public String decrypt(String encryptedString) { 
    String decryptedText=null; 
    try { 
     cipher.init(Cipher.DECRYPT_MODE, key); 
     byte[] encryptedText = encryptedString.getBytes(); 
     byte[] plainText = cipher.doFinal(encryptedText); 
     decryptedText= new String(plainText); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return decryptedText; 
} 
} 
+0

À quel problème vous devez faire face. –

Répondre

2

Vous faites plus complexe que vous devez le SecretKeyFactory, etc. Mais la principale Le problème est que vous ne convertissez pas correctement les nombres hexadécimaux.

class TripleDES 
{ 

    private final String key; 

    public static void main(String... args) 
    throws Exception 
    { 
    TripleDES td = new TripleDES("1032FD2CD64A9D7FA4D061F76B04BFEA"); 
    String decrypted = td.decrypt("AC9C5A46A63FC9EA"); 
    System.out.println("expecting: 04286EDDFDEA6BD7"); 
    System.out.println("found: " + decrypted); 
    } 

    TripleDES(String key) 
    { 
    this.key = key; 
    } 

    public String decrypt(String input) 
    throws Exception 
    { 
    byte[] tmp = h2b(this.key); 
    byte[] key = new byte[24]; 
    System.arraycopy(tmp, 0, key, 0, 16); 
    System.arraycopy(tmp, 0, key, 16, 8); 
    Cipher cipher = Cipher.getInstance("DESede/ECB/NoPadding"); 
    cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "DESede")); 
    byte[] plaintext = cipher.doFinal(h2b(input)); 
    return b2h(plaintext); 
    } 

    private static byte[] h2b(String hex) 
    { 
    if ((hex.length() & 0x01) == 0x01) 
     throw new IllegalArgumentException(); 
    byte[] bytes = new byte[hex.length()/2]; 
    for (int idx = 0; idx < bytes.length; ++idx) { 
     int hi = Character.digit((int) hex.charAt(idx * 2), 16); 
     int lo = Character.digit((int) hex.charAt(idx * 2 + 1), 16); 
     if ((hi < 0) || (lo < 0)) 
     throw new IllegalArgumentException(); 
     bytes[idx] = (byte) ((hi << 4) | lo); 
    } 
    return bytes; 
    } 

    private static String b2h(byte[] bytes) 
    { 
    char[] hex = new char[bytes.length * 2]; 
    for (int idx = 0; idx < bytes.length; ++idx) { 
     int hi = (bytes[idx] & 0xF0) >>> 4; 
     int lo = (bytes[idx] & 0x0F); 
     hex[idx * 2] = (char) (hi < 10 ? '0' + hi : 'A' - 10 + hi); 
     hex[idx * 2 + 1] = (char) (lo < 10 ? '0' + lo : 'A' - 10 + lo); 
    } 
    return new String(hex); 
    } 

} 
+0

Huzza. C'était le chaînon manquant. –

0

Le principal problème semble l'utilisation de chaînes de caractères contenant des données hexadécimal qui sont convertis par l'intermédiaire d'getBytes() pour un tableau d'octets.

Pour convertir une chaîne hexagonale pour sa représentation de tableau d'octets dont vous avez besoin d'une bibliothèque externe, comme apache.commons.codec.binary:

myEncryptionKey = "1032FD2CD64A9D7FA4D061F76B04BFEA"; 
arrayBytes = Hex.decodeHex(myEncryptionKey.toCharArray());