encrypt and decrypt the image using Hill cipher

Hello, I have tried to use the normal procedures of Hill Cipher techniques in encrypting the Image. Please,assist me
close all; clear;clc; workspace;
text = imread('lena.bmp');
text = double(text);
[m,n]=size(text);
text=reshape(text,1,m*n);
key = [2 3;1 4]; %key
kinverse= [6 15; 5 16];%inverse matrix
[ new_code,original_image ] = encrypt_decrypt( key,keyinverse,text );
figure(1),imshow(new_code);
figure(2),imshow(original_image);
my encryption decryption function is
function [ new_code,original_image ] = encrypt_decrypt( k,kinv, text )
a=reshape(a,2,(length(text))/2); %convert to two rows
code=mod(k*(a),256);
new_code=reshape(code,1,length(text)); %reshape to one row
new_code=uint8(new_code);
new_code=reshape(new_code,m,n);
text2=new_code;
a =double(text2);
text2=reshape(text2,1,m*n);
b=reshape(a,2,length(text2)/2);
msg=mod(k*(b),256);
new_msg=reshape(msg,1,length(text2));
original_image=uint8(new_msg);
original_image=reshape(original_image,m,n);
end

4 comentarios

gayatri Pagadala
gayatri Pagadala el 17 de Mzo. de 2022
Movida: Walter Roberson el 25 de Nov. de 2024
Prepare your message as said below, Encrypt and decrypt the message formed by you using MATLB. 𝐴 = 0, 𝐵 = 1, 𝐶 = 2, ⋯ 𝑋 = 23, 𝑌 = 24, 𝑍 = 25 Key matrix is [ 1 −1 −1 −3 2 6 2 −2 −3 ] Name : SRI RAMA KUMAR Roll no: 21BEC7852 Message is SRIRAMCBBECHIFC (use capital letters only) S R I R A M C B B E C H I F C (Six letters from name) (2 1) (B E C) (7 8 5 2
Sumaya
Sumaya el 24 de Nov. de 2024
Movida: Walter Roberson el 25 de Nov. de 2024
In my programme why shows this in command section when I run "Function definitions are not permitted in this context."?
Perhaps you are using a MATLAB older than R2015b. Older versions of MATLAB do not permit mixing scripts and function definitions, so for older versions of MATLAB, the function would need to be written to its own .m file.
Image Analyst
Image Analyst el 25 de Nov. de 2024
Editada: Image Analyst el 25 de Nov. de 2024
@Sumaya, and to build on Walter's reply, versions from 2015b until around r202x would allow you to have a function in the m-file with a script on top (first in the m-file) and the function(s) on the bottom (only) and the function(s) had to end with an "end" statement. Attach your m-file if you need us to correct it for you.

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 9 de En. de 2021
text is a built-in function so you can't use that as a variable name. Anyway, it's not text - it's an image so call it grayImage or something more descriptive.
You need to define keyinverse. You don't have it. You have something similar kinverse, but that's a different variable.
You need to define m and n. You really need to use descriptive variable names so it doesn't look like an impenetrable alaphbet soup of code. And add LOTS more comments. Here's a little bit further but this code still needs MAJOR improvement:
close all; clear;clc; workspace;
grayImage = imread('cameraman.tif');
grayImage = double(grayImage);
[m,n]=size(grayImage);
grayImage=reshape(grayImage,1,m*n);
key = [2 3;1 4]; %key
keyinverse= [6 15; 5 16];%inverse matrix
[ new_code,original_image ] = encrypt_decrypt( key, keyinverse,grayImage );
figure(1),imshow(new_code);
figure(2),imshow(original_image);
fprintf('Done running %s.m.\n', mfilename);
function [ new_code,original_image ] = encrypt_decrypt( k, kinv, grayImage )
a=reshape(grayImage,2,(length(grayImage))/2); %convert to two rows
code=mod(k*(a),256);
new_code=reshape(code,1,length(grayImage)); %reshape to one row
new_code=uint8(new_code);
new_code=reshape(new_code,m,n);
text2=new_code;
a =double(text2);
text2=reshape(text2,1,m*n);
b=reshape(a,2,length(text2)/2);
msg=mod(k*(b),256);
new_msg=reshape(msg,1,length(text2));
original_image=uint8(new_msg);
original_image=reshape(original_image,m,n);
end

Más respuestas (0)

Categorías

Más información sobre Encryption / Cryptography en Centro de ayuda y File Exchange.

Preguntada:

el 9 de En. de 2021

Editada:

el 25 de Nov. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by