2

I've read a few articles on doing this and I think there is a variable I'm not considering here.

I'm going through someone else's code, where they have implemented triple-des encryption. They do not have the key and iv stored somewhere as a string. Only as byte arrays.

I need to make this same encrypt/decrypt call outside of C#, so I'm trying to find out the keys used.

Things I've tried:

byte[] key; // this is set
byte[] iv; // this is set

Convert.ToBase64String(key);
Encoding.Default.GetString(key);
Encoding.ASCII.GetString(key);
Encoding.UTF8.GetString(key);

Maybe the results from one of these is correct, but I need to do another conversion to encode it?

In case it matters, I'm simply trying to use these keys with openssl via ruby or node.js.

For what it's worth, I've also tried creating an array of the bytes in ruby and doing a pack on them, but still seems like the wrong result.

John Agan
  • 333
  • 4
  • 12

2 Answers2

0

Have you tried using the straightforward hex representation of your byte arrays?

string keyHex = BitConverter.ToString(key).Replace("-", "");
string ivHex = BitConverter.ToString(iv).Replace("-", "");

(You might also need to lowercase the strings and/or prefix them with 0x, depending on how fussy the other systems are.)

LukeH
  • 263,068
  • 57
  • 365
  • 409
0

Ok, I also had to reproduce a .NET cryptography routine in a Ruby code. After a couple of hours banging my head to the wall, here's what I've figured. Forget about Base64, use this code for the C# part:

BitConverter.ToString(key).Replace("-", "")

Store the result. Now define this function in Ruby:

def self.hex_to_bin(s) 
  s.scan(/../).map { |x| x.hex.chr }.join
end

And call it with the output from the C# part, it's going to convert the hexa string into a bin string. The dealbreaker for me was to make this conversion at the Ruby side. It works for both the iv and the key.

Thiago Ganzarolli
  • 1,161
  • 12
  • 17