From: Joshua Mckinney on
I'm here are all the methods for encryption

def mysql_encrypt(s, key)
encrypt(s, mysql_key(key))
end

def mysql_decrypt(s, key)
puts s
decrypt(s, mysql_key(key))
end

protected
def aes(m,k,t)
(aes = OpenSSL::Cipher::AES128.new("ECB").send(m)).key = k
aes.update(t) << aes.final
end

def encrypt(text, key)
aes(:encrypt, key, text)
end

def decrypt(text, key)
aes(:decrypt, key, text)
end

def mysql_key(key)
key = key.encode("UTF-8")
final_key = "\0" * 16
key.length.times do |i|
final_key[i%16] = (final_key[i%16].ord ^ key[i].ord).chr
end
final_key

end


When is use the key set to "test key" and string to be encrypted set to
"Some text for encryption". The returned encrypted string is:
"\x9E\xB6g\xB7\xF0\xF8\x9F
M\xC1\x82\xA0\xFC\xEF[hY\xEC]=\xE6U\xE8o\xFBN\xCD\x929\x9A\xF4\xB5"

But the encrypted string should be:
"9EB667B7F0F89F204DC182A0FCEF5B6859EC5D3DE655E86FFB4ECD92399AF4B5"
(generated from C# mysql_mimic and Mysql itself)

My encrypted string appears some hexadecimal values interlaced and I'm
not sure why or how "decode" it properly to match the desire encryption
results.

The above results were produced in ruby 1.9.1 (which is what we are
using for this project)

Thanks





Rob Biedenharn wrote:
>
> What final_key do you expect? You need to post the full code and
> input along with the expected value (or what 1.8.6 gives?) in order to
> help.
>
> -Rob

--
Posted via http://www.ruby-forum.com/.

From: Joshua Mckinney on
Looks like was I given some skewed information from our C# fella.

"\x9E\xB6g\xB7\xF0\xF8\x9F
M\xC1\x82\xA0\xFC\xEF[hY\xEC]=\xE6U\xE8o\xFBN\xCD\x929\x9A\xF4\xB5".unpack("H*")
= ["9eb667b7f0f89f204dc182a0fcef5b6859ec5d3de655e86ffb4ecd92399af4b5"]

which means everything it is working :-)

My only issue now is taking
"9eb667b7f0f89f204dc182a0fcef5b6859ec5d3de655e86ffb4ecd92399af4b5" and
"packing" it back to "\x9E\xB6g\xB7\xF0\xF8\x9F
M\xC1\x82\xA0\xFC\xEF[hY\xEC]=\xE6U\xE8o\xFBN\xCD\x929\x9A\xF4\xB5"

Anyone know how to do that?

Thanks,
--
Posted via http://www.ruby-forum.com/.

From: Rob Biedenharn on

On Jul 1, 2010, at 5:27 PM, Joshua Mckinney wrote:

> Looks like was I given some skewed information from our C# fella.
>
> "\x9E\xB6g\xB7\xF0\xF8\x9F
> M\xC1\x82\xA0\xFC\xEF[hY\xEC]=\xE6U\xE8o\xFBN\xCD\x929\x9A
> \xF4\xB5".unpack("H*")
> = ["9eb667b7f0f89f204dc182a0fcef5b6859ec5d3de655e86ffb4ecd92399af4b5"]
>
> which means everything it is working :-)
>
> My only issue now is taking
> "9eb667b7f0f89f204dc182a0fcef5b6859ec5d3de655e86ffb4ecd92399af4b5" and
> "packing" it back to "\x9E\xB6g\xB7\xF0\xF8\x9F
> M\xC1\x82\xA0\xFC\xEF[hY\xEC]=\xE6U\xE8o\xFBN\xCD\x929\x9A\xF4\xB5"
>
> Anyone know how to do that?
>
> Thanks,
> --
> Posted via http://www.ruby-forum.com/.
>


irb>
["9eb667b7f0f89f204dc182a0fcef5b6859ec5d3de655e86ffb4ecd92399af4b5
"].pack("H*")
=> "\x9E\xB6g\xB7\xF0\xF8\x9F M\xC1\x82\xA0\xFC\xEF[hY\xEC]=\xE6U\xE8o
\xFBN\xCD\x929\x9A\xF4\xB5"

Rob Biedenharn
http://agileconsultingllc.com
Rob(a)AgileConsultingLLC.com
http://gaslightsoftware.com
rab(a)GaslightSoftware.com


From: Joshua Mckinney on
Awesome, could not find .pack in the 1.9.1 documentation. Everything is
now working.

Here is all the code for 1.9.1:


def mysql_encrypt(s, key=(a)key)
encrypt(s, mysql_key(key))
end

def mysql_decrypt(s, key=(a)key)
puts s
decrypt(s, mysql_key(key))
end

protected
def aes(m,k,t)
c = OpenSSL::Cipher::Cipher.new('aes-128-ecb').send(m)
c.key = k
c.update(t) + c.final

end

def encrypt(text, key)
aes(:encrypt, key, text).unpack("H*")
end

def decrypt(text, key)
aes(:decrypt, key, [text].pack("H*"))
end

def mysql_key(key)

final_key = "\0" * 16
key.length.times do |i|
final_key[i%16] = (final_key[i%16].ord ^ key[i].ord).chr
end
final_key
end


Thanks for you help Rob



Rob Biedenharn wrote:
> ["9eb667b7f0f89f204dc182a0fcef5b6859ec5d3de655e86ffb4ecd92399af4b5
> "].pack("H*")
> => "\x9E\xB6g\xB7\xF0\xF8\x9F M\xC1\x82\xA0\xFC\xEF[hY\xEC]=\xE6U\xE8o
> \xFBN\xCD\x929\x9A\xF4\xB5"
>
> Rob Biedenharn
> http://agileconsultingllc.com
> Rob(a)AgileConsultingLLC.com
> http://gaslightsoftware.com
> rab(a)GaslightSoftware.com

--
Posted via http://www.ruby-forum.com/.

From: Brian Candler on
Joshua Mckinney wrote:
> Awesome, could not find .pack in the 1.9.1 documentation

It's Array#pack, as opposed to String#unpack.
--
Posted via http://www.ruby-forum.com/.