Ошибка дешифрования AES-GCM iaik.cms.CMSException: Невозможно расшифровать зашифрованный ключ шифрования содержимого: Неверное заполнение - PullRequest
0 голосов
/ 22 февраля 2019

Я использую следующий код для шифрования данных с помощью AES-GCM:

// the stream to which to write the EnvelopedData
ByteArrayOutputStream resultStream = new ByteArrayOutputStream();
EnvelopedDataOutputStream envelopedData;

//  wrap EnvelopedData into a ContentInfo 
ContentInfoOutputStream contentInfoStream = 
  new ContentInfoOutputStream(ObjectID.cms_envelopedData, resultStream);
// create a new EnvelopedData object encrypted with AES
try {
 envelopedData = new EnvelopedDataOutputStream(contentInfoStream,                                                     
 (AlgorithmID)AlgorithmID.aes256_GCM.clone());
} catch (NoSuchAlgorithmException ex) {
 throw new CMSException("No implementation for AES.");
}

// create the recipient infos
RecipientInfo[] recipients = new RecipientInfo[1];
// user1 is the first receiver
recipients[0] = new KeyTransRecipientInfo(signCert, 
(AlgorithmID)AlgorithmID.rsaEncryption.clone()); 

// specify the recipients of the encrypted message
envelopedData.setRecipientInfos(recipients);
//int blockSize = 2048; // in real world we would use a block size like 2048
//  write in the data to be encrypted
//byte[] buffer = new byte[blockSize];
byte[] buffer = new byte[16];
//byte[] buffer;
int bytesRead;
while ((bytesRead = inputstream.read(buffer)) != -1) {
 envelopedData.write(buffer, 0, bytesRead);
}

Но при попытке расшифровать то же самое с помощью следующего кода:

EnvelopedDataStream enveloped_data = new EnvelopedDataStream(is);
enveloped_data.setupCipher(privateSignKey, 0);
InputStream decrypted = enveloped_data.getInputStream();
ByteArrayOutputStream os = new ByteArrayOutputStream();      
ByteStreams.copy(decrypted, os);
os.close();

Я получаю следующую ошибку:

iaik.cms.CMSException: Unable to decrypt encrypted content-encryption key: Invalid padding!

Я не уверен в размере блока, который будет использоваться, это может быть причиной ошибки, но не уверен.

Более того, если я сравню свой зашифрованный файл с этимправильной, я вижу следующую разницу:

Мои зашифрованные данные:

OBJECT IDENTIFIER 2.16.840.1.101.3.4.1.46 aes256-GCM (NIST Algorithm)
   SEQUENCE (1 elem)
     OCTET STRING (12 byte) FE5729470184A04A5AA30158

Правильные данные:

OBJECT IDENTIFIER 2.16.840.1.101.3.4.1.46 aes256-GCM (NIST Algorithm)
SEQUENCE (2 elem)
  OCTET STRING (12 byte) CA985F7EAFB709DF711DCA2A
  INTEGER 16

Не могли бы вы помочь нам получить эторабота

...