Convert Encrypt code in java to Ruby

*爱你&永不变心* 提交于 2019-11-29 10:25:12

问题


I have been trying to convert a code for encrypt in java to ruby, but I am not able to do it completely. I getting different values.

   passphrase = passphrase + STATIC_KEY;
   byte[] key = passphrase.getBytes("UTF-8");

   MessageDigest sha = MessageDigest.getInstance("SHA-1");
   key = sha.digest(key);
   key = Arrays.copyOf(key, 16);
   SecretKey secretKey = new SecretKeySpec(key, "AES");

   Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
   IvParameterSpec initialisationVector = new IvParameterSpec(
           new byte[16]);
   cipher.init(Cipher.ENCRYPT_MODE, secretKey, initialisationVector);

   byte[] encryptedData = cipher.doFinal(plainText.getBytes("UTF-8"));

   return SimpleCrypto.toHex(encryptedData);

Can anyone let me know, how this can be done in it ruby.

  unencrypted = "passphrase"
  c = OpenSSL::Cipher.new("aes-128-cbc")
  c.encrypt
  c.key = Digest::SHA1.hexdigest('secret_key')[0...32]
  e = c.update(unencrypted)
  e << c.final
  return e

回答1:


require 'openssl'

Encrypt:

unencrypted = "I am a secret!"

initialize the Cipher for encrypt

cipher = OpenSSL::Cipher::AES.new(128, :CBC)
cipher.encrypt

create the key using SHA1

key = Digest::SHA1.hexdigest('secret_key')[0...32]
cipher.key = key

create the initialisationVector with an input

iv = Digest::SHA1.hexdigest('secret_iv')[0...32]
cipher.iv = iv

or create a random initialisationVector

iv = cipher.random_iv

run the encryption

encrypted = cipher.update(unencrypted) + cipher.final

Decrypt:

initialize the Cipher for decrypt

decipher = OpenSSL::Cipher::AES.new(128, :CBC)
decipher.decrypt

load the key and initialisationVector

decipher.key = key
decipher.iv = iv

decrypt the plaintext

plain = decipher.update(encrypted) + decipher.final

Does it match?

puts unencrypted == plain #=> true

For more information look at the Ruby Docs for the Class - OpenSSL::Cipher




回答2:


Encrypt Code:

def aes(key,string)
  cipher = OpenSSL::Cipher::Cipher.new("aes-128-cbc")
  cipher.encrypt
  cipher.padding = 1
  cipher.key = hex_to_bin(Digest::SHA1.hexdigest('secret_key')[0..32])
  cipher_text = cipher.update(string)
  cipher_text << cipher.final
  return bin_to_hex(cipher_text).upcase
end

Decrypt Code:

def aes_decrypt(key, encrypted)
  encrypted = hex_to_bin(encrypted.downcase)
  cipher = OpenSSL::Cipher::Cipher.new("aes-128-cbc")
  cipher.decrypt
  cipher.padding = 1
  cipher.key = hex_to_bin(Digest::SHA1.hexdigest('secret_key')[0..32])
  d = cipher.update(encrypted)
  d << cipher.final
end

hex_to_bin and bin_to_hex

def hex_to_bin(str)
  [str].pack "H*"
end

def bin_to_hex(s)
  s.unpack('C*').map{ |b| "%02X" % b }.join('')
end

In My case, The java code was using default initialization vector, So I did not set any iv, Also, there was hex_to_bin was a missing piece there. So after that, all started working properly.

I hope it helps someone if they come across this issue.



来源:https://stackoverflow.com/questions/28235569/convert-encrypt-code-in-java-to-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!