本文介绍了AES / CBC / PKCS5Padding加密在java解密ruby中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在尝试加密java中的数据并解密ruby中的数据。I am trying to encrypt data in java and decrypt data in ruby.我发现几乎相同的问题,但我的情况略有不同。I found almost same asks, but my case is little bit different. 用Ruby加密和用Java解密 - 为什么它不起作用? 用Java加密AES / CBC,用Ruby解密我的代码是... 加密javaMy code is ...Encrypt in javaimport java.util.HashMap;import java.util.Map;import javax.crypto.Cipher;import javax.crypto.SecretKey;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;import net.sf.json.JSONObject;import org.apache.commons.codec.binary.Hex;class Crypt { public static void main(String[] args) throws Exception { Map<String, String> node = new HashMap<String, String>(); node.put("timestamp", "1377499097199"); JSONObject jsonObject = JSONObject.fromObject(node); String json = jsonObject.toString(); System.out.println(json); //key String skeyString = "97128424897797a166913557a6f4cc8e"; byte[] skey = Hex.decodeHex(skeyString.toCharArray()); System.out.println("key : " + skeyString); //iv String ivString = "84e8c3ea8859a0e293941d1cb00a39c3"; byte[] iv = Hex.decodeHex(ivString.toCharArray()); System.out.println("iv : " + ivString); //encrypt SecretKeySpec skeySpec1 = new SecretKeySpec(skey, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec1, new IvParameterSpec(iv)); byte[] encrypted = cipher.doFinal(json.getBytes()); String encryptedString = Hex.encodeHexString(encrypted); System.out.println("=============>"); System.out.println("encrypted string: " + encryptedString); }}结果是{"timestamp":"1377499097199"}key : 97128424897797a166913557a6f4cc8eiv : 84e8c3ea8859a0e293941d1cb00a39c3=============>encrypted string: 395f6c0e8ad27f57c4a5a8975aa633e5b26f288d37ce18c6971779951f3b3527我希望在Ruby中解密(加密字符串)and I hope to decrypt (encrypted string) in Ruby Ruby代码是...(得到错误)Ruby code is ...(got error)require 'openssl'key = "97128424897797a166913557a6f4cc8e"iv = "84e8c3ea8859a0e293941d1cb00a39c3"encrypted_string = "395f6c0e8ad27f57c4a5a8975aa633e5b26f288d37ce18c6971779951f3b3527"de_cipher = OpenSSL::Cipher::Cipher.new("AES-256-CBC")de_cipher.decryptde_cipher.key = keyde_cipher.iv = ivde_cipher.update(encrypted_string) << de_cipher.final我希望得到{"timestamp":"1377499097199"}但它返回错误`final': bad decrypt (OpenSSL::Cipher::CipherError)我认为问题是cipher.padding和key / iv的类型。 但我不知道如何完成ruby代码。I think the problem is cipher.padding and type of key/iv.But I don't know exactly how to complete ruby code.请告诉我如何填写此代码。Please let me know How to complete this code.谢谢。推荐答案您的Ruby代码存在两个问题。There's two problems with your Ruby code.首先,当您使用AES 128时,您使用的是AES 256. Java根据您使用的密钥大小使用AES 128或256,并且您使用的是128位密钥。First, you're using AES 256 when you should be using AES 128. Java uses AES 128 or 256 based on the size of the key you use, and you're using a 128 bit key.其次,你需要Hex解码你的键, iv 和 encrypted_string 值。 OpenSSL Cipher期待二进制,而不是十六进制字符串。Second, you need to Hex decode your key, iv, and encrypted_string values in Ruby. OpenSSL Cipher is expecting binary, not hex strings.require 'openssl';key = "97128424897797a166913557a6f4cc8e";iv = "84e8c3ea8859a0e293941d1cb00a39c3";encrypted_string = "395f6c0e8ad27f57c4a5a8975aa633e5b26f288d37ce18c6971779951f3b3527";de_cipher = OpenSSL::Cipher::Cipher.new("AES-128-CBC");de_cipher.decrypt;de_cipher.key = [key].pack('H*');de_cipher.iv = [iv].pack('H*');puts de_cipher.update([encrypted_string].pack('H*')) << de_cipher.final;输出:{"timestamp":"1377499097199"} 这篇关于AES / CBC / PKCS5Padding加密在java解密ruby中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-06 02:04