Sanket
Sanket

Reputation: 33

Java code in react native

I am looking for the following code logic in react native code. Tell me if you know the below code convert in react native code. I am new in react native development.

public static String Encrypt_Decrypt(String Str_Message) {
        int Len_Str_Message;
        String Str_Encrypted_Message;
        int Position;
        int Key_To_Use;
        char Byte_To_Be_Encrypted;
        int Ascii_Num_Byte_To_Encrypt;
        int Xored_Byte;
        char Encrypted_Byte;

        Len_Str_Message = Str_Message.length();
        Str_Encrypted_Message = "";

        for (Position = 0; (Position <= (Len_Str_Message - 1)); Position++) {
            Key_To_Use = (Len_Str_Message + (Position + 1));

            Key_To_Use = ((255 + Key_To_Use) % 255);

            Byte_To_Be_Encrypted = (char) Str_Message.charAt(Position);

            Ascii_Num_Byte_To_Encrypt = (char) (Byte_To_Be_Encrypted);

            Xored_Byte = Ascii_Num_Byte_To_Encrypt ^ Key_To_Use;

            Encrypted_Byte = ((char) (Xored_Byte));

            Str_Encrypted_Message = (Str_Encrypted_Message + Encrypted_Byte);
        }

        // System.out.println(Str_Encrypted_Message);
        return Str_Encrypted_Message;
    }

Upvotes: 0

Views: 564

Answers (1)

Prince
Prince

Reputation: 851

the above code is simple "string encryption method", it can easily be written in JS, with same logic,

 Encrypt_Decrypt = (str)=>{
       let length = str.length;
       let encryptyed_str = "";
       for(let i = 0; i< length ; i++){
    let  Key_To_Use = length + i + 1;
         Key_To_Use = ((255 + Key_To_Use) % 255);
    let Byte_To_Be_Encrypted = str[i];
    let Ascii_Num_Byte_To_Encrypt = str[i].charCodeAt(0);
    let Xored_Byte = Ascii_Num_Byte_To_Encrypt ^ Key_To_Use;
    let Encrypted_Byte = String.fromCharCode(Xored_Byte);
    encryptyed_str = encryptyed_str+Encrypted_Byte;
       }
       return encryptyed_str;
    }

for Encrypt_Decrypt('aaa'); it gives "edg"

Upvotes: 1

Related Questions