` @random_iv = OpenSSL::Random.random_bytes(@iv_random_length)
@cipher.iv = @random_iv
@logger.info('random_iv '+Base64.strict_encode64( @random_iv) )
@logger.info(' data '+Base64.strict_encode64(data) )
result = @cipher.update(data)+@cipher.final
Код: Выделить всё
if @mode == "encrypt"
# if we have a random_iv, prepend that to the crypted result
if !@random_iv.nil?
result = @random_iv + result
end
if @algorithm.downcase.include? "gcm" or @algorithm.downcase.include? "ccm"
result = result + @cipher.auth_tag
end
result = Base64.strict_encode64(result).encode("utf-8") if @base64 == true
end`
`byte[] textBytes = Base64.getDecoder().decode(text);
byte[] iv = new byte[16];ByteBuffer bb = ByteBuffer.wrap(textBytes);
bb.get(iv);
byte[] cipherText = новый байт[bb.remaining()];
bb.get (cipherText);
try {
Cipher cipher = Cipher.getInstance(mode);
SecretKeySpec secretKeySpec = getSecretKeySpec(key);
GCMParameterSpec gcmParameterSpec=new GCMParameterSpec(128, textBytes, 0,16);
System.out.println(Base64.getEncoder().encodeToString(gcmParameterSpec.getIV()));
System.out.println(gcmParameterSpec.getTLen());
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, gcmParameterSpec);
cipher.updateAAD("".getBytes());
byte[] decryptedBytes = cipher.doFinal(textBytes,16,textBytes. длина-16);
Код: Выделить всё
return new String(decryptedBytes, StandardCharsets.UTF_8);
я поместил iv и auth_tag в зашифрованный текст и разделил iv в Java, но теги все равно не совпадают
я проверяю это в nodejs, он работает нормально. Зашифрованный текст, который зашифрован nodejs, можно расшифровать в Java.
'new GCMParameterSpec(128, textBytes,0,16)' я попытался изменить на 'new GCMParameterSpec(96, textBytes,0,16) )'
Подробнее здесь: https://stackoverflow.com/questions/782 ... ed-in-java