1

我尝试在 java 中编写一个加密包,它包装了 javax.crypto 包并使其使用起来更舒适。所以我实现了一个只包装 javax.crypto 的 RSACore 类。然后我实现了两个类 RSAEncoder 和 RSADecoder 来隐藏 javax.crypto.Cipher 和它的启动。

现在我的问题是:我已经为 RSACore、RSAEncoder 和 RSADecoder 编写了一些小测试。除了解码器的测试之外,所有测试都可以正常工作。当我对编码数据调用 dofinal() 时,我每次都会收到 BadPaddingException。我用谷歌搜索它,发现在大多数情况下它与错误的键有关,但我确信我只生成一对并处理它。我不知道在哪里搜索错误。作为提供者,我使用 bouncycastle,因为在使用 JCE 时,我收到一个错误,即无法使用 OAEP 进行签名以下是完整的错误消息:

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

你看到有什么逻辑错误吗?我也可以发布编码器、解码器和核心,但那是很多行。感谢您的阅读和帮助!

RSA编码器:

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

RSA核心:

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

1 回答 1

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在这个循环中永远不会改变。

于 2013-03-10T13:32:24.890 回答