2011-11-11 1 views
0

저는 두 개의 개별 소프트웨어를 사용하여 네트워크 연결의 양쪽 끝에서 정보를 암호화하고 암호 해독하는 Java 소프트웨어를 개발 중입니다. 이를 쉽게하기 위해 데이터 암호화를 처리하는 클래스 인 Cryptographer가 있습니다. 현재 컨트롤러 (연결의 한 쪽)와 에이전트 (다른 ​​쪽)는이 클래스를 사용하여 두 프로그램간에 공유되는 암호를 기반으로 SecretKey를 생성합니다.동일한 암호 기반 암호화 키를 생성 할 수없는 이유는 무엇입니까?

public SecretKey generateKey(String key) { 
    this._paramSpec = new PBEParameterSpec(this.SALT, this.ITERATION_COUNT); 
    PBEKeySpec spec = new PBEKeySpec(key.toCharArray()); 
    SecretKeyFactory fac = null; 
    try { 
     fac = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); 
    } catch (NoSuchAlgorithmException ex) { 
     ex.printStackTrace(); 
     System.err.println("[ERR] Cryptographer could not create a SecretKeyFactory due to an unsupported algorithm."); 
    } 
    try { 
     if (fac == null) 
      return null; 
     return fac.generateSecret(spec); 
    } catch (InvalidKeySpecException ex) { 
     System.err.println("[ERR] Cryptographer could not generate a SecretKey due to an invalid Key Specification."); 
     ex.printStackTrace(); 
     return null; 
    } 
} 

암호화 자체 암호화 기능 일어난다 :

public byte[] encrypt(byte[] message) { 
    try { 
     this._cipher.init(Cipher.ENCRYPT_MODE, this._key, this._paramSpec); 
    } catch (InvalidKeyException ex) { 
     System.err.println("[ERR] Cryptographer could not encrypt a message because the provided key is invalid."); 
     ex.printStackTrace(); 
     return new byte[0]; 
    } catch (InvalidAlgorithmParameterException ex) { 
     System.err.println("[ERR] Cryptographer could not encrypt a message because the parameters are invalid."); 
     ex.printStackTrace(); 
     return new byte[0]; 
    } 
    try { 
     return this._cipher.doFinal(message); 
    } catch (IllegalBlockSizeException ex) { 
     System.err.println("[ERR] Cryptographer could not encrypt a message due to an illegal block size."); 
     ex.printStackTrace(); 
     return new byte[0]; 
    } catch (BadPaddingException ex) { 
     System.err.println("[ERR] Cryptographer could not encrypt a message due to bad padding."); 
     ex.printStackTrace(); 
     return new byte[0]; 
    } 
} 

되고 상기 복호화 기능에 의해 복호화 얻는다 :

키는 암호 해독 클래스의 함수 생성

public byte[] decrypt(byte[] message) { 
    try { 
     this._cipher.init(Cipher.DECRYPT_MODE, this._key, this._paramSpec); 
    } catch (InvalidKeyException ex) { 
     System.err.println("[ERR] Cryptographer could not decrypt a message because the provided key is invalid."); 
     return new byte[0]; 
    } catch (InvalidAlgorithmParameterException ex) { 
     System.err.println("[ERR] Cryptographer could not decrypt a message because the parameters are invalid."); 
    } 
    try { 
     return this._cipher.doFinal(message); 
    } catch (IllegalBlockSizeException ex) { 
     System.err.println("[ERR] Cryptographer could not decrypt a message due to an illegal block size."); 
     return new byte[0]; 
    } catch (BadPaddingException ex) { 
     System.err.println("[ERR] Cryptographer could not decrypt a message due to bad padding."); 
     return new byte[0]; 
    } 
} 

암호화가 제대로 작동하는 것처럼 보이지만 seriali의 암호를 해독하려고하면 수신 측에서 zed 객체를 처리하면 InvalidKeyException이 발생합니다. 컨트롤러와 에이전트에서 독립적으로 생성 된 키를 비교하면 동일한 암호로 출처를 나타내지 만 동일한 키가 생성되지 않는다는 것을 알 수 있습니다.

이제 자바 암호화가 처음이에요. 그래서 전 여기서 뭔가 잘못하고있는 것 같습니다. 거기에 무작위로 요소가있는 것 같습니다. 목표는 연결의 각면에서 동일한 암호로 동일한 키를 생성하는 것입니다. 내가하는 일이 분명히 틀렸어? 더 많은 코드가 필요하면 알려주십시오. 나는 그것을 게시하게되어 기쁠 것이다.

+1

아마 같은 소금을 사용하지 않고 있습니까? – CodesInChaos

답변

0

내게 던져진 InvalidKeyException은 수신 측에서 키가 어떻게 사용되고 있는지를 보여줍니다. 데이터베이스 또는 파일에 저장 하시겠습니까? 데이터를 암호화하는 데 사용 된 것과 동일한 인코딩에 있는지 확인하십시오.

+0

키를 생성하는 데 사용되는 암호는 일반 텍스트로 저장되고 응용 프로그램으로 읽어 들여 키 생성 기능을 통과합니다. – GradysGhost