0

I need to encrypt a text string in a .net application using a known text key, and then decrypt it in Ruby using the same known key...however I'm having trouble making this happen. I think it has to do with string encoding issues. .Net to Ruby RC4 examples have proven to be elusive.

I'm receiving and invalid decryption on the Ruby side. The encryption/decryption works fine on the .net implementation. But when I copy the encrypted value over to the Ruby implementation and use the same key, I don't get my original value.

Below is my .net RC4 implementation (this does not require the highest level of security, but some is nice) :)

On the ruby side I'm using ruby-rc4 https://github.com/caiges/Ruby-RC4

       public string keytext = "thisismykey";
    public Form1()
    {
        InitializeComponent();
    }
    public void RC4(ref Byte[] bytes, Byte[] key)
    {

        Byte[] s = new Byte[256];
        Byte[] k = new Byte[256];
        Byte temp;
        int i, j;

        for (i = 0; i < 256; i++)
        {
            s[i] = (Byte)i;
            k[i] = key[i % key.GetLength(0)];
        }

        j = 0;
        for (i = 0; i < 256; i++)
        {
            j = (j + s[i] + k[i]) % 256;
            temp = s[i];
            s[i] = s[j];
            s[j] = temp;
        }

        i = j = 0;
        for (int x = 0; x < bytes.GetLength(0); x++)
        {
            i = (i + 1) % 256;
            j = (j + s[i]) % 256;
            temp = s[i];
            s[i] = s[j];
            s[j] = temp;
            int t = (s[i] + s[j]) % 256;
            bytes[x] ^= s[t];
        }
    }
    static byte[] GetBytes(string str)
    {
        Byte[] bytes = new byte[str.Length * sizeof(char)];
        System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
        return bytes;
    }
    static string GetString(byte[] bytes)
    {
        char[] chars = new char[bytes.Length / sizeof(char)];
        System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
        return new string(chars);
    }
    private void button1_Click(object sender, EventArgs e)
    {
        Byte[] content = Encoding.ASCII.GetBytes(textBox1.Text);
        RC4(ref content, Encoding.ASCII.GetBytes(keytext));
        textBox2.Text = Encoding.ASCII.GetString(content);
        RC4(ref content, Encoding.ASCII.GetBytes(keytext));
        label1.Text = Encoding.ASCII.GetString(content);
    }
Webjedi
  • 4,677
  • 7
  • 42
  • 59
  • Can you elaborate on exactly what trouble you are having? An error message? An invalid result? – PinnyM May 21 '12 at 18:35
  • Updated question with: I'm receiving and invalid decryption on the Ruby side. The encryption/decryption works fine on the .net implementation. But when I copy the encrypted value over to the Ruby implementation and use the same key, I don't get my original value. – Webjedi May 21 '12 at 18:44
  • What are you using to transfer the hash from .net to ruby? Is there any specific encoding you are using on the ruby end (UTF-8 instead of ASCII)? – PinnyM May 21 '12 at 19:44
  • Oh, and can you verify if ruby encryption yields the same hash as .NET? That would help pin the source of the problem down... – PinnyM May 21 '12 at 20:11
  • .net yields "-" (without quotes) Ruby yields "<\x85-\x1F" (without quotes) I'm using notepad to transfer the values...(presently) I plan to ultimate stuff them into a database. – Webjedi May 21 '12 at 21:06
  • Is the '?' really a question mark or an unprintable character? Can you extract the binary data (before conversion to ASCII/UTF-8) and compare the byte values? – PinnyM May 21 '12 at 21:13
  • 60, 133, 45, 31 - This is the byte arrary that gets put into textBox2, and the label1 get set to the original word "star"...so it round trips within the .net program. – Webjedi May 21 '12 at 21:21
  • 1
    Why RC4? It's very easy to use incorrectly, becoming insecure. For example your code is utterly broken, because if you use the same key twice without an IV, it's trivial to calculate the xor of plaintexts, eliminating the encryption. And thanks to RC4's related key weaknesses, using an IV in the wrong way is easy too. That's how WEP got broken. – CodesInChaos May 21 '12 at 21:36
  • I largely agree with you CodeInChaos...in this case this encryption is to be used during the migration from one system to another where it will be re-encrypted...this part of the step is transitory...but your point is made. – Webjedi May 21 '12 at 21:39

1 Answers1

0

The problem is likely occurring because ASCII encoding is 7-bit and has trouble recreating characters for values above 127. Try encoding with UTF-8 or convert the byte array to a Base64 string.

PinnyM
  • 35,165
  • 3
  • 73
  • 81