2017-08-05 72 views
1

我有一段代碼負責加密和解密,需要在Ruby中進行轉換。在發佈之前,我經歷了4個鏈接,但沒有運氣。對Ruby的Java加密/解密

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 

爲了讓我寫了下面的代碼相同的結果

我的努力:紅寶石

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代碼的區別。

任何幫助將不勝感激!

+0

「任何幫助將不勝感激! - 你的代碼有什麼麻煩?你有錯誤信息嗎?什麼是錯誤信息?你得到的結果不是你期待的結果嗎?你期望得到什麼結果?爲什麼?你得到的結果是什麼?兩者有什麼不同?你正在觀察的行爲不是所期望的行爲?什麼是期望的行爲,爲什麼,觀察到的行爲是什麼,以及它們以何種方式不同? –

+0

@JörgWMittag謝謝你的意見我已經編輯了我的問題並帶有預期的結果,在這裏可以注意到密碼是不同的。 – VKatz

回答

2

您需要使用IV和密鑰從Java實例,而不是一個新的/隨機IV /鍵:

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 
1

您需要提供相同InitVector和加密和兩個解密方法的關鍵。

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

include Base64 

plain_text = "abceeffslaj" 

cipher = OpenSSL::Cipher::AES128.new(:CBC) 
cipher.encrypt 
key = "Bar12345Bar12345" 
iv = "RandomInitVector" 
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