need C# code to decode XOR string
|
Seer Xingjian
Registered User
Join date: 29 Sep 2005
Posts: 35
|
11-17-2005 00:16
Can anyone help me with a method in C# to decode a XOR encrypted string from second life? All my attempts so far have failed to get eh correct answer.
|
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
|
11-17-2005 13:06
First off i should say i don't know c# but here is what i hacked together  I have to head out i'll fix this in a couple hours. char [] XorBase64Strings(char [] s1, char[] s2) { byte [] out = new Byte[s1.Length]; byte [] s1 = new Byte[s1.Length]; byte [] s2 = new Byte[s2.Length]; s1 = System.Convert.FromBase64CharArray(s1); s2 = System.Convert.FromBase64CharArray(s2); long a = s1.length(); long b = 0; while(b < a) { s1 } return Convert.ToBase64CharArray() }
def XorBase64Strings(s1, s2): s1 = b64decode(s1) # internally this works with s2 = b64decode(s2) # the decoded strings while len(s2) < len(s1): s2 += s2 # repeat s2 until it's at least as long as s1 out = "" for index in range(len(s1)): # take each character from s1 out += chr( ord(s1[index]) ^ ord(s2[index]) ) # and XOR it with one from s2 return b64encode(out)
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river. - Cyril Connolly
Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence. - James Nachtwey
|
Seer Xingjian
Registered User
Join date: 29 Sep 2005
Posts: 35
|
11-17-2005 13:19
Ok i am at work and sadly i did not bring a encoded string from SL with me to test with. Below is the code i tried to use but it failed with SL XOR strings. works fine for its own but not SL's
private static string NormalStringToBase64String(string normalString) { Encoding asciiEncoding = Encoding.ASCII; byte[] byteArray = new byte[asciiEncoding.GetByteCount(normalString)]; byteArray = asciiEncoding.GetBytes(normalString);
// Perform Base64 transform. return Convert.ToBase64String(byteArray); }
private static string Base64StringToNormalString(string base64String) { byte[] byteArray = Convert.FromBase64String(base64String); Encoding asciiEncoding = Encoding.ASCII; return asciiEncoding.GetString(byteArray); }
public static string EncodeXOR(string CodeKey, string text) { string CodeKey64 = NormalStringToBase64String(CodeKey); string text64 = NormalStringToBase64String(text); string output = PerformXOR(CodeKey64, text64); return NormalStringToBase64String(output); }
public static string DecodeXOR(string CodeKey, string XORtext) { string CodeKey64 = NormalStringToBase64String(CodeKey); string text = Base64StringToNormalString(XORtext); string output = PerformXOR(CodeKey64, text); return Base64StringToNormalString(output); }
//each char in the string is xor'd with another char, namely the key. to decrypt, just run the same function again private static string PerformXOR(string CodeKey, string DataIn) { char[] output = String.Copy(DataIn).ToCharArray(); int cnt = 0; for (int i=0; i<DataIn.Length; i++) { output = (char)(DataIn ^ CodeKey[cnt]); cnt++; if(cnt >= CodeKey.Length) { cnt = 0; } } return new String(output); }
|
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
|
11-17-2005 13:57
Just a few minor things, SL uses UTF-8 for it's encoding Beyond that i don't see anything wrong with your code, but then as i said before... i've never writen anything in C# or uses the .net libraries so i i don't how the different functions work. You should try to isolate any errors by using try-catch private static string NormalStringToBase64String(string normalString) { Encoding charEncoding = Encoding.UTF8; byte[] byteArray = new byte[asciiEncoding.GetByteCount(normalString)]; byteArray = charEncoding.GetBytes(normalString);
// Perform Base64 transform. return Convert.ToBase64String(byteArray); }
private static string Base64StringToNormalString(string base64String) { Encoding charEncoding = Encoding.UTF8; byte[] byteArray = Convert.FromBase64String(base64String); return charEncoding.GetString(byteArray); }
public static string EncodeXOR(string CodeKey, string text) { string CodeKey64 = NormalStringToBase64String(CodeKey); string text64 = NormalStringToBase64String(text); string output = PerformXOR(CodeKey64, text64); return NormalStringToBase64String(output); }
public static string DecodeXOR(string CodeKey, string XORtext) { string CodeKey64 = NormalStringToBase64String(CodeKey); string text = Base64StringToNormalString(XORtext); string output = PerformXOR(CodeKey64, text); return Base64StringToNormalString(output); }
//each char in the string is xor'd with another char, namely the key. to decrypt, just run the same function again private static string PerformXOR(string CodeKey, string DataIn) { char[] output = String.Copy(DataIn).ToCharArray(); int cnt = 0; for (int i=0; i<DataIn.Length; i++) { output = (char)(DataIn ^ CodeKey[cnt]); cnt++; if(cnt >= CodeKey.Length) { cnt = 0; } } return new String(output); }
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river. - Cyril Connolly
Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence. - James Nachtwey
|
Seer Xingjian
Registered User
Join date: 29 Sep 2005
Posts: 35
|
11-17-2005 14:03
Thanks i will try that tonight. The issues i seem to have when i debug my code seem to be realting to be when to and when no to do the base64 encode and decode. I could be wrong on that but thats when it looks like it all goes wrong.
I will have to wait till tonight when i can login and drop an object in SL that i can connect to to test it. I deleted my test object by mistake before i logged last night.
You could be write with the encodign beinghte problem as that would make sense
|
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
|
11-17-2005 14:15
^^' it's unlikely that the encoding is the issue... but then again...
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river. - Cyril Connolly
Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence. - James Nachtwey
|
Seer Xingjian
Registered User
Join date: 29 Sep 2005
Posts: 35
|
11-17-2005 14:34
well the issue is mainly that the string ends up not being a valid base64 string. If i am encoding the caracters incorrectly it could be part of the problem.
|
Seer Xingjian
Registered User
Join date: 29 Sep 2005
Posts: 35
|
11-20-2005 17:40
Still not working. got all the base64 encode/decode working now so it is jus the XOR that is wrong
|
Seer Xingjian
Registered User
Join date: 29 Sep 2005
Posts: 35
|
11-21-2005 02:35
ok guys here is a progress update. I have it all work except for one small thing.
It seems that when encoding with XOR if the end of the second string that is passed in is reached the whole thing is repeated again. This would not be an issue except for the 5 extra characters they add between the end of the string and the restart of the string. These 5 characters seem to change based of the content of the string but are not effected by the first string passed into the function. So far i have only tested this with 4 letter strings for the second string.
so far i have been unable to work out how these 5 extra letters are generated. There must be a formula to it but i just can't spot it. can anyone help me out with this?
If anyone else is working on the same problem let me know maybe we can work together on this.
any hints, tips or maybe answers would be great!
|
Seer Xingjian
Registered User
Join date: 29 Sep 2005
Posts: 35
|
11-22-2005 17:34
ok guys someone must have worked this out for one of the languages can't anyone help me with a formula for the XOR encryption/decription and the extra characters added to the second string. it does nto need to be in C# i just need the info.
|