小编use*_*751的帖子

当数据阵列的第一个条目为零时,使用RSA加密获得太短的数组

我试图使用java bouncy castle库来使用RSA来解密和解密一个短字节数组.我做了以下步骤:

  1. 生成2048位密钥对.
  2. 创建一个短数据字节数组,其中第一个条目为零.
  3. 使用生成的密钥和RSA加密数据阵列.
  4. 使用生成的密钥和RSA解密加密的数据阵列.
  5. 比较原始和解密的数据数组.

我注意到原始和解密的数据数组不一样.解密的数据数组缺少第一个条目,因此比原始数据数组短1.仅当数据数组的第一个条目为"0"时才会发生这种情况.为什么会这样?解密不应该返回相同的数据数组吗?或者我对图书馆的假设,使用和理解是错误的?

这里是完整的测试用例(带有导入以便更好地理解):

import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.math.BigInteger;
import java.security.*;
import java.security.spec.RSAKeyGenParameterSpec;
import java.util.Arrays;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.jce.provider.JDKKeyPairGenerator;
import org.bouncycastle.util.encoders.Hex;
import org.hive2hive.core.H2HJUnitTest;
import org.junit.Test;

public class EncryptionUtil2Test {

    @Test
    public void testBug() throws IOException, InvalidKeyException, IllegalBlockSizeException,
        BadPaddingException, DataLengthException, IllegalStateException, InvalidCipherTextException,
        NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException,
        InvalidAlgorithmParameterException {

        Security.addProvider(new BouncyCastleProvider());

        // generate RSA keys
        BigInteger publicExp = new BigInteger("10001", 16); // Fermat …
Run Code Online (Sandbox Code Playgroud)

java arrays encryption rsa bouncycastle

5
推荐指数
1
解决办法
702
查看次数

标签 统计

arrays ×1

bouncycastle ×1

encryption ×1

java ×1

rsa ×1