2012-05-16 3 views
1

3Des를 사용하는 제 3 자로부터 값을 해독하는 데 문제가 있습니다. 그들은 나에게 키, 암호화 된 값, 사용 된 모드, 해독 된 값이 무엇인지 알려주지 만, 지금까지는 포인트 a에서 b로가는 방법을 찾지 못했습니다. 나는 그 문제가 그들이 준 열쇠와 관련이 있다고 믿는다. 그들은 그것이 명백한 텍스트 키라고 말하지만, 나는 여전히 어떻게 든 더 변형 될 필요가 있다고 생각한다.Java를 사용하여 3Des의 값을 암호 해독

코드는 다음과 같습니다 (이 경우에 AC9C5A46A63FC9EA) 값을 해독하는 방법을

모든 통찰력을 주시면 감사하겠습니다을 이해하는 내 초기 시도의 예입니다.

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

당신이 직면 한 문제는 무엇을 써야합니다. –

답변

2

당신은 등 SecretKeyFactory, 그러나 주와 필요보다 더 복잡하고 있습니다 : 그것은에 16 진수 문자열로 변환

는 외부 라이브러리를 필요로 바이트 배열 표현 등 apache.commons.codec.binary 등의 문제는 16 진수를 올바르게 변환하지 않는다는 것입니다.

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. 그것은 누락 된 링크였습니다. –

0

주요 문제는 바이트 배열 getBytes()로 변환된다 진수 데이터를 포함하는 스트링의 사용량을 보인다.

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