views:

52

answers:

2

I have a class that encrypts and decrypts a string. It uses rijndael. Anyways, I am trying to store an encrypted value on the client. That works all fine and dandy. My problem is when I want to decrypt the value, I need the Key and IV(InitVector) that was used to encrypt the string. They are byte[] values. I currently output those to the client through a javascript variable. I need them when I call a service, as this app talks to a mainframe and it needs the users credentials. They output to the client in the form of var vector = [143,147,31,70,195,72,228,190,152,222,65,240,152,183,0,161]; I am able to pass that value to my service as a string. My question is , inside my service, how can I convert this string back into a byte array? I have tried Encoding.ASCII.GetBytes("[143,147,31,70,195,72,228,190,152,222,65,240,152,183,0,161]"); but that is not what I want. That tries to make a new Byte[] from that string value. I need a byte array who's values are that inside the string. Is this possible? Please provide a quick sample if you know how to do this.

Thanks, ~ck in San Diego

+1  A: 

I would encode the bytes in base64, using System.Convert.ToBase64String and FromBase64String.

Edit: this program demonstrates this in more detail

class A{
      public static void Main()
      {
       byte[] a = new byte[]{143,147,31,70,195,72,228,190,152,222,65,240,152,183,0,161};
       string s = System.Convert.ToBase64String(a);
       System.Console.WriteLine(s);
       byte[] b = System.Convert.FromBase64String(s);
       System.Console.Write("[");
       foreach (var n in b)
           System.Console.Write(n+",");
       System.Console.WriteLine("]");
      }
}
Martin v. Löwis
this works great Martin. Now tho when I decrypt, it tacks on null bytes. I just do a .replace("\0", string.Empty) and it seems to be ok. Is there a more elegant way? Thanks All!!!
Hcabnettek
I can't see where you get a null byte from (I also don't understand what you mean by "decrypt" - there is no operation called "decrypt" here anywhere I can see). See my edit; in this program, the result is exactly the same as the input.
Martin v. Löwis
+1  A: 

I think these two methods in C# might help you. I have used these with my crypto routine in order to get a byte[] for my IV and Key. Is it possible that you can output a string returned from the GetString method below in Javascript and then use that value to call the service with?

private static byte[] GetArray(string input)
    {
        List<byte> bytes = new List<byte>();

        for (int i = 0; i < input.Length; i += 2)
        {
            string hex = input.Substring(i, 2);
            bytes.Add(Convert.ToByte(Convert.ToUInt32(hex, 16)));
        }

        return bytes.ToArray();
    }

    private static string GetString(byte[] input)
    {
        StringBuilder buffer = new StringBuilder(input.Length);
        foreach (byte b in input)
        {
            buffer.Append(b.ToString("x2"));
        }
        return buffer.ToString();
    }
Wil P
I was in the process of posting something basically the same. +1
Taylor Leese