Like Christian said, your main problem is how Javascript and how Go iterate.
In your Javascript example, you use:
for(var i=0;i<input.length;i++){
so you iterate in a +1 increment regardless of any type of characters. When
you range over a Go string, you will see the iterator jumping 1,2 or 3
positions depending on the type of rune.
Your modulo calculation index % len(key) will match for ascii (e.g. English
characters), but will different after the first non-ascii characters
So you should explore something like this:
https://play.golang.org/p/23nMX2dFhJF
On Sunday, 20 January 2019 10:14:05 UTC-5, Soorya Prakash wrote:
>
>
> I am using the below code to encrypt and decrypt the data. Now I want to
> encrypt the data from Node JS and want to decrypt the data from Go lang.
> But I am not able to achieve it using GO lang.
>
> var B64XorCipher = {
> encode: function(key, data) {
> return new Buffer(xorStrings(key, data),'utf8').toString('base64');
> },
> decode: function(key, data) {
> data = new Buffer(data,'base64').toString('utf8');
> return xorStrings(key, data);
> }};
> function xorStrings(key,input){
> var output='';
> for(var i=0;i<input.length;i++){
> var c = input.charCodeAt(i);
> var k = key.charCodeAt(i%key.length);
> output += String.fromCharCode(c ^ k);
> }
> return output;}
>
> From go I am trying to decode like below I am not able to achieve it.
>
> bytes, err := base64.StdEncoding.DecodeString(actualInput)
> encryptedText := string(bytes)
> fmt.Println(EncryptDecrypt(encryptedText, "XXXXXX"))
>
> func EncryptDecrypt(input, key string) (output string) {
> for i := range input {
> output += string(input[i] ^ key[i%len(key)])
> }
>
> return output}
>
> Can someone help me to resolve it.
>
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.