javax.crypto.badpaddingexception decryption error unpadv15

48 Views Asked by At

After generating and encrypting a license file, I'm encountering a decryption error when trying to open the encrypted file. I've debugged the code and tried different padding types, but haven't been successful. The code block is provided below. What do you think could be the issue?

static ObjectMapper objectMapper=new ObjectMapper();

public static void main(String[] args) throws Exception {
    KeyPairGenerator keyPairGenerator  = KeyPairGenerator.getInstance ("RSA");
    keyPairGenerator.initialize(2048);
    KeyPair pair = keyPairGenerator.generateKeyPair();
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DATE, 1);
    LicenseImportDTO licenseData = LicenseImportDTO.builder()
            .licenseKey ("123DS92-SAKH-SKH-22")
            .contact("WORLD A.Ş.")
            .customerName(DigestUtils.md5Hex("WORLD"))
            .cluster(false)
            .numberOfNodes(0)
            .numberOfSites(2)
            .duration(12)
            .licenseType(LicenseType.BASE)
            .deviceSize(DeviceSize.SMALL).build();

    String encrpyLicense = encryptExportLicenceDTO(licenseData,pair.getPublic());
    Thread.sleep(2000);
    readLicenseData(encrpyLicense,pair.getPrivate());
}

public static String encryptExportLicenceDTO(LicenseImportDTO licenceDTO, PublicKey publicKey) throws NoSuchAlgorithmException, NoSuchPaddingException, JsonProcessingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, InvalidKeySpecException {
    KeyGenerator keyGen = KeyGenerator.getInstance("AES");
    keyGen.init(128);
    SecretKey secretKey = keyGen.generateKey();
    Cipher aesCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    aesCipher.init(Cipher.ENCRYPT_MODE, secretKey);
    byte[] encryptedBytes = aesCipher.doFinal(objectMapper.writeValueAsString(licenceDTO).getBytes());
    Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey);
    byte[] encryptedSymmetricKey = rsaCipher.doFinal(secretKey.getEncoded());
    byte[] combined = new byte[encryptedBytes.length + encryptedSymmetricKey.length];
    System.arraycopy(encryptedSymmetricKey, 0, combined, 0, encryptedSymmetricKey.length);
    System.arraycopy(encryptedBytes, 0, combined, encryptedSymmetricKey.length, encryptedBytes.length);
    return Base64.getEncoder().encodeToString(combined);
}

private static LicenseImportDTO readLicenseData(String  encodedLicense , PrivateKey privateKey) throws Exception {
    byte[] combined = Base64.getDecoder().decode(encodedLicense);
    byte[] encryptedSymmetricKey = new byte[128];
    byte[] encryptedBytes = new byte[combined.length - 128];
    System.arraycopy(combined, 0, encryptedSymmetricKey, 0, 128);
    System.arraycopy(combined, 128, encryptedBytes, 0, encryptedBytes.length);
    Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    rsaCipher.init(Cipher.DECRYPT_MODE, privateKey);
    byte[] symmetricKeyBytes = rsaCipher.doFinal(encryptedSymmetricKey);
    SecretKey secretKey = new SecretKeySpec(symmetricKeyBytes, 0, symmetricKeyBytes.length, "AES");
    Cipher aesCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    aesCipher.init(Cipher.DECRYPT_MODE, secretKey);
    byte[] decryptedBytes = aesCipher.doFinal(encryptedBytes);
    String decryptedJson = new String(decryptedBytes, StandardCharsets.UTF_8);
    ObjectMapper objectMapper = new ObjectMapper();
    return objectMapper.readValue(decryptedJson, LicenseImportDTO.class);
}

I try everything but not fix this bug. My expecthing is read the license contents.

0

There are 0 best solutions below