Android Question How to implement a XOR between array of bytes

prbmjr

Active Member
Licensed User
Hi everyone,

I must implement a XOR operation between arrays of bytes, because I need to implement a special data encryption requeted by my client..

I found only the bit.xor function between integers, Is there a way to implement a XOR with arrays?

Thank you in advance!

br,

Paulo Bueno
 

prbmjr

Active Member
Licensed User
B4X:
Sub ArrayXor (b1() As Byte, b2() As Byte) As Byte()
    'assuming that the arrays are with the same length
    Dim b3(b1.Length) As Byte
    For i = 0 To b1.Length - 1
        b3(i) = Bit.Xor(b1(i), b2(i))
    Next
    Return b3
End Sub

Hi Erel,

Thank you very much for your answer. I had something like this already implemented, but my concern is about the performance, if I have to use this type of code instead a real function/library using big arrays, like 500k, 50mb, etc... These arrays are entires files that must be encrypted... I have a similar code in python for reference and this runs really fast using the function numpy.bitwise_xor . I will try and I let you know with the result.

br,

Paulo Bueno
 
Upvote 0

prbmjr

Active Member
Licensed User
Test it.
B4X:
Dim b1(500000), b2(500000) As Byte
For i = 1 To 100
    Dim n As Long = DateTime.Now
    ArrayXor(b1, b2)
    Log(DateTime.Now - n)
Next
On my device it takes between 0 to 1 millisecond.

Hi Erel,

I tested with this code:

B4X:
Sub ArrayXor_teste As Byte()
    'assuming that the arrays are with the same length
    Dim b1(552643), b2(552643) As Byte
    Dim b3(b1.Length) As Byte

    For i = 0 To b1.Length - 1
        
        b3(i) = Bit.Xor(b1(i), b2(i))
    Next
    Return b3
End Sub

In my test I used a Sansung S8 device and takes 32 seg, why??
 
Upvote 0

DonManfred

Expert
Licensed User
In my test I used a Sansung S8 device and takes 32 seg, why??
Did you tested it in RELEASE mode?? NEVER do performance measures in DEBUG mode.
 
Last edited:
Upvote 0
Top