-3

The following is my java code for DES decryption:

public static byte[] decrypt(final byte[] value, final String key) throws InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, InvalidKeySpecException, IllegalBlockSizeException, BadPaddingException {
    final DESKeySpec objDesKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
    final SecretKeyFactory objKeyFactory = SecretKeyFactory.getInstance("DES");
    final SecretKey objSecretKey = objKeyFactory.generateSecret(objDesKeySpec);
    final byte[] rgbIV = key.getBytes();
    final IvParameterSpec iv = new IvParameterSpec(rgbIV);
    final Cipher objCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    objCipher.init(2, objSecretKey, iv);
    return objCipher.doFinal(value);
}

And I try to convert it to Ruby code as the following:

def decryption(key, decodeString)
    ALG = 'des'
    cipher = OpenSSL::Cipher::Cipher.new(ALG)
    cipher.decrypt  #choose descryption mode.
    cipher.key = key
    plain = cipher.update(decodeString )
    plain << cipher.final
end

After executing the java and ruby code, I got the same size of bytes, but the contents of bytes are different. Where did I go wrong?

Dennis Meng
  • 5,109
  • 14
  • 33
  • 36
Kevin Huang
  • 53
  • 1
  • 5
  • 3
    You should try Googling the relevant keywords and attempting your own implementation before seeking a solution on SO. – Chris Heald Oct 21 '14 at 18:09
  • 1
    Stack Overflow isn't a code conversion service. Instead, you need to try to come up with a solution, and when you run into problems ask a question. – the Tin Man Oct 21 '14 at 18:10
  • possible duplicate of [How to perform Triple DES calculations in Ruby in hexadecimal?](http://stackoverflow.com/questions/3149288/how-to-perform-triple-des-calculations-in-ruby-in-hexadecimal) – Alexander Oct 21 '14 at 18:35

2 Answers2

3

Thanks for your question!

To do this, use the OpenSSL::Cipher library. Here is a link with some sample code for AES: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/openssl/rdoc/OpenSSL/Cipher.html#class-OpenSSL::Cipher-label-Encrypting+and+decrypting+some+data

To use DES, run this command to see if your Ruby installation supports DES.

puts OpenSSL::Cipher.ciphers
Barett
  • 5,826
  • 6
  • 51
  • 55
  • Thank you. If I do both encryption and decryption in ruby, it works fine. And if encryption and decryption both in Java, it also works find. But if encrypting in Java and decrypting in Ruby, I get the different result from decrypting in Java , as I mentioned in the question above. – Kevin Huang Oct 23 '14 at 05:58
  • did you try investigating how to set the padding and IV values? and that those parts of the algorithm are the same between your Java and Ruby implementations? There are a lot of moving pieces in properly executing crypto, many libraries use slightly different implementations. I'm not intimately familiar with those in Ruby. – Barett Oct 23 '14 at 23:13
  • Hi Barret, I have solved this problem by checking the article below. Thank you for your suggestion. It really helps. – Kevin Huang Oct 27 '14 at 14:57
  • Glad to hear it! :) – Barett Sep 12 '18 at 21:19
3

According to this article : http://43n141e.blogspot.tw/2008/08/des-encryption-java-to-openssl-to-ruby.html , I try the following two steps : 1. Calculate iv value in Java:

String key = "123456"
final byte[] rgbIV = key.getBytes();
final IvParameterSpec iv = new IvParameterSpec(rgbIV);
byte[] ivBytes = iv.getIV();
StringBuffer sbuf = new StringBuffer();
for (byte b : ivBytes) {
    sbuf.append(String.format("%02x", (b & 0xFF)));
}
System.out.println("iv: " + sbuf);

2. Decrypt in Ruby :

def decode(encryptedString, key, iv)
    decrypt = OpenSSL::Cipher::Cipher.new('des-cbc')
    decrypt.decrypt
    decrypt.key = key
    decrypt.iv = iv.scan(/../).map{|b|b.hex}.pack('c*')
    decrypt.update(encryptedString) + decrypt.final
end

and it works !

Kevin Huang
  • 53
  • 1
  • 5