Java加密/解密到Ruby

标签 java ruby encryption cryptography

我有一段 Java 代码负责加密和解密,需要将其转换为 Ruby。在此处发帖之前,我通过了 4 个链接,但没有成功。

aes-cbc-pkcs5padding-encrypt-in-java-decrypt-in-ruby

aes-cbc-pkcs5padding-implementation-in-ruby-for-rails

gist.github.com

symmetric encryption algorithms in Ruby

* 
    /**
     * 
     */
    package in.bets.gsm.util;
    
    import javax.crypto.Cipher;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;
    
    import org.apache.commons.codec.binary.Base64;
    
    /**
     * @author VKatz
     *
     */
    public class SecurePath {
    
        /**
         * 
         */
        public SecurePath() {
            // TODO Auto-generated constructor stub
        }
        
        public static String key = "Bar12345Bar12345";
        public static String initVector = "RandomInitVector"; 
        
        public static String encrypt(String value) {
            try {
                IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
                SecretKeySpec [skeySpec][4] = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
    
                Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
                cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
    
                byte[] encrypted = cipher.doFinal(value.getBytes());
                System.out.println("encrypted string: "
                        + Base64.encodeBase64String(encrypted));
    
                return Base64.encodeBase64String(encrypted);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
    
            return null;
        }
    
        public static String decrypt(String encrypted) {
            try {
                IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
                SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
    
                Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
                cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
    
                byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));
    
                return new String(original);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
    
            return null;
        }
        
    public static void main(String[] args) {
            
            String encText = encrypt("abceeffslaj");
           
            System.out.println("Decripted text ::  " + decrypt("XZy6gJinORmH+LOiZL6/Jw=="));
        }
    
    }



Output: 
Simple Text ::  abceeffslaj
Encrypted  text ::  XZy6gJinORmH+LOiZL6/Jw==
Decripted Text ::  abceeffslaj

为了得到相同的结果,我写了下面的代码

我的努力:Ruby

require "openssl"
require "base64"
require 'byebug'

include Base64

plain_text = "abceeffslaj"

cipher = OpenSSL::Cipher::AES128.new(:CBC)
cipher.encrypt
key = cipher.random_key
iv = cipher.random_iv
cipher_text = cipher.update(plain_text) + cipher.final

cipher = OpenSSL::Cipher::AES128.new(:CBC)
cipher.decrypt
cipher.key = key
cipher.iv = iv
decrypted_plain_text = cipher.update(cipher_text) + cipher.final

puts "AES128 in CBC mode"
puts "Key: " + urlsafe_encode64(key)
puts "Iv: " + urlsafe_encode64(iv)
puts "Plain text: " + plain_text
puts "Cipher text: " + urlsafe_encode64(cipher_text)
puts "Decrypted plain text: " + decrypted_plain_text

输出:

AES128 in CBC mode
Key: CJ-SNuUllNKl1vAllEazKg==
Iv: ZMb2W6K07oaAXuvoL8Ckpg==
Plain text: abceeffslaj
Cipher text: jyutt1ljXW9Xn-HFxpvcEg==
Decrypted plain text: abceeffslaj

我们可能会注意到这里的密文与 Java 代码不同。

非常感谢任何帮助!

最佳答案

您需要使用 Java 示例中的 IV 和 key ,而不是新的/随机的 IV/ key :

require "openssl"
require "base64"
require 'byebug'

include Base64

plain_text = "abceeffslaj"

key = 'Bar12345Bar12345'
iv = 'RandomInitVector'

cipher = OpenSSL::Cipher::AES128.new(:CBC)
cipher.encrypt
cipher.key = key
cipher.iv = iv
cipher_text = cipher.update(plain_text) + cipher.final

cipher = OpenSSL::Cipher::AES128.new(:CBC)
cipher.decrypt
cipher.key = key
cipher.iv = iv
decrypted_plain_text = cipher.update(cipher_text) + cipher.final

puts "AES128 in CBC mode"
puts "Key: " + urlsafe_encode64(key)
puts "Iv: " + urlsafe_encode64(iv)
puts "Plain text: " + plain_text
puts "Cipher text: " + urlsafe_encode64(cipher_text)
puts "Decrypted plain text: " + decrypted_plain_text

关于Java加密/解密到Ruby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45518870/

相关文章:

java - Jetty Mutual TLS 身份验证未找到客户端证书

java - 减少Java中多余的变量声明

java - 如何枚举 JDK 的所有 MBean?

ruby - 处理哈希数组

xml - 使用 libxml-ruby 解析命名空间 XML

linux - C 私钥中的 RSA 算法

reactjs - 在我的客户端应用程序中保护本地存储数据的最佳方法是什么

java - 在 Java 中加载 RSA 私钥(algid 解析错误,不是序列)

java - Request.getattribute 在一个 Servlet 中工作,同时在其他 Servlet 中抛出空指针

ruby-on-rails - 如何附加到 Rails 中 ActiveRecord 列上的字符串