2013-03-10 73 views
1

我尝试在java中编写一个加密包,它封装了javax.crypto包并使它更加易于使用。所以我实现了一个只包装javax.crypto的RSACore类。然后我实现了两个类RSAEncoder和RSADecoder来隐藏javax.crypto.Cipher并启动它。javax.crypto.BadPaddingException:数据散列错误

现在我的问题: 我已经写了一些RSACore,RSAEncoder和RSADecoder的小测试。 除了解码器之外,所有测试都可以正常工作。当我对编码数据调用dofinal()时,每次都会遇到BadPaddingException。 我搜索了它,并发现在大多数情况下,它必须与错误的键,但我相信我只生成一对,并在其上工作。我不知道在哪里搜索错误。 作为供应商我使用BouncyCastle的,因为与JCE我得到了一个错误,签约是不可能的OAEP 下面是完整的ErrorMessage:

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(); 
     } 
    } 
} 

你看到有任何逻辑错误? 我也可以发布编码器,解码器和核心,但很多线路。 感谢您的阅读和帮助!

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的相关方法。 – 2013-03-10 10:50:04

+0

完成它在我原来的帖子编辑 – Coding 2013-03-10 12:13:19

回答

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; 
} 

除了第一个字节,所有其他字节都将为0,因为index在此循环中永不改变。

+0

omg!非常感谢!!! atm我无法测试,但我认为这可能是错误。这也解释了我的另一个错误。 – Coding 2013-03-10 14:24:21

+0

解决了这个问题。谢谢! – Coding 2013-03-11 00:27:10