Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javax.crypto.BadPaddingException:解密错误-Can';t解密加密的公钥_Java_Encryption - Fatal编程技术网

javax.crypto.BadPaddingException:解密错误-Can';t解密加密的公钥

javax.crypto.BadPaddingException:解密错误-Can';t解密加密的公钥,java,encryption,Java,Encryption,这是我的加密设置 public static final String ALGORITHM = "RSA"; public static final String CIPHER = "RSA/ECB/PKCS1Padding"; public static final String HASH_ALGORITHM = "SHA-256"; public static final String SIGNATURE_ALGORITHM = "SHA256withRSA"; public static

这是我的加密设置

public static final String ALGORITHM = "RSA";
public static final String CIPHER = "RSA/ECB/PKCS1Padding";
public static final String HASH_ALGORITHM = "SHA-256";
public static final String SIGNATURE_ALGORITHM = "SHA256withRSA";
public static final int KEY_SIZE = 1048;
这是我要加密的代码

public byte[] encrypt(byte[] toEncrypt, String keyPath){

    int keyLength = KEY_SIZE/8 - 11;

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    String toEncryptString = new String(toEncrypt,StandardCharsets.UTF_8);

    String[] lines = toEncryptString.split("(?<=\\G.{" + keyLength + "})");

    byte[] encryptedData = new byte[0];

    try{
        inputStream = new ObjectInputStream(new FileInputStream(keyPath));
        final PublicKey publicKey = (PublicKey) inputStream.readObject();
        final Cipher cipher = Cipher.getInstance(CIPHER);

        cipher.init(Cipher.ENCRYPT_MODE,publicKey);

        if(toEncrypt.length >= keyLength){
            for(String line : lines){
                byteArrayOutputStream.write(cipher.doFinal(line.getBytes("UTF-8")));
            }
        }else{
            byteArrayOutputStream.write(cipher.doFinal(toEncrypt));
        }
        encryptedData = byteArrayOutputStream.toByteArray();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    }

    byte[] cipheredData = base64Encoder(encryptedData);

    System.out.println(Arrays.toString(cipheredData));

    return cipheredData;
}
公共字节[]加密(字节[]到加密,字符串密钥路径){
int keyLength=键大小/8-11;
ByteArrayOutputStream ByteArrayOutputStream=新建ByteArrayOutputStream();
String-toEncryptString=新字符串(toEncrypt,StandardCharsets.UTF_8);

String[]lines=toEncryptString.split((?您的代码没有意义。您试图在明文字符串上拆分密文。它不在那里。拆分字符串时它被删除。在任何情况下,数据都已加密,因此在其中搜索明文是徒劳的


您应该使用base64解码、解密、读取对象,然后使用
”(?我很确定,如果
toEncrypt
是一个base64编码的公钥,它将在base64解码后包含二进制数据,您不能将其视为字符串并拆分它们。您需要通过处理
字节[]来拆分它们
。此外,您的代码还存在其他问题,但这似乎是您应该解决的第一个问题。这段代码中有这么多令人困惑的事情……为什么您选择了1048位的密钥大小?为什么要加密公钥?@ArtjomB。所以我应该拆分字节,而不是将其转换为字符串?@LukePark当我ad加密公钥。在我们的项目中,公钥将用作ID,因此,由于我们在客户机-服务器之间通信,我们必须对其进行加密。如果没有要求,我不会加密公钥。在提供给我们所有人的项目中,我们必须将公钥从客户机发送到服务器并存储,因为公钥y将用作用户的ID。如果我们有一个常规ID,我们会希望它不是纯文本,对吗?公钥也是一样。我们必须加密它。我只是不能用密码对大字节[]进行加密,这就是我拆分字节[]的原因比如说,在较小的块中。为了解密,我也不能将密码文本作为一个整体解密,因此将其拆分并尝试解密。公钥是一个公共实体。如果您试图将其用作身份验证机制,那么您做错了什么,加密它不会改变这一点。任何人都可以使用任何其他公钥加密任何公钥密钥。它完全不能证明什么。你需要的是由用户的私钥签名的东西。这可能是你的任务,但它仍然完全是胡说八道。我不明白你的最后一句话。所以你是说我们可能必须用用户的私钥签名?我的最后一句话是,它给了我一个错误,数据太多了要破译就要大,这就是为什么我试图拆分以破译它。这不是我所说的,这是我所说的,没有“可能”。你的最后一句话没有说“太大而无法破译”。
public byte[] decrypt(byte[] toDecrypt, String keyPath) {
    byte[] decypherText = base64Decoder(toDecrypt);

    System.out.println(toDecrypt.length);
    System.out.println(decypherText.length);

    int keyLength = KEY_SIZE/8 - 11; 

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    String toEncryptString = Arrays.toString(decypherText);

    String[] lines = toEncryptString.split("(?<=\\G.{" + keyLength + "})");

    byte[] decipheredData = new byte[0];

    try{
        inputStream = new ObjectInputStream(new FileInputStream(keyPath));
        final PrivateKey privateKey = (PrivateKey) inputStream.readObject();
        final Cipher cipher = Cipher.getInstance(CIPHER);

        cipher.init(Cipher.DECRYPT_MODE,privateKey);

        if(decypherText.length >= keyLength){
            for(String line : lines){
                byteArrayOutputStream.write(cipher.doFinal(line.getBytes("UTF-8")));
            }
        }else{
            byteArrayOutputStream.write(cipher.doFinal(decypherText));
        }
        decipheredData = byteArrayOutputStream.toByteArray();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    }

    System.out.println(Arrays.toString(decipheredData));

    return decipheredData;
}