Java 错误填充异常

Java 错误填充异常,java,encryption,message-digest,Java,Encryption,Message Digest,我正在尝试一个程序,首先对字符串进行加密和解密,然后将其编码为64base,然后将其解码为64base(这是必需的)。但是我得到了下面的错误。可能的解决办法是什么 Exception in thread "main" javax.crypto.BadPaddingException: Decryption error at java.base/sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:378) at java.bas

我正在尝试一个程序,首先对字符串进行加密和解密,然后将其编码为64base,然后将其解码为64base(这是必需的)。但是我得到了下面的错误。可能的解决办法是什么

Exception in thread "main" javax.crypto.BadPaddingException: Decryption error
    at java.base/sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:378)
    at java.base/sun.security.rsa.RSAPadding.unpad(RSAPadding.java:290)
    at java.base/com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:366)
    at java.base/com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:392)
    at java.base/javax.crypto.Cipher.doFinal(Cipher.java:2202)
    at CryptographyExample.decrypt(encryt_decrypt.java:53)
    at CryptographyExample.main(encryt_decrypt.java:88)
我的代码

class CryptographyExamples {

    private static final String ALGORITHM = "RSA";

    public static byte[] encrypt(byte[] publicKey, byte[] inputData) throws Exception {
        PublicKey key = KeyFactory.getInstance(ALGORITHM).generatePublic(new X509EncodedKeySpec(publicKey));
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] encryptedBytes = cipher.doFinal(inputData);
        return encryptedBytes;
    }

    public static byte[] decrypt(byte[] privateKey, byte[] inputData) throws Exception {
        PrivateKey key = KeyFactory.getInstance(ALGORITHM).generatePrivate(new PKCS8EncodedKeySpec(privateKey));
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] decryptedBytes = cipher.doFinal(inputData);
        return decryptedBytes;
    }

    public static KeyPair generateKeyPair() throws NoSuchAlgorithmException, NoSuchProviderException {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM);
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
        // 512 is keysize
        keyGen.initialize(512, random);
        KeyPair generateKeyPair = keyGen.generateKeyPair();
        return generateKeyPair;
    }

    public static MessageDigest md;

    public static void main(String[] args) throws Exception {
        String originalMessage = "The message to be encrypted and sent";
        md = MessageDigest.getInstance("SHA-256");
        KeyPair generateKeyPair = generateKeyPair();
        byte[] publicKey = generateKeyPair.getPublic().getEncoded();
        byte[] privateKey = generateKeyPair.getPrivate().getEncoded();
        byte[] encryptedData = encrypt(publicKey, originalMessage.getBytes());
        byte[] shaEncryptedData = md.digest(encryptedData);
        String shaEncryption64 = Base64.getEncoder().encodeToString(shaEncryptedData);
        byte[] decryptedData = decrypt(privateKey, Base64.getDecoder().decode(shaEncryption64));
        System.out.println("Decrypted Message: " + new String(decryptedData));
    }
}

为什么要对加密数据进行SHA-256摘要,并将其命名为SHANCRYPTEDDATA?SHA-256是单向散列,不是加密。这基本上破坏了你的加密数据。我必须在这里实现一个简单的签名方法。所以在主程序中,我有加密消息和加密消息的SHA-256散列。稍后,我需要它们来检查接收方接收的消息是否未被更改,而不是解密加密消息时产生的实际消息。但是您尝试解密您的SHA-256哈希,而不是加密消息。为什么要对加密数据进行SHA-256摘要,并将其称为shaEncryptedData?SHA-256是单向散列,不是加密。这基本上破坏了你的加密数据。我必须在这里实现一个简单的签名方法。所以在主程序中,我有加密消息和加密消息的SHA-256散列。稍后,我需要它们来检查接收方接收的消息是否未被更改,而不是解密加密消息时产生的实际消息。但是您尝试解密的是SHA-256哈希,而不是加密消息。。