String.fromCharCode and charCodeAt
-
hi all does anyone know how to convert the following js into c# ?
my_str += String.fromCharCode((src.charCodeAt(c) ^ keys[cc]));
which is contained in the following loop (for clarity):
for(var c:Number = 0; c < src.length; c ++){
if(cc>=i) {
cc = 0;
}
my_str += String.fromCharCode((src.charCodeAt(c) ^ keys[cc]));
cc++;
}I've searched and found the equivalent for both but dont seem to be able to put it together, it should be something like this in c#:
my_str += new String(new char[]{(Int32)Data[c] ^ keys[cc]});
but that obviously fails and im just confusing myself now. any pointer very very much appreciated. keys is already converted into an array of ASCII codes and Data is a string at present, i was thinking I should convert it into an array of ASCII codes as well..... thank you tim
-
hi all does anyone know how to convert the following js into c# ?
my_str += String.fromCharCode((src.charCodeAt(c) ^ keys[cc]));
which is contained in the following loop (for clarity):
for(var c:Number = 0; c < src.length; c ++){
if(cc>=i) {
cc = 0;
}
my_str += String.fromCharCode((src.charCodeAt(c) ^ keys[cc]));
cc++;
}I've searched and found the equivalent for both but dont seem to be able to put it together, it should be something like this in c#:
my_str += new String(new char[]{(Int32)Data[c] ^ keys[cc]});
but that obviously fails and im just confusing myself now. any pointer very very much appreciated. keys is already converted into an array of ASCII codes and Data is a string at present, i was thinking I should convert it into an array of ASCII codes as well..... thank you tim
ignore ;p was having one of those moments when you cant see the woods for the trees (or whatever!)
my_str += (char)(letters[c] ^ keys[cc]);
does the job after creating the letters array using (Int32)input[i] in a for loop thanks anyway. t
-
hi all does anyone know how to convert the following js into c# ?
my_str += String.fromCharCode((src.charCodeAt(c) ^ keys[cc]));
which is contained in the following loop (for clarity):
for(var c:Number = 0; c < src.length; c ++){
if(cc>=i) {
cc = 0;
}
my_str += String.fromCharCode((src.charCodeAt(c) ^ keys[cc]));
cc++;
}I've searched and found the equivalent for both but dont seem to be able to put it together, it should be something like this in c#:
my_str += new String(new char[]{(Int32)Data[c] ^ keys[cc]});
but that obviously fails and im just confusing myself now. any pointer very very much appreciated. keys is already converted into an array of ASCII codes and Data is a string at present, i was thinking I should convert it into an array of ASCII codes as well..... thank you tim
You are on the right track, you just need a few more parentheses so that you are casting just the character instead the entire result. Don't use += to build the string, put all the characters in an array and create a string from that.
char[] chars = new char[Data.Length];
for (int c = 0; c < Data.Length; c++) {
chars[c] = (char)(((int)Data[c]) ^ keys[cc++ % i]);
}
string my_str = new string(chars);Despite everything, the person most likely to be fooling you next is yourself.
-
You are on the right track, you just need a few more parentheses so that you are casting just the character instead the entire result. Don't use += to build the string, put all the characters in an array and create a string from that.
char[] chars = new char[Data.Length];
for (int c = 0; c < Data.Length; c++) {
chars[c] = (char)(((int)Data[c]) ^ keys[cc++ % i]);
}
string my_str = new string(chars);Despite everything, the person most likely to be fooling you next is yourself.
hi guffa whats the difference between creating an array(then string) or building the string as we go. I dont mean that in a rude way ;) I'm intrigued as they produce two different results from what *should* be the same string or is it the char creation that differs the result ? tim
-
hi guffa whats the difference between creating an array(then string) or building the string as we go. I dont mean that in a rude way ;) I'm intrigued as they produce two different results from what *should* be the same string or is it the char creation that differs the result ? tim
The difference is that if you use += to create the string, you are creating every intermediate string as a separate object. If your code creates the string "This is a test", it will also have created the strings "T", "Th", "Thi", "This", "This ", "This i", "This is", "This is ", "This is a", "This is a ", "This is a t", "This is a te" and "This is a tes". Strings in .NET are immutable. When you use the += operator, you are actually creating a new string object every time, copying the data from the original string. For very short strings, this is not a big problem, but it quickly gets bad. To create a 100 character string, you will have created 10 kB of strings. To create a 1000 character string, you will have created 1 MB of strings.
Despite everything, the person most likely to be fooling you next is yourself.
-
The difference is that if you use += to create the string, you are creating every intermediate string as a separate object. If your code creates the string "This is a test", it will also have created the strings "T", "Th", "Thi", "This", "This ", "This i", "This is", "This is ", "This is a", "This is a ", "This is a t", "This is a te" and "This is a tes". Strings in .NET are immutable. When you use the += operator, you are actually creating a new string object every time, copying the data from the original string. For very short strings, this is not a big problem, but it quickly gets bad. To create a 100 character string, you will have created 10 kB of strings. To create a 1000 character string, you will have created 1 MB of strings.
Despite everything, the person most likely to be fooling you next is yourself.
hi thanks for that, thats very interesting and something I didn't realise. but... strangely enough that must be the way that javascript handles it (i think *gulp*) as the original way I was doing it : c#
my_str += (char)(letters[c] ^ keys[cc]);
produces exactly the same result as: js
my_str += String.fromCharCode((src.charCodeAt(c) ^ keys[cc]));
which was(after to base64 encoding for transport(the unencoded isn't really printable)):
OhFBBxQWEBFLGgsEHgoVRBwSEAYCCw==
and yours produced:
OhhBBh4eCRNPEQkMBQQVVRgaHggBBQ==
I was doing this as an little encryption thing between flash and .net as i couldn't find anything that was compatible, I tried many different ones (rijndael, TEA etc) but all the flash conversions of them produced completely different results from the c# end and I was tearing my hair out trying to do my own conversions of them so I thought i would write a simple (obviously not that strong - but it doesnt need to be super difficult) bit of encryption that i could rewrite in javascript(for the .as files in flash and also for php etc) that would suffice. but... like you say the larger the string then the larger the problem(or at least load) so now i'm wondering if theres a way to reproduce what you've said in js - this isn't a problem for what I'm doing now but it would be nice to have a better model in place for future. something for me over the weekend ;) thanks for your replies and if you have any thoughts I'd be really interested to hear them. tim i suppose in js i would just create an array and then use push to add them in and then create the string...but i still don't get why it produces different results from what *should* be the same string...hmmmm time for some script outputting...
modified on Friday, July 4, 2008 8:33 PM
-
hi thanks for that, thats very interesting and something I didn't realise. but... strangely enough that must be the way that javascript handles it (i think *gulp*) as the original way I was doing it : c#
my_str += (char)(letters[c] ^ keys[cc]);
produces exactly the same result as: js
my_str += String.fromCharCode((src.charCodeAt(c) ^ keys[cc]));
which was(after to base64 encoding for transport(the unencoded isn't really printable)):
OhFBBxQWEBFLGgsEHgoVRBwSEAYCCw==
and yours produced:
OhhBBh4eCRNPEQkMBQQVVRgaHggBBQ==
I was doing this as an little encryption thing between flash and .net as i couldn't find anything that was compatible, I tried many different ones (rijndael, TEA etc) but all the flash conversions of them produced completely different results from the c# end and I was tearing my hair out trying to do my own conversions of them so I thought i would write a simple (obviously not that strong - but it doesnt need to be super difficult) bit of encryption that i could rewrite in javascript(for the .as files in flash and also for php etc) that would suffice. but... like you say the larger the string then the larger the problem(or at least load) so now i'm wondering if theres a way to reproduce what you've said in js - this isn't a problem for what I'm doing now but it would be nice to have a better model in place for future. something for me over the weekend ;) thanks for your replies and if you have any thoughts I'd be really interested to hear them. tim i suppose in js i would just create an array and then use push to add them in and then create the string...but i still don't get why it produces different results from what *should* be the same string...hmmmm time for some script outputting...
modified on Friday, July 4, 2008 8:33 PM
How did you turn the string into a byte array, so that it could be base64 encoded? I think that the problem is that the codes are using different character sets. The characters outside the ASCII set uses different character codes in different character sets.
Despite everything, the person most likely to be fooling you next is yourself.
-
How did you turn the string into a byte array, so that it could be base64 encoded? I think that the problem is that the codes are using different character sets. The characters outside the ASCII set uses different character codes in different character sets.
Despite everything, the person most likely to be fooling you next is yourself.
hi in c# i used
Convert.ToBase64String(Encoding.ASCII.GetBytes(my_str))
and in js i used the base64 encoding i found in amongst the encryption files from http://labs.boulevart.be/index.php/2007/05/23/encryption-in-as2-and-as3/[^] which is where my problems began.... not that i'm putting down what they have there, I just couldn't get it to produce the same results from my end with the rijndael/TEa encryption, the base64 seems to be fine tim edit: this is the function that encodes to base64 in js:
\*/ private static var base64chars:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; /\*\* \* Encodes a base64 string. \*/ public static function encode(src:String):String { var i:Number = 0; var output:String = new String(""); var chr1:Number, chr2:Number, chr3:Number; var enc1:Number, enc2:Number, enc3:Number, enc4:Number; while (i < src.length) { chr1 = src.charCodeAt(i++); chr2 = src.charCodeAt(i++); chr3 = src.charCodeAt(i++); enc1 = chr1 >> 2; enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); enc4 = chr3 & 63; if(isNaN(chr2)) enc3 = enc4 = 64; else if(isNaN(chr3)) enc4 = 64; output += base64chars.charAt(enc1)+base64chars.charAt(enc2); output += base64chars.charAt(enc3)+base64chars.charAt(enc4) } return output; }
-
The difference is that if you use += to create the string, you are creating every intermediate string as a separate object. If your code creates the string "This is a test", it will also have created the strings "T", "Th", "Thi", "This", "This ", "This i", "This is", "This is ", "This is a", "This is a ", "This is a t", "This is a te" and "This is a tes". Strings in .NET are immutable. When you use the += operator, you are actually creating a new string object every time, copying the data from the original string. For very short strings, this is not a big problem, but it quickly gets bad. To create a 100 character string, you will have created 10 kB of strings. To create a 1000 character string, you will have created 1 MB of strings.
Despite everything, the person most likely to be fooling you next is yourself.
Guffa wrote:
To create a 100 character string, you will have created 10 kB of strings. To create a 1000 character string, you will have created 1 MB of strings.
Great ! I am curious to know how you calculated this ?
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
Guffa wrote:
To create a 100 character string, you will have created 10 kB of strings. To create a 1000 character string, you will have created 1 MB of strings.
Great ! I am curious to know how you calculated this ?
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
You just add the length of the strings, and multiply by two (as each character is two bytes).
(1+2+3+4+...+98+98+99+100) * 2 = (101 * 50) * 2 = 10100
(1+2+3+4+...+998+999+1000) * 2 = (1001 * 500) * 2 = 1001000
The actual amount of memory used is slightly more, as there is some overhead in each object.Despite everything, the person most likely to be fooling you next is yourself.
modified on Sunday, July 6, 2008 9:25 AM
-
hi in c# i used
Convert.ToBase64String(Encoding.ASCII.GetBytes(my_str))
and in js i used the base64 encoding i found in amongst the encryption files from http://labs.boulevart.be/index.php/2007/05/23/encryption-in-as2-and-as3/[^] which is where my problems began.... not that i'm putting down what they have there, I just couldn't get it to produce the same results from my end with the rijndael/TEa encryption, the base64 seems to be fine tim edit: this is the function that encodes to base64 in js:
\*/ private static var base64chars:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; /\*\* \* Encodes a base64 string. \*/ public static function encode(src:String):String { var i:Number = 0; var output:String = new String(""); var chr1:Number, chr2:Number, chr3:Number; var enc1:Number, enc2:Number, enc3:Number, enc4:Number; while (i < src.length) { chr1 = src.charCodeAt(i++); chr2 = src.charCodeAt(i++); chr3 = src.charCodeAt(i++); enc1 = chr1 >> 2; enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); enc4 = chr3 & 63; if(isNaN(chr2)) enc3 = enc4 = 64; else if(isNaN(chr3)) enc4 = 64; output += base64chars.charAt(enc1)+base64chars.charAt(enc2); output += base64chars.charAt(enc3)+base64chars.charAt(enc4) } return output; }
tim_gunning wrote:
in c# i used Convert.ToBase64String(Encoding.ASCII.GetBytes(my_str))
The ASCII encoding only handles characters with character code 0 to 127. To get the equivalent of the JScript code you would have to loop through the string and get the character codes into a byte array, so that you get the characters with character codes from 0 to 255. The JScript code claims to encode a string to base64, but that it not really possible. It assumes that there are never any character codes above 255. If there is, the result will be incorrect. To correctly turn a string into base64, you first have to encode it using an encoding that can handle the full unicode character set, like UTF-8: Convert.ToBase64String(Encoding.UTF8.GetBytes(my_str))
Despite everything, the person most likely to be fooling you next is yourself.
-
You just add the length of the strings, and multiply by two (as each character is two bytes).
(1+2+3+4+...+98+98+99+100) * 2 = (101 * 50) * 2 = 10100
(1+2+3+4+...+998+999+1000) * 2 = (1001 * 500) * 2 = 1001000
The actual amount of memory used is slightly more, as there is some overhead in each object.Despite everything, the person most likely to be fooling you next is yourself.
modified on Sunday, July 6, 2008 9:25 AM
Thanks. :)
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
tim_gunning wrote:
in c# i used Convert.ToBase64String(Encoding.ASCII.GetBytes(my_str))
The ASCII encoding only handles characters with character code 0 to 127. To get the equivalent of the JScript code you would have to loop through the string and get the character codes into a byte array, so that you get the characters with character codes from 0 to 255. The JScript code claims to encode a string to base64, but that it not really possible. It assumes that there are never any character codes above 255. If there is, the result will be incorrect. To correctly turn a string into base64, you first have to encode it using an encoding that can handle the full unicode character set, like UTF-8: Convert.ToBase64String(Encoding.UTF8.GetBytes(my_str))
Despite everything, the person most likely to be fooling you next is yourself.
hi originally I wss using UTF8 but the js wasnt coping and then I realised chars were missing from the c# end (anything over 127 or so) which I needed (long story....;p) anyway to cut it short I've encoded the js to UTF8 and the c# and then both to base64, used StringBuilder to create my return string and both the js in the flash and c# are producing identical results which is what I needed, I'll post it up here at some point as it may be of interest to someone. thanks for all your help/pointers guffa, very much appreciated. tim