How can i encrypt image by xoring?

3 views (last 30 days)
Md.Simul Hasan Talukder
Md.Simul Hasan Talukder on 4 Sep 2018
Commented: DGM on 22 Aug 2024
How can i encrypt image by xoring?

Answers (1)

Hornett
Hornett on 21 Aug 2024
Encrypting an image using the XOR operation in MATLAB is also a straightforward process. Below is a step-by-step guide on how to achieve this:Step-by-Step Guide
  1. Read the Image: Load the image into MATLAB.
  2. Generate a Key: Create a key with the same dimensions as the image.
  3. Apply XOR: Use the XOR operation between the image data and the key.
  4. Save the Encrypted Image: Save the resulting image.
Example in MATLAB
Here's how you can implement this in MATLAB:
function xor_encrypt_image(input_image_path, output_image_path, key)
% Read the image
image = imread(input_image_path);
% Convert key to uint8 and resize to match image dimensions
key = uint8(key);
key_length = numel(key);
[rows, cols, channels] = size(image);
% Create a key matrix
key_matrix = repmat(key, ceil(rows * cols * channels / key_length), 1);
key_matrix = key_matrix(1:rows * cols * channels);
key_matrix = reshape(key_matrix, [rows, cols, channels]);
% Perform XOR operation
encrypted_image = bitxor(image, key_matrix);
% Save the encrypted image
imwrite(encrypted_image, output_image_path);
end
% Example usage
input_image_path = 'temp.png';
output_image_path = 'encrypted_image.png';
key = '858629701497d3ae2b60abde8d3b2179c51e35318997260ec07e6ca2aef05f9f72e618fd4f0c4157669ffe89c2e625e3'; % The key should be kept secret
xor_encrypt_image(input_image_path, output_image_path, key);
Important Considerations
  • Key Management: The security of XOR encryption is dependent on the secrecy of the key. Ensure the key is kept confidential and is not easily guessable.
  • Key Length: The key is repeated to match the size of the image data. Make sure the key is long enough to provide sufficient randomness.
  • Reversible Process: To decrypt the image, apply the same XOR operation using the same key on the encrypted image.
  • Security: XOR encryption is not very secure for serious applications. It's vulnerable to various attacks, so for stronger encryption, consider using more robust algorithms.
This MATLAB script assumes you have the image processing toolbox available. By following these steps, you can encrypt and decrypt images using XOR in MATLAB.
  4 Comments
Walter Roberson
Walter Roberson on 22 Aug 2024
You can use typecast() to uint8 to deal with the possibility of different datatypes. typecast() needs a scalar or vector as input, so you would already be using (:) at that level.
DGM
DGM on 22 Aug 2024
You could, but along with geometry, you'd also need to retain knowledge of the original class in order to fully recover the data. I think it would make more sense to just generate the key array to match the class of the input instead of forcing the input into a presumed class.
Of course, the problem with that is that we've designed our function around letting it read and write files from/to disk unattended. Even if we did what I suggested, we're limited by the output format we've presumed. Similarly, we're limited by the ability of imwrite() and imread() to be able to correctly handle any given image without needing adjustments to their usage. That means no alpha content, no indexed-color, no multiframe images. Things like JPGs with orientation metadata are going to wind up sideways as their metadata is lost. Images in non-native integer scale may or may not lose their intended scale. For such a simple obfuscation utility, making it handle all the possible inputs would complicate it greatly.
I would avoid all that by just writing the function to accept/return arrays from the workspace. I guess that's not really what the demo is about, but I think it demonstrates how it makes it hard to be flexible when the file handling is all internal to such a simple function.
FWIW, as much as I poke at things that aren't flexible, it's no sin for a simple code to only support a narrow set of inputs. For a demo, it's no harm, but in practice, I'd at least hope the limitation is acknowledged and documented.

Sign in to comment.

Categories

Find more on Encryption / Cryptography in Help Center and File Exchange

Tags

Products


Release

R2015a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!