2013-03-10 2 views
1

javax.crypto 패키지를 래핑하는 java에 암호화 패키지를 작성하려고 시도하고 사용하기가 더 편리합니다. 그래서 javax.crypto 만 감싸는 RSACore 클래스를 구현했습니다. 그런 다음, RSAEncoder와 RSADecoder라는 두 클래스를 구현하여 javax.crypto.Cipher를 숨기고 RSA를 시작했습니다.javax.crypto.BadPaddingException : 데이터 해시가 잘못됨

내 문제 : RSACore, RSAEncoder 및 RSADecoder에 대한 몇 가지 작은 테스트를 작성했습니다. 디코더의 테스트를 제외하고는 모든 테스트가 제대로 작동합니다. 인코딩 된 데이터에서 dofinal()을 호출 할 때마다 BadPaddingException이 발생합니다. 나는 그것을 찾아 봤는데 대부분의 경우 잘못된 키와 관련이있다. 그러나 나는 한 쌍을 생성하고 그것에 대해 작업 할 것이라고 확신한다. 나는 어디에서 오류를 찾을 지 모른다. 테스트 여기

javax.crypto.BadPaddingException: data hash wrong 
at org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi.engineDoFinal(Unknown Source) 
at javax.crypto.Cipher.doFinal(Cipher.java:2145) 
at de.coding.crypto.rsa.RSACore.doFinal(RSACore.java:178) 
at de.coding.crypto.rsa.RSADecoder.doFinal(RSADecoder.java:59) 
at de.coding.crypto.interfaces.AbstractDecoder.doFinal(AbstractDecoder.java:49) 
at de.coding.crypto.rsa.RSADecoderTest.test(RSADecoderTest.java:21) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
at java.lang.reflect.Method.invoke(Unknown Source) 
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) 
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:300) 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 

됩니다 :

package de.coding.crypto.rsa; 

import static org.junit.Assert.*; 

import java.security.KeyPair; 

import org.junit.Test; 

public class RSADecoderTest 
{ 
    @Test 
    public void test() 
    { 
     try 
     { 
      KeyPair kp = RSACore.generateKeys(); 
      String str = "Hello world! This is me. Life should be fun for everyone."; 
      RSAEncoder encoder = new RSAEncoder(kp.getPublic()); 
      byte[] encoded = encoder.doFinal(str.getBytes()); 
      RSADecoder decoder = new RSADecoder(kp.getPrivate()); 
      byte[] decoded = decoder.doFinal(encoded);//<-------------------BadPaddingException 
      assertTrue(str.getBytes().length == decoded.length); 
      int size = decoded.length; 
      for (int i=0; i<size; i++) 
      { 
       assertTrue(decoded[i] == str.getBytes()[i]); 
      } 
     } 
     catch (Exception e) 
     { 
      fail(); 
     } 
    } 
} 

당신이 볼 수 있나요 I 때문에 JCE와 bouncycastle를 사용 제공 업체로서 나는 서명이 완료 ErrorMessage가 여기에 OAEP 가능합니다 아니라고 오류가있어 논리적 오류가 있습니까? 나는 또한 인코더, 디코더 및 코어를 게시 할 수 있지만 그 수는 많습니다. 읽고 도와 주셔서 감사합니다!

RSAEncoder :

public RSAEncoder(PublicKey key) throws InvalidKeyException 
{ 
    super(); 
    cipher = RSACore.initEncodeMode(key); 
} 

public byte[] doFinal() 
{ 
    try 
    { 
     buffer.add(RSACore.doFinal(cipher));//buffer is a LinkedList<byte[]> 
     return clearBuffer(); 
    } 
    catch (IllegalBlockSizeException e) 
    { 
     throw new RuntimeException(e); 
    } 
    catch (BadPaddingException e) 
    { 
     throw new RuntimeException(e); 
    } 
} 

protected byte[] clearBuffer() 
{ 
    byte[] ret = new byte[getBufferSize()]; 
    int index = 0; 
    for (byte[] b : buffer) 
    { 
     if (b != null) 
     { 
      for (int i=0; i<b.length; i++) 
      { 
       ret[index] = b[i]; 
      } 
     } 
    } 
    buffer.clear(); 
    return ret; 
} 

public byte[] doFinal(byte[] b, int offset, int length) 
{ 
    if (length > RSACore.KEYSIZE-11) 
    { 
     update(b, offset, length); 
     return doFinal(); 
    } 
    else 
    { 
     try 
     { 
      buffer.add(RSACore.doFinal(cipher, b, offset, length)); 
      return clearBuffer(); 
     } 
     catch (IllegalBlockSizeException e) 
     { 
      throw new RuntimeException(e); 
     } 
     catch (BadPaddingException e) 
     { 
      throw new RuntimeException(e); 
     } 
    } 
} 

RSADecoder (condtructor 제외하고 동일) :

public RSADecoder(PrivateKey key) throws InvalidKeyException 
{ 
    cipher = RSACore.initDecodeMode(key); 
} 

RSACore :

public byte[] doFinal(byte[] b) 
{ 
    return doFinal(b, 0, b.length); 
} 

public static byte[] doFinal(Cipher cipher, byte[] msg, int offset, int length) throws IllegalBlockSizeException, BadPaddingException 
{ 
    byte[] ret = cipher.doFinal(msg, offset, length); 
    if (ret == null) 
    { 
     return new byte[0]; 
    } 
    return ret; 
} 

public static Cipher initEncodeMode(Key key) throws InvalidKeyException 
{ 
    try 
    { 
     Cipher cipher = Cipher.getInstance(ALGORITHMDETAIL, PROVIDER); 
     cipher.init(Cipher.ENCRYPT_MODE, key); 
     return cipher; 
    } 
    catch (NoSuchAlgorithmException e) 
    { 
     throw new RuntimeException(e); 
    } 
    catch (NoSuchPaddingException e) 
    { 
     throw new RuntimeException(e); 
    } 
    catch (NoSuchProviderException e) 
    { 
     throw new RuntimeException(e); 
    } 
} 

public static Cipher initDecodeMode(Key key) throws InvalidKeyException 
{ 
    try 
    { 
     Cipher cipher = Cipher.getInstance(ALGORITHMDETAIL, PROVIDER); 
     cipher.init(Cipher.DECRYPT_MODE, key); 
     return cipher; 
    } 
    catch (NoSuchAlgorithmException e) 
    { 
     throw new RuntimeException(e); 
    } 
    catch (NoSuchPaddingException e) 
    { 
     throw new RuntimeException(e); 
    } 
    catch (NoSuchProviderException e) 
    { 
     throw new RuntimeException(e); 
    } 
} 
+0

RSAEncoder 및 RSADecoder의 관련 방법을 제공하십시오. –

+0

편집 완료 내 원래 게시물 – Coding

답변

1

당신이 연결리스트가 필요한 이유를 잘 모르겠어요,하지만 clearBuffer() 방법은 분명히 잘못되었습니다 :

protected byte[] clearBuffer() { 
    byte[] ret = new byte[getBufferSize()]; 
    int index = 0; 
    for (byte[] b : buffer) 
    { 
     if (b != null) 
     { 
      for (int i=0; i<b.length; i++) 
      { 
       ret[index] = b[i]; 
      } 
     } 
    } 
    buffer.clear(); 
    return ret; 
} 

index이이 루프에서 결코 변경되지 않기 때문에 첫 번째 바이트를 제외하고 다른 모든 바이트는 0이됩니다.

+0

omg! 많이 감사합니다!!! atm 나는 그것을 테스트 할 수 없지만 이것이 오류 일 수 있다고 생각합니다. 그것도 또 다른 오류를 설명합니다. – Coding

+0

문제가 해결되었습니다. 고맙습니다! – Coding