I am kind of stuck on this 412: Invalid symmetric key length error from the Polish Government JPK_VAT Submission API.
The requirement is to encrypt the JPK.XML.ZIP file using AES 256 CBC PKCS#7 BlockSize 16 and IV Random 16 key.
Then to encrypt the AES KEY using public key provided by polish ministry of Finance. Then sign the XML with XAdES-BES.
Than make call to InitUpload -> UPload the ZIP -> Finish upload.
Till this point all works, but when I get the status I am getting above error i.e.
"Code": 412, "Description": "Dokument nieprawidłowo zaszyfrowany", "Details": "Nieprawidłowa długość klucza symetrycznego"
As per my analysis I suspect it might be issue with the JAVA encryption and .NET decryption (Assuming the Polish Gov API is in C#).
Can you help in pointing what I may be doing wrong?
Below is my code:
Generate AES Key:
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(256);
SecretKey secretKey = keyGenerator.generateKey();
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecureRandom r = new SecureRandom();
byte[] iv = new byte[cipher.getBlockSize()];
r.nextBytes(iv);
IvParameterSpec ivParams = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParams);
//This will encrypt the ZIP with AES
byte[] encryptedByte = cipher.doFinal(zipFile);
//Now will encrypt the AES key with RSA
//Get Public key
InputStream certstream = new ByteArrayInputStream (Files.readAllBytes(path));
Certificate cert = CertificateFactory.getInstance("X.509").generateCertificate(certstream);
PublicKey key = cert.getPublicKey();
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptedKey=cipher.doFinal(secretKey.getEncoded());
String encodedKey = Base64.getEncoder().encodeToString(encryptedKey);
Then I sign the XML.