Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby AES CBC解密的Java等价物_Java_Ruby_Encryption_Aes - Fatal编程技术网

Ruby AES CBC解密的Java等价物

Ruby AES CBC解密的Java等价物,java,ruby,encryption,aes,Java,Ruby,Encryption,Aes,下面的ruby代码可以工作 require 'openssl' require "base64" cipher = OpenSSL::Cipher::AES256.new(:CBC) cipher.decrypt cipher.key = Base64.strict_decode64("LLkRRMSAlD16lrfbRLdIELdj0U1+Uiap0ihQrRz7HSQ=") cipher.iv = Base64.strict_decode64("A23OFOSvsC4U

下面的ruby代码可以工作

require 'openssl'
require "base64"

cipher = OpenSSL::Cipher::AES256.new(:CBC)   
cipher.decrypt   
cipher.key = Base64.strict_decode64("LLkRRMSAlD16lrfbRLdIELdj0U1+Uiap0ihQrRz7HSQ=")    
cipher.iv = Base64.strict_decode64("A23OFOSvsC4UyejA227d8g==")
crypt = cipher.update(Base64.strict_decode64("D/e0UjAwBF+d8aVqZ0FpXA=="))    
crypt << cipher.final
puts crypt # prints Test123

为简单起见,密钥和iv是硬编码的

您告诉它要加密,而不是解密。正确的代码行是

cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"), new IvParameterSpec(iv));
此外,如果要为此使用BouncyCastle,请使用

Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", BouncyCastleProvider.PROVIDER_NAME);
或将BouncyCastle设置为默认值:

Security.insertProviderAt(new BouncyCastleProvider(), 1);

cipher.decrypt
不会转换为
cipher.ENCRYPT\u模式
@zapl。谢谢我真傻。“AES/CBC/PKCS5Padding”的前导空格被截断
Security.insertProviderAt(new BouncyCastleProvider(), 1);