2010-05-27 4 views
0

현재 BadPaddingException 오류가 발생했습니다. 암호화 된 데이터를 클라이언트에서 서버로 보내고 있습니다. 서버는 수신 된 데이터에 대해 해독을 수행합니다.
누구든지 코드를 살펴보고 오류를 지적하는 데 도움이 될 수 있습니까?BadPaddingException RSA 암호화/암호 해독 오류

클라이언트 측

try{ 
    Cipher c = Cipher.getInstance("RSA"); 
    c.init(Cipher.ENCRYPT_MODE, servPubKey); 
    String myMessage = "this is a secret message"; 
    byte[] msgByte = myMessage.getBytes(); 
    ObjectOutputStream outVehicle3 = new ObjectOutputStream(socket.getOutputStream()); 
    ParamClass print4 = new ParamClass (cipherText); 
    outVehicle3.writeObject(print4); 
} catch (Throwable e) { 
    // TODO Auto-generated catch block 
    tv.append("\nUnable to perform RSA encryption!"); 
    e.printStackTrace(); 
} 

서버 측

ObjectInputStream inServ3 = new ObjectInputStream(socket.getInputStream()); 
try{ 
    ParamClass print5 = (ParamClass) (inServ3.readObject()); 
    Cipher dec = Cipher.getInstance("RSA"); 
    dec.init(Cipher.DECRYPT_MODE, serverPrivateKey); 
    byte[] dectyptedText = dec.doFinal(print5.cipherText); 
    String decipherText = dectyptedText.toString(); 
    System.out.println("\nDecrypted Message to string: " + decipherText); 
}catch (Throwable e) { 
    e.printStackTrace(); 
    System.out.println("Unable to perform RSA decryption!"); 
} 

는 에러가 다음 라인 occoured했다.

byte[] dectyptedText = dec.doFinal(print5.cipherText); 

귀하의 도움과 안내에 미리 감사드립니다.

답변

2

난 당신이 클라이언트 코드에서이 줄 누락 가정이 제외

byte[] cipherText = c.doFinal(msgByte); 

, 가장 가능성이 오류가 servPubKeyserverPrivateKey이 (일치하지 않는다는 것입니다을 즉, 그들은 동일한 아닌 두 반쪽은 RSA 키 쌍).

일치하는지 확인하는 빠른 방법은 servPubKey.getModulus() 및 serverPrivateKey.getModulus()를 인쇄하여 일치하는지 확인하는 것입니다.

관련 문제