Saransh-cpp / Chaotic-Encryption

This repository contains the code for encrypting an image using various techniques and PRNGs.
23 stars 3 forks source link

For floating point data #1

Open nivi1501 opened 2 years ago

nivi1501 commented 2 years ago

Hi, Any idea how to re-design the code, so that it works for floating-point data also? Basically, how to xor between two floating-point values?

Regards, Nivedita

Saransh-cpp commented 2 years ago

Hi @nivi1501! Thanks for reaching out.

As far as I know, one can't perform the XOR operation on floating-point numbers using the inbuilt operations of python (as well as other languages). When converting this code to a Julia package, I struggled a lot with this aspect, as julia represents image pixels from 0 to 1 rather than from 0 to 255.

Below is the code from my julia package (ChaoticEncryption.jl). Notice how I convert the float values to an integer value, XOR it, and then convert it back to float for julia's image style.

"""
    _substitute_pixel(pixel::RGB, key::Int64)

Returns the pixel after XORing the R, G, and B values with the key.
Specifically developed to return an `Array` (or the complete image)
of XORed RGB values in one go.

See [`_substitution`](@ref) for more details.

# Arguments
- `pixel::RGB`: Pixel value with r, g, and b components.
- `key::Int64`: The key.
# Returns
- `pixel::RGB`: Substituted pixel.
"""

_substitute_pixel(pixel::RGB, key::Int64) = RGB(
    (trunc(Int, pixel.r * 255) ⊻ key) / 255,
    (trunc(Int, pixel.g * 255) ⊻ key) / 255,
    (trunc(Int, pixel.b * 255) ⊻ key) / 255
)

https://github.com/Saransh-cpp/ChaoticEncryption.jl/blob/93baccf0d54a8446343f56b0e114bf56ec5d195c/src/substitution.jl#L64-L84

This conversion should be fine if you are working with images, but this would lead to a loss of information if you are working with random floating-point numbers. There are a few answers here that might help you.

Best, Saransh

nivi1501 commented 2 years ago

Thanks for the reply. I am working with random floating-point numbers.