├── .gitattributes ├── 9781484273333.jpg ├── Ch14 - Steganography ├── swan_input.jpg ├── swan_output.png ├── Listing14_2.m └── Listing14_1.m ├── Ch7 - Pseudo-Random Numbers Generators ├── Listing7_3.m ├── Listing7_2.m ├── Listing7_4.m └── Listing7_1_DirectMethod_BinomialRandomNumberGenerator.m ├── Ch13 - Chaos-based Cryptography ├── enc_image1.jpg ├── enc_image2.jpg ├── Listing13_1.m ├── Listing13_4.m ├── Listing13_3.m ├── Listing13_6.m ├── Listing13_2.m └── Listing13_5.m ├── Ch3 - Conversions used in MATLAB ├── Listing3_6.m ├── Listing3_5.m ├── Listing3_4.m ├── Listing3_3.m ├── Listing3_1.m └── Listing3_2.m ├── Ch4 - Basic Arithmetic Foundations ├── Listing4_3.m ├── Listing4_2.m └── Listing4_1.m ├── Ch11 - Formal Techniques ├── GenerateNumbers.m └── GenerateNumbersInterval.m ├── Ch6 - Classic Cryptography ├── VigenereAlgorithm │ ├── Shift_Encryption.m │ ├── Vigenere_Encryption.m │ └── Vigenere_Decryption.m ├── HillAlgorithm │ └── Hill_Encryption.m └── CaesarAlgorithm │ └── caesaralgorithm.m ├── errata.md ├── Ch8 - Hash Functions ├── Listing81.m ├── Listing84.m ├── Listing82.m └── Listing83.m ├── README.md ├── Ch5 - Number Theory ├── Wilson.m ├── NaivePrimality.m ├── Fermat.m └── Fermat2.m ├── Contributing.md ├── Ch2 - MATLAB Cryptography Functions ├── Listing2-2.m └── Listing2-1.m ├── Ch9 - Block Ciphers_ DES and AES └── DES │ └── Listing91.m ├── Ch12 - Visual Cryptography └── Shamir.m ├── LICENSE.txt └── Ch10 - Asymmetric Encryption ├── RSA.m └── ElGamal.m /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /9781484273333.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/cryptography-cryptanalysis-matlab/HEAD/9781484273333.jpg -------------------------------------------------------------------------------- /Ch14 - Steganography/swan_input.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/cryptography-cryptanalysis-matlab/HEAD/Ch14 - Steganography/swan_input.jpg -------------------------------------------------------------------------------- /Ch14 - Steganography/swan_output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/cryptography-cryptanalysis-matlab/HEAD/Ch14 - Steganography/swan_output.png -------------------------------------------------------------------------------- /Ch7 - Pseudo-Random Numbers Generators/Listing7_3.m: -------------------------------------------------------------------------------- 1 | %see [21] for a detailed version 2 | rng('default') 3 | unit = 1; 4 | output = expinv(rand(1e4,1),unit); -------------------------------------------------------------------------------- /Ch13 - Chaos-based Cryptography/enc_image1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/cryptography-cryptanalysis-matlab/HEAD/Ch13 - Chaos-based Cryptography/enc_image1.jpg -------------------------------------------------------------------------------- /Ch13 - Chaos-based Cryptography/enc_image2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/cryptography-cryptanalysis-matlab/HEAD/Ch13 - Chaos-based Cryptography/enc_image2.jpg -------------------------------------------------------------------------------- /Ch3 - Conversions used in MATLAB/Listing3_6.m: -------------------------------------------------------------------------------- 1 | function array_output = Listing3_6(min, max, length) 2 | f = floor(max+1-min); 3 | r = rand(1,length); 4 | array_output = min + (f * r); -------------------------------------------------------------------------------- /Ch4 - Basic Arithmetic Foundations/Listing4_3.m: -------------------------------------------------------------------------------- 1 | function computingInverse = Listing4_3(w,moduloN) 2 | [d, x, y] = gcd(w,moduloN); 3 | computingInverse = mod(x,moduloN); 4 | end -------------------------------------------------------------------------------- /Ch11 - Formal Techniques/GenerateNumbers.m: -------------------------------------------------------------------------------- 1 | clc; 2 | disp('Generating numbers...'); 3 | clear all; close all; 4 | 5 | rng(25); 6 | times = 8; 7 | noOfExp = 10; 8 | randNums = rand(times,noOfExp); 9 | disp(randNums); -------------------------------------------------------------------------------- /Ch13 - Chaos-based Cryptography/Listing13_1.m: -------------------------------------------------------------------------------- 1 | load('lorenzAttractorExampleData.mat','data','fs'); 2 | plot3(data(:,1),data(:,2),data(:,3)); 3 | title('Computing Lyapunov Exponent for Lorenz Attractor Data Set'); 4 | 5 | -------------------------------------------------------------------------------- /Ch7 - Pseudo-Random Numbers Generators/Listing7_2.m: -------------------------------------------------------------------------------- 1 | % see [21] for this sample 2 | rng('default') 3 | binomial_random_value = Listing7_1_DirectMethod_BinomialRandomNumberGenerator(325,0.2,1e3,1); 4 | histogram(binomial_random_value,101) -------------------------------------------------------------------------------- /Ch6 - Classic Cryptography/VigenereAlgorithm/Shift_Encryption.m: -------------------------------------------------------------------------------- 1 | function OutputString = Shift_Encryption(plaintext,moduloInteger) 2 | Array = plaintext - 'a'; 3 | OutputArray = mod( Array + moduloInteger, 26); 4 | OutputString = char(OutputArray + 'A'); -------------------------------------------------------------------------------- /Ch3 - Conversions used in MATLAB/Listing3_5.m: -------------------------------------------------------------------------------- 1 | function output_array = Listing3_5(string_array) 2 | 3 | while (max((string_array < 'A') + (string_array > 'Z'))) > 0 4 | error('The text provided must be only capital letters'); 5 | end 6 | 7 | output_array = string_array - 'A'; 8 | -------------------------------------------------------------------------------- /Ch3 - Conversions used in MATLAB/Listing3_4.m: -------------------------------------------------------------------------------- 1 | function output_array = Listing3_4(string_array) 2 | 3 | for i=1:length(string_array) 4 | if double(string_array(i)) > 32 5 | output_array(i) = double(string_array(i))-97; 6 | else 7 | output_array(i) = 26; 8 | end 9 | end -------------------------------------------------------------------------------- /Ch3 - Conversions used in MATLAB/Listing3_3.m: -------------------------------------------------------------------------------- 1 | function output_array = Listing3_3(string_array) 2 | 3 | while (max((string_array < 'a') + (string_array > 'z'))) > 0 4 | error('The integer value provided has to contain lower-case letter.'); 5 | end 6 | 7 | output_array = string_array - 'a'; 8 | -------------------------------------------------------------------------------- /Ch11 - Formal Techniques/GenerateNumbersInterval.m: -------------------------------------------------------------------------------- 1 | clc; 2 | disp('Generating numbers interval...'); 3 | clear all; close all; 4 | 5 | rng(25); 6 | times = 8; 7 | noOfExp = 10; 8 | minVal = 100; 9 | maxVal = 1000; 10 | randNums = round(minVal+(maxVal-minVal)*rand(times,noOfExp)); 11 | disp(randNums); -------------------------------------------------------------------------------- /errata.md: -------------------------------------------------------------------------------- 1 | # Errata for *Cryptography and Cryptanalysis in MATLAB* 2 | 3 | On **page xx** [Summary of error]: 4 | 5 | Details of error here. Highlight key pieces in **bold**. 6 | 7 | *** 8 | 9 | On **page xx** [Summary of error]: 10 | 11 | Details of error here. Highlight key pieces in **bold**. 12 | 13 | *** -------------------------------------------------------------------------------- /Ch13 - Chaos-based Cryptography/Listing13_4.m: -------------------------------------------------------------------------------- 1 | %load images 2 | encrypted_image_1 = imread('enc_image1.jpg'); 3 | encrypted_image_2 = imread('enc_image2.jpg'); 4 | 5 | % encrypted image 1 6 | img1 = entropy(encrypted_image_1); 7 | 8 | % encrypted image 2 9 | img2 = entropy(encrypted_image_2); 10 | 11 | disp(img1); 12 | disp(img2); 13 | -------------------------------------------------------------------------------- /Ch3 - Conversions used in MATLAB/Listing3_1.m: -------------------------------------------------------------------------------- 1 | function converted_output_string = Listing3_1(integer_array) 2 | 3 | while (max((integer_array < 0) | (integer_array > 25))) > 0 4 | error('The integer value provided has to be situated between 0 and 25. Please, try again!'); 5 | end 6 | 7 | converted_output_string = char(integer_array + 'a'); -------------------------------------------------------------------------------- /Ch3 - Conversions used in MATLAB/Listing3_2.m: -------------------------------------------------------------------------------- 1 | function converted_output_string = Listing3_2(integer_array) 2 | 3 | while (max((integer_array < 0) | (integer_array > 25))) > 0 4 | error('The integer value provided has to be situated between 0 and 25. Please, try again!'); 5 | end 6 | 7 | converted_output_string = char(integer_array + 'A'); -------------------------------------------------------------------------------- /Ch4 - Basic Arithmetic Foundations/Listing4_2.m: -------------------------------------------------------------------------------- 1 | xbest = [0,0,0,0,0,0,0] 2 | fmin = fun(xbest); 3 | for i1 = 0:20 4 | for i2 = 0:20 5 | for i7 = 1:20 6 | x = [i1,i2,i3,i4,i5,i6,i7] 7 | f = fun(x) 8 | if f < fmin 9 | xbest = x 10 | fmin = f 11 | end 12 | end 13 | end 14 | end -------------------------------------------------------------------------------- /Ch8 - Hash Functions/Listing81.m: -------------------------------------------------------------------------------- 1 | function Listing81 2 | n=5; 3 | x=input('Give a value X = ') 4 | e_s=(0.3*13^(4-n)) 5 | e=100 6 | ml(1)=1 7 | it=1 8 | while e>e_s 9 | it=it+1 10 | ml(it)=ml(it-1)+(x)^(it-1)+x^(2+(it-1))/factorial(2+(it-1)) 11 | e=abs((ml(it)-ml(it-1))/ml(it)) 12 | end 13 | 14 | disp(['number of iterations= ',num2str(it)]) 15 | disp(['epsilon= ',num2str(e)]) -------------------------------------------------------------------------------- /Ch6 - Classic Cryptography/VigenereAlgorithm/Vigenere_Encryption.m: -------------------------------------------------------------------------------- 1 | function output_string = Vigenere_Encryption(plaintext,encryption_key) 2 | Array = encryption_key - 'a'; 3 | keylength = length(encryption_key); 4 | for i=1:length(plaintext) 5 | ishift = mod(i,keylength); 6 | if ishift == 0, ishift = keylength; end 7 | 8 | output_string(i) = Shift_Encryption(plaintext(i),Array(ishift)); 9 | end -------------------------------------------------------------------------------- /Ch13 - Chaos-based Cryptography/Listing13_3.m: -------------------------------------------------------------------------------- 1 | %load images 2 | encrypted_image_1 = imread('enc_image1.jpg'); 3 | encrypted_image_2 = imread('enc_image2.jpg'); 4 | 5 | % encrypted image 1 6 | subplot(1,2,1); 7 | imhist(encrypted_image_1); 8 | title('Histogram for encrypted image 1'); 9 | 10 | % encrypted image 2 11 | subplot(1,2,2); 12 | imhist(encrypted_image_2); 13 | title('Histogram for encrypted image 2'); -------------------------------------------------------------------------------- /Ch7 - Pseudo-Random Numbers Generators/Listing7_4.m: -------------------------------------------------------------------------------- 1 | %see [21] for a detailed version 2 | 3 | rng('default') 4 | unit = 1; 5 | output = expinv(rand(1e4,1),unit); 6 | Y = output; 7 | 8 | numbins = 150; 9 | theHistogram = histogram(Y,numbins,'Normalization','pdf'); 10 | hold on 11 | a = linspace(theHistogram.BinEdges(1),theHistogram.BinEdges(end)); 12 | b = exppdf(a,unit); 13 | plot(a,b,'LineWidth',1) 14 | hold off -------------------------------------------------------------------------------- /Ch8 - Hash Functions/Listing84.m: -------------------------------------------------------------------------------- 1 | hash_algorithms={'MD2','MD5','SHA-1','SHA-256','SHA-384','SHA-512'}; 2 | 3 | disp('Welcome to Apress!'); 4 | disp('------------------'); 5 | disp('The following text will be hashed using the hash algorithms from below'); 6 | disp('The text is: Good luck with Hash Functions!'); 7 | disp(' '); 8 | 9 | for n=1:6 10 | my_hash_result = Listing83('Good luck with Hash Functions!',hash_algorithms{n}); 11 | disp([hash_algorithms{n} ' (' num2str(length(my_hash_result)*4) ' bits):']) 12 | disp(my_hash_result) 13 | end -------------------------------------------------------------------------------- /Ch6 - Classic Cryptography/VigenereAlgorithm/Vigenere_Decryption.m: -------------------------------------------------------------------------------- 1 | function output_vigenere_decryption = Vigenere_Decryption(plaintext,decryptionKey) 2 | 3 | Array = decryptionKey - 'a'; 4 | decryption_key = length(decryptionKey); 5 | lowercase_string = char((plaintext - 'A') + 'a'); 6 | for i=1:length(plaintext) 7 | ishift = mod(i,decryption_key); 8 | if ishift == 0, ishift = decryption_key; end 9 | output_vigenere_decryption(i) = Shift_Encryption(lowercase_string(i),-Array(ishift)); 10 | end 11 | output_vigenere_decryption = char((output_vigenere_decryption - 'A') + 'a'); 12 | 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Apress Source Code 2 | 3 | This repository accompanies [*Cryptography and Cryptanalysis in MATLAB*](https://www.apress.com/9781484273333) by Marius Iulian Mihailescu and Stefania Loredana Nita (Apress, 2021). 4 | 5 | [comment]: #cover 6 | ![Cover image](9781484273333.jpg) 7 | 8 | Download the files as a zip using the green button, or clone the repository to your machine using Git. 9 | 10 | ## Releases 11 | 12 | Release v1.0 corresponds to the code in the published book, without corrections or updates. 13 | 14 | ## Contributions 15 | 16 | See the file Contributing.md for more information on how you can contribute to this repository. -------------------------------------------------------------------------------- /Ch7 - Pseudo-Random Numbers Generators/Listing7_1_DirectMethod_BinomialRandomNumberGenerator.m: -------------------------------------------------------------------------------- 1 | % see [21] for this sample 2 | function BinomialRandomValue = Listing7_1_DirectMethod_BinomialRandomNumberGenerator(throws,probability,a,b) 3 | % throws - represents the number of throws or tosses 4 | % probability - represents the probability value on a single throw 5 | % a and b - values entered by user, check Listing 7-2 for the example 6 | BinomialRandomValue = zeros(a,b); 7 | for k = 1:a*b 8 | random_value = rand(throws,1); 9 | BinomialRandomValue(k) = sum(random_value < probability); 10 | end 11 | BinomialRandomValue 12 | end -------------------------------------------------------------------------------- /Ch14 - Steganography/Listing14_2.m: -------------------------------------------------------------------------------- 1 | % identifying physical on the drive the images (original and modified) 2 | original_image = 'swan_input.jpg'; 3 | modified_image = 'swan_output.png'; 4 | 5 | % read the images and save them in o_i (original image variable) and m_i 6 | % (modified image variable) 7 | o_i = imread(original_image); 8 | m_i = imread(modified_image); 9 | 10 | % original image 11 | subplot(1,2,1); 12 | imhist(o_i); 13 | title('Histogram for original image'); 14 | 15 | % new image with the message hidden 16 | subplot(1,2,2); 17 | imhist(m_i); 18 | title('Histogram of the modified image'); 19 | 20 | sgt = sgtitle('Histogram Analysis','Color','blue'); 21 | sgt.FontSize = 20; -------------------------------------------------------------------------------- /Ch5 - Number Theory/Wilson.m: -------------------------------------------------------------------------------- 1 | fprintf('\nTesting the primality of a positive integer...'); 2 | clear all; close all; 3 | 4 | number = input('\nEnter the number to be tested: '); 5 | if number<2 6 | disp('The number should be greater than 2. \n'); 7 | return; 8 | end 9 | 10 | if number ~= round(number) 11 | disp('The number should be a positive integer. \n'); 12 | return; 13 | end 14 | 15 | if is_prime(number) == 1 16 | fprintf('The number %d is prime. \n\n', number); 17 | else 18 | fprintf('The number %d is not prime. \n\n', number); 19 | end 20 | 21 | function check=is_prime(x) 22 | check=0; 23 | fact=factorial(sym((x-1))); 24 | if mod(fact+1,x) == 0 25 | check=1; 26 | end 27 | end -------------------------------------------------------------------------------- /Contributing.md: -------------------------------------------------------------------------------- 1 | # Contributing to Apress Source Code 2 | 3 | Copyright for Apress source code belongs to the author(s). However, under fair use you are encouraged to fork and contribute minor corrections and updates for the benefit of the author(s) and other readers. 4 | 5 | ## How to Contribute 6 | 7 | 1. Make sure you have a GitHub account. 8 | 2. Fork the repository for the relevant book. 9 | 3. Create a new branch on which to make your change, e.g. 10 | `git checkout -b my_code_contribution` 11 | 4. Commit your change. Include a commit message describing the correction. Please note that if your commit message is not clear, the correction will not be accepted. 12 | 5. Submit a pull request. 13 | 14 | Thank you for your contribution! -------------------------------------------------------------------------------- /Ch2 - MATLAB Cryptography Functions/Listing2-2.m: -------------------------------------------------------------------------------- 1 | clc; 2 | clear all; 3 | 4 | fprintf("Generating matrix... \n"); 5 | min_lim = 100; 6 | max_lim = 1000; 7 | v = round(min_lim +(max_lim-min_lim)*rand(2,10)); 8 | disp(v); 9 | 10 | fprintf("The length of v is %d. \n", length(v)); 11 | fprintf("The size of v is %d. \n", size(v)); 12 | 13 | fprintf("v has %d elements. \n", numel(v)); 14 | fprintf("\n"); 15 | fprintf("Sorting v... \n"); 16 | disp(sort(v)); 17 | 18 | fprintf("Sorting rows of v... \n"); 19 | disp(sortrows(v)); 20 | 21 | fprintf("Flipping v... \n"); 22 | disp(flip(v)); 23 | 24 | fprintf("Reshaping v... \n"); 25 | v2 = reshape(v, [5,4]); 26 | disp(v2); 27 | 28 | fprintf("Replicating v...\n"); 29 | v3 = repmat(v,2); 30 | disp(v3); -------------------------------------------------------------------------------- /Ch13 - Chaos-based Cryptography/Listing13_6.m: -------------------------------------------------------------------------------- 1 | %reading the encrypted images 2 | enc_image1=imread('enc_image1.jpg'); 3 | enc_image2=imread('enc_image2.jpg'); 4 | 5 | % compute the size of the of encrypted image 6 | % and assigned as rows and columns 7 | [rows,columns]=size(enc_image1); 8 | step=0; 9 | for i=1:1:rows 10 | for j=1:1:columns 11 | if(enc_image1(i,j)~=enc_image2(i,j)) step=step+1; 12 | else step=step+0; 13 | end 14 | end 15 | end 16 | 17 | NPCR =(step/(rows*columns))*100 18 | 19 | % encryption image 1 20 | subplot(1,2,1); 21 | imshow('enc_image1.jpg'); 22 | title('Encrypted Image 1'); 23 | 24 | % encryption image 2 25 | subplot(1,2,2); 26 | imshow('enc_image2.jpg'); 27 | title('Encrypted Image 2'); 28 | -------------------------------------------------------------------------------- /Ch5 - Number Theory/NaivePrimality.m: -------------------------------------------------------------------------------- 1 | fprintf('\nTesting the primality of a positive integer...'); 2 | clear all; close all; 3 | 4 | number = input('\nEnter the number to be tested: '); 5 | if number<2 6 | disp('The number should be greater than 2. \n'); 7 | return; 8 | end 9 | 10 | if number ~= round(number) 11 | disp('The number should be a positive integer. \n'); 12 | return; 13 | end 14 | 15 | if is_prime(number) == 1 16 | fprintf('The number %d is prime. \n\n', number); 17 | else 18 | fprintf('The number %d is not prime. \n\n', number); 19 | end 20 | 21 | function check=is_prime(x) 22 | check=1; 23 | for i = 2:sqrt(x) 24 | if mod(x,i) == 0 25 | check=0; 26 | return; 27 | end 28 | end 29 | end -------------------------------------------------------------------------------- /Ch5 - Number Theory/Fermat.m: -------------------------------------------------------------------------------- 1 | tic; 2 | fprintf('\nTesting the primality of a positive integer...'); 3 | clear all; close all; 4 | 5 | number = input('\nEnter the number to be tested: '); 6 | if number<2 7 | disp('The number should be greater than 2. \n'); 8 | return; 9 | end 10 | 11 | if number ~= round(number) 12 | disp('The number should be a positive integer. \n'); 13 | return; 14 | end 15 | 16 | if is_prime(number) == 1 17 | fprintf('The number %d is prime. \n\n', number); 18 | else 19 | fprintf('The number %d is not prime. \n\n', number); 20 | end 21 | toc; 22 | 23 | function check=is_prime(x) 24 | check=1; 25 | for i = 1:(x-1) 26 | if mod(power(sym(i),x-1),x)~=1 27 | check=0; 28 | return; 29 | end 30 | end 31 | end -------------------------------------------------------------------------------- /Ch4 - Basic Arithmetic Foundations/Listing4_1.m: -------------------------------------------------------------------------------- 1 | % positive numbers should be entered by the user 2 | x = input('Enter number X = '); 3 | y = input('Enter number Y = '); 4 | 5 | % validation of X number 6 | if isempty(x) 7 | error 'Number X -> You need to enter a value. Non-empty values are not allowed'; 8 | else 9 | x = abs(x); 10 | end 11 | 12 | % validation of Y number 13 | if isempty(y) 14 | error 'Number Y -> You need to enter a value. Non-empty values are not allowed'; 15 | else 16 | y = abs(y); 17 | end 18 | 19 | % computing the remainder 20 | remainder = x - y*floor(x/y); 21 | 22 | % we will perform untill 23 | while remainder ~= 0 24 | x = y; 25 | y = remainder; 26 | remainder = x - y*floor(x/y); 27 | end 28 | 29 | % show the result 30 | GreatCommonDivisor = y -------------------------------------------------------------------------------- /Ch6 - Classic Cryptography/HillAlgorithm/Hill_Encryption.m: -------------------------------------------------------------------------------- 1 | function console_output = Hill_Encryption(plaintext,matrix) 2 | 3 | [n n] = size(matrix); 4 | Array = plaintext - 'a'; 5 | array_length = length(Array); 6 | append_array_length = mod(array_length,n); %if this is postive, we need to append on the plaintext vector 7 | %to make its length a multiple of n 8 | if append_array_length > 0 9 | add_length = n - append_array_length; 10 | Array(array_length + 1: array_length + add_length) = 13; 11 | end 12 | 13 | columns_number = length(Array)/n; 14 | reshaped_array = reshape(Array, [n columns_number]); 15 | ciphertext = mod(matrix*reshaped_array,26); 16 | array_output = ciphertext(:)'; 17 | console_output = char(array_output + 'A'); 18 | 19 | 20 | -------------------------------------------------------------------------------- /Ch8 - Hash Functions/Listing82.m: -------------------------------------------------------------------------------- 1 | clear; clc; 2 | random_group_people = 100; % set the size of the group to 100 random people 3 | A = ones(100, random_group_people); % initialize array A to all 1's 4 | probability = 1; % initialize since probability for only 1 person is 1 5 | 6 | for i = 1 : 100 % begin for loop 7 | A(i) = 1 - probability; % each element of A assigned a probability 8 | probability = probability * (365 -i)/365; % then calculate new probability and repeat 9 | end % end for loop 10 | 11 | plot(A, 'green') % plot the number of people vs. the probability 12 | title('The probability that two people to have the same birthday from a group of 100 people'); 13 | xlabel('The size of the group of people'); 14 | ylabel('Probability for the same birthday'); -------------------------------------------------------------------------------- /Ch9 - Block Ciphers_ DES and AES/DES/Listing91.m: -------------------------------------------------------------------------------- 1 | function cryptography_key=Listing91(a,b) 2 | switch nargin 3 | case 0 4 | cryptography_key=round(rand(8,7)); 5 | cryptography_key(:,8)= mod(sum(cryptography_key,2)+1,2); 6 | case 1 7 | cryptography_key=cell(1,a); 8 | for i=1:a 9 | key=round(rand(8,7)); 10 | key(:,8)= mod(sum(key,2)+1,2); 11 | cryptography_key{i}=key; 12 | end 13 | case 2 14 | if a>1 15 | cryptography_key=cell(2,b); 16 | else 17 | cryptography_key=cell(1,b); 18 | end 19 | for j=1:m 20 | key= round(rand(8,7)); 21 | key(:,8)= mod(sum(key,2)+1,2); 22 | cryptography_key{1,j}=key; 23 | if a > 1 24 | cryptography_key{2,j}=round(rand(8,8)); 25 | end 26 | end 27 | end 28 | assignin('base','GENERATE_CRYPTO_KEY',cryptography_key) -------------------------------------------------------------------------------- /Ch5 - Number Theory/Fermat2.m: -------------------------------------------------------------------------------- 1 | tic; 2 | fprintf('\nTesting the primality of a positive integer...'); 3 | clear all; close all; 4 | 5 | number = input('\nEnter the number to be tested: '); 6 | if number<2 7 | disp('The number should be greater than 2. \n'); 8 | return; 9 | end 10 | 11 | if number ~= round(number) 12 | disp('The number should be a positive integer. \n'); 13 | return; 14 | end 15 | 16 | if is_prime(number) == 1 17 | fprintf('The number %d might be prime. \n\n', number); 18 | else 19 | fprintf('The number %d is not prime. \n\n', number); 20 | end 21 | toc; 22 | 23 | function check=is_prime(x) 24 | check=1; 25 | a=generate_coprime(3,x,x); 26 | b=mod(power(a,sym(x-1)),x); 27 | if b~=1 28 | check=0; 29 | end 30 | end 31 | 32 | function q = generate_coprime(a,b,n) 33 | aux=round(a+(b-a)*rand(1,1)); 34 | while gcd(n,aux)~=1 35 | aux=round(a+(b-a)*rand(1,1)); 36 | end 37 | q=aux; 38 | end -------------------------------------------------------------------------------- /Ch12 - Visual Cryptography/Shamir.m: -------------------------------------------------------------------------------- 1 | clear all; 2 | clc; 3 | original = imread('apress.JPG'); 4 | black_white = imbinarize(rgb2gray(original)); 5 | 6 | [bw_row0,bw_col0]=size(black_white); 7 | share_1=num2cell(ones(bw_row0,bw_col0)); 8 | share_2=share_1; 9 | for i = 1:bw_row0 10 | for j = 1:bw_col0 11 | if black_white(i,j)==0 12 | rand_pixel = randsrc(1,1,[0 1; 0.5 0.5]); 13 | share_1{i,j}=[rand_pixel ~rand_pixel]; 14 | share_2{i,j}=[rand_pixel ~rand_pixel]; 15 | else 16 | rand_pixel = randsrc(1,1,[0 1; 0.5 0.5]); 17 | share_1{i,j}=[rand_pixel ~rand_pixel]; 18 | share_2{i,j}=[~rand_pixel rand_pixel]; 19 | end 20 | end 21 | end 22 | 23 | figure; 24 | imshow(black_white); 25 | title('Black-white image'); 26 | 27 | figure; 28 | imshow(cell2mat(share_1)); 29 | title('First Share'); 30 | 31 | figure; 32 | imshow(cell2mat(share_2)); 33 | title('Second Share'); 34 | 35 | figure; 36 | recovered=cell2mat(share_1)+cell2mat(share_2); 37 | imshow(recovered); 38 | title('Recovered image: Overlapping Share 1 and Share 2'); -------------------------------------------------------------------------------- /Ch13 - Chaos-based Cryptography/Listing13_2.m: -------------------------------------------------------------------------------- 1 | clear all; 2 | 3 | %load two encrypted images for computing UACI 4 | encrypted_image1=imread('enc_image1.jpg'); 5 | encrypted_image2=imread('enc_image2.jpg'); 6 | 7 | %compute the size of the first image 8 | [rows,columns]=size(encrypted_image1); 9 | 10 | %create a matrix with rows and columns 11 | first_step=zeros(rows,columns); 12 | 13 | %acording to the formula presented above 14 | for i=1:1:rows 15 | for j=1:1:columns 16 | if(encrypted_image1(i,j)>encrypted_image2(i,j)) 17 | first_step(i,j)=(encrypted_image1(i,j)-encrypted_image2(i,j)); 18 | else 19 | first_step(i,j)=(encrypted_image2(i,j)-encrypted_image1(i,j)); 20 | end 21 | end 22 | end 23 | 24 | first_step=first_step/255; 25 | 26 | %computing the next step 27 | second_step=0; 28 | for i=1:1:rows 29 | for j=1:1:columns 30 | second_step=second_step+step(i,j); 31 | end 32 | end 33 | 34 | % the last step is multiplying the result of division between second step and 35 | % the product of rows with columns, with 100 - according 36 | uaci=(second_step/(rows*columns))*100 37 | 38 | % encryption image 1 39 | subplot(1,2,1); 40 | imshow('enc_image1.jpg'); 41 | title('Encrypted Image 1'); 42 | 43 | % encryption image 2 44 | subplot(1,2,2); 45 | imshow('enc_image2.jpg'); 46 | title('Encrypted Image 2'); 47 | 48 | 49 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Freeware License, some rights reserved 2 | 3 | Copyright (c) 2021 Marius Iulian Mihailescu and Stefania Loredana Nita 4 | 5 | Permission is hereby granted, free of charge, to anyone obtaining a copy 6 | of this software and associated documentation files (the "Software"), 7 | to work with the Software within the limits of freeware distribution and fair use. 8 | This includes the rights to use, copy, and modify the Software for personal use. 9 | Users are also allowed and encouraged to submit corrections and modifications 10 | to the Software for the benefit of other users. 11 | 12 | It is not allowed to reuse, modify, or redistribute the Software for 13 | commercial use in any way, or for a user’s educational materials such as books 14 | or blog articles without prior permission from the copyright holder. 15 | 16 | The above copyright notice and this permission notice need to be included 17 | in all copies or substantial portions of the software. 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | SOFTWARE. 26 | 27 | 28 | -------------------------------------------------------------------------------- /Ch8 - Hash Functions/Listing83.m: -------------------------------------------------------------------------------- 1 | function my_hash_result = Listing83(data_input,hash_function) 2 | data_input=data_input(:); 3 | % the data input will be converted into an UINT8 format 4 | if ischar(data_input) || islogical(data_input) 5 | data_input=uint8(data_input); 6 | 7 | % implemented for not having any losses of the input data 8 | else 9 | data_input=typecast(data_input,'uint8'); 10 | end 11 | 12 | % verify hash method 13 | hash_function=upper(hash_function); 14 | switch hash_function 15 | case 'SHA1' 16 | hash_function='SHA-1'; 17 | case 'SHA256' 18 | hash_function='SHA-256'; 19 | case 'SHA384' 20 | hash_function='SHA-384'; 21 | case 'SHA512' 22 | hash_function='SHA-512'; 23 | otherwise 24 | end 25 | 26 | al={'MD2','MD5','SHA-1','SHA-256','SHA-384','SHA-512'}; 27 | if isempty(strmatch(hash_function,al,'exact')) 28 | error(['Hash algorithm must be ' ... 29 | MD2, MD5, SHA-1, SHA-256, SHA-384, or SHA-512']); 30 | end 31 | 32 | % generate the hash using the mentioned algorithms 33 | jhf=java.security.MessageDigest.getInstance(hash_function); 34 | jhf.update(data_input); 35 | my_hash_result=typecast(jhf.digest,'uint8'); 36 | my_hash_result=dec2hex(my_hash_result)'; 37 | 38 | % if it is the case that all hashes bytes are less than 39 | % 128, then perform with a padding operation 40 | if(size(my_hash_result,1))==1 41 | my_hash_result=[repmat('0',[1 size(my_hash_result,2)]);my_hash_result]; 42 | end 43 | my_hash_result=lower(my_hash_result(:)'); 44 | clear x 45 | return 46 | -------------------------------------------------------------------------------- /Ch13 - Chaos-based Cryptography/Listing13_5.m: -------------------------------------------------------------------------------- 1 | %corellation on vertical 2 | clc; 3 | clear all; 4 | 5 | %control parameters 6 | ctrl_p_1=0; 7 | ctrl_p_2=0; 8 | indexJ = 1; 9 | indexI = 1; 10 | 11 | %load the image for which the corellation should be performed 12 | encrypted_image=imread('enc_image1.jpg'); 13 | 14 | %use the encrypted image and represented it as a RGB version and not as a 15 | %grayscale version conversion 16 | representation_as_rgb=encrypted_image; 17 | 18 | %computing the size in order to represented as a matrix of rows and columns 19 | [rows,columns]=size(representation_as_rgb); 20 | 21 | %compute the total length by multiplying rows with columns 22 | total_length=rows*columns; 23 | 24 | %generate structure with zeroes from 1 to 10240 (256*40) 25 | x_axis=zeros(1,(256*40)); 26 | y_axis=zeros(1,(256*40)); 27 | 28 | while indexJ <= 80 29 | while indexI <= rows 30 | if(mod(indexJ,2)==0) 31 | ctrl_p_1=ctrl_p_1+1; 32 | x_axis(1,ctrl_p_1)=representation_as_rgb(indexI,indexJ); 33 | indexI = indexI+1; 34 | indexJ = indexJ+1; 35 | disp(x_axis(1,ctrl_p_1)); 36 | else 37 | ctrl_p_2=ctrl_p_2+1; 38 | y_axis(1,ctrl_p_2)=representation_as_rgb(indexI,indexJ); 39 | indexI = indexI+1; 40 | indexJ = indexJ+1; 41 | disp(y_axis(1,ctrl_p_2)); 42 | end 43 | end 44 | end 45 | 46 | %figure 47 | scatter(x_axis,y_axis,2) 48 | title('Encrypted image 1 - Enc(Img1) - vertical correlation'); 49 | ENC_IMG1 = corrcoef(x_axis,y_axis) -------------------------------------------------------------------------------- /Ch6 - Classic Cryptography/CaesarAlgorithm/caesaralgorithm.m: -------------------------------------------------------------------------------- 1 | function console_output = caesaralgorithm() 2 | alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o','p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']; 3 | clear_text = input('Enter a phrase that you wish to encrypt it (eliminate the spaces): ','s'); %our input 4 | key = input('Enter the key: '); 5 | exception_message = 'There is an invalid input. Please restart the program.'; 6 | 7 | if key > 26 || key < 0 8 | key = mod(key,26); 9 | end 10 | 11 | breaking_loop = 0; 12 | iteration_flag = 1; 13 | 14 | while iteration_flag <= length(clear_text) 15 | if ismember(clear_text(iteration_flag),'A':'Z') == 1 16 | breaking_loop = 1; 17 | console_output = exception_message; 18 | 19 | elseif isletter(clear_text(iteration_flag)) == 1 20 | y = strfind(alphabet,clear_text(iteration_flag)); 21 | z = y+key; 22 | if z > 26 23 | z = z - 26; 24 | end 25 | console_output(iteration_flag) = alphabet(z); 26 | 27 | elseif isletter(clear_text(iteration_flag)) == 0 28 | if plain(iteration_flag) == ' ' 29 | console_output(iteration_flag) = clear_text(iteration_flag); 30 | else 31 | breaking_loop = 1; 32 | console_output = exception_message; 33 | end 34 | end 35 | 36 | iteration_flag = iteration_flag+1; 37 | if breaking_loop == 1 38 | break 39 | end 40 | end 41 | end 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /Ch2 - MATLAB Cryptography Functions/Listing2-1.m: -------------------------------------------------------------------------------- 1 | clc; 2 | clear all; 3 | 4 | no1 = 25; 5 | bin1 = dec2bin(no1); 6 | fprintf("The binary value of %d is: %s. \n", no1, bin1); 7 | no2 = -65; 8 | bin1 = dec2bin(no2); 9 | fprintf("The binary value of %d is: %s. \n", no2, bin1); 10 | fprintf("\n"); 11 | 12 | no3 = 1739; 13 | set_res = bitset(no3, 5); 14 | fprintf("Changing the second bit of %d to 1 results in: %d. \n", no3, set_res); 15 | fprintf("\n"); 16 | 17 | get_res = bitget(no3,3); 18 | fprintf("The binary value of %d is %s and the 3rd bit is %d. \n", no3, dec2bin(no1), get_res); 19 | fprintf("\n"); 20 | 21 | no4 = round(2000 +(10000-2000)*rand(1,1)); 22 | shift_res = bitshift(no4, 5); 23 | fprintf("Shifting 5 bits to left of %d (%s) results in %d (%s). \n", no4, dec2bin(no4), shift_res, dec2bin(shift_res)); 24 | shift_res2 = bitshift(no4, -5); 25 | fprintf("Shifting 5 bits to right of %d (%s) results in %d (%s). \n", no4, dec2bin(no4), shift_res2, dec2bin(shift_res2)); 26 | shift_res = bitshift(0-no4, 5, 'int64'); 27 | fprintf("Shifting 5 bits to left of %d (%s) results in %d (%s). \n", no4, dec2bin(no4), shift_res, dec2bin(shift_res)); 28 | shift_res2 = bitshift(0-no4, -5, 'int64'); 29 | fprintf("Shifting 5 bits to right of %d (%s) results in %d (%s). \n", no4, dec2bin(no4), shift_res2, dec2bin(shift_res2)); 30 | fprintf("\n"); 31 | 32 | cmp_res = bitcmp(no4, 'int64'); 33 | fprintf("The bit-wise complement of %d (%s) is %d (%s). \n", no4, dec2bin(no4), cmp_res, dec2bin(cmp_res)); 34 | fprintf("\n"); 35 | 36 | no5 = round(2000 +(10000-2000)*rand(1,1)); 37 | fprintf("%d (%s) OR %d (%s) = %d (%s) \n", no4, dec2bin(no4), no5, dec2bin(no5), bitor(no4,no5), dec2bin(bitor(no4,no5))); 38 | fprintf("%d (%s) AND %d (%s) = %d (%s) \n", no4, dec2bin(no4), no5, dec2bin(no5), bitand(no4,no5), dec2bin(bitand(no4,no5))); 39 | fprintf("%d (%s) XOR %d (%s) = %d (%s) \n", no4, dec2bin(no4), no5, dec2bin(no5), bitxor(no4,no5), dec2bin(bitxor(no4,no5))); 40 | fprintf("\n"); 41 | 42 | no6 = 0x134DF5ED; 43 | swap_res = swapbytes(no6); 44 | fprintf("Number %d (%s) swapped is %d (%s). \n", no6, dec2hex(no6), swap_res, dec2hex(swap_res)); 45 | -------------------------------------------------------------------------------- /Ch10 - Asymmetric Encryption/RSA.m: -------------------------------------------------------------------------------- 1 | clc; 2 | disp('RSA Algorithm'); 3 | clear all; close all; 4 | 5 | %p = input('\nEnter the first prime number (p) : '); 6 | %q = input('Enter the second prime number (q) : '); 7 | 8 | p=generate_prime(sym(2)^10,sym(2)^11); 9 | fprintf('Computed value (p): %d\n', p); 10 | 11 | q=generate_prime(sym(2)^10,sym(2)^11); 12 | fprintf('Computed value (q): %d \n', q); 13 | 14 | fprintf('\n') 15 | 16 | n=p*q; 17 | fprintf('Computed value (n): %d\n', n); 18 | 19 | phi_n=(p-1)*(q-1); 20 | fprintf('Computed value (phi_n): %d\n', phi_n); 21 | 22 | e=-1; 23 | val=round(1+(phi_n-1)*rand(1,1)); 24 | if gcd(val,phi_n) ~= 1 25 | while gcd(val,phi_n) ~= 1 || val ==1 26 | val=round(1+(phi_n-1)*rand(1,1)); 27 | if gcd(val,phi_n) == 1 28 | e=val; 29 | break; 30 | end 31 | end 32 | end 33 | 34 | fprintf('Computed value (e): %d\n', e); 35 | 36 | [aux, aux2, ~] = gcd(e,phi_n); 37 | if aux==1 38 | d = mod(aux2,phi_n); 39 | fprintf('Computed value (d): %d\n', d); 40 | else 41 | disp('Value (d) cannot be computed'); 42 | end 43 | 44 | fprintf('\n'); 45 | fprintf('Public key: (%d, %d)\n', n, e); 46 | fprintf('Public key: (%d, %d, %d, %d)\n', p, q, phi_n, d); 47 | 48 | 49 | plain_message = input('\nEnter the message to be sent (m < n): '); 50 | if plain_message>n 51 | disp('The message (m) should be less than (n).'); 52 | return; 53 | end 54 | encrypted_message=power(sym(plain_message), e); 55 | encrypted_message=mod(encrypted_message, n); 56 | disp('Encrypted message: '); 57 | disp(encrypted_message); 58 | 59 | decrypted_message=power(sym(encrypted_message), d); 60 | decrypted_message=mod(decrypted_message, n); 61 | disp('Decrypted message: '); 62 | disp(decrypted_message); 63 | 64 | function q = generate_prime(a,b) 65 | aux=round(a+(b-a)*rand(1,1)); 66 | while is_prime(aux) == 0 67 | aux=round(a+(b-a)*rand(1,1)); 68 | end 69 | q=aux; 70 | end 71 | 72 | function check=is_prime(x) 73 | check=1; 74 | for i = 2:sqrt(x) 75 | if mod(x,i) == 0 76 | check=0; 77 | return; 78 | end 79 | end 80 | end -------------------------------------------------------------------------------- /Ch10 - Asymmetric Encryption/ElGamal.m: -------------------------------------------------------------------------------- 1 | clc; 2 | disp('ElGamal Algorithm'); 3 | clear all; close all; 4 | 5 | q=generate_prime(sym(2)^10,sym(2)^11); 6 | disp('Computed value (q): '); 7 | disp(q) 8 | 9 | g=round(1+((q-1)-1)*rand(1,1)); 10 | disp('Computed value (g): '); 11 | disp(g); 12 | 13 | x=round(1+((q-1)-1)*rand(1,1)); 14 | disp('Computed value (x): '); 15 | disp(x); 16 | 17 | h=power(sym(g),x); 18 | disp('Computed value (h): '); 19 | disp(h); 20 | 21 | plain_message = input('\nEnter the message to be sent (m < q): '); 22 | if plain_message>q 23 | disp('The message (m) should be less than (q).'); 24 | return; 25 | end 26 | 27 | fprintf('\nEncrypting...\n'); 28 | 29 | y=round(1+((q-1)-1)*rand(1,1)); 30 | disp('Computed value (y): '); 31 | disp(y); 32 | 33 | s=power(sym(h),y); 34 | disp('Computed value (s) [shared secret - sender]: '); 35 | disp(s); 36 | 37 | c1=power(sym(g),y); 38 | disp('Computed value (c1): '); 39 | disp(c1); 40 | 41 | c2=plain_message*s; 42 | disp('Computed value (c2): '); 43 | disp(c2); 44 | 45 | disp('Encrypted message has two components: '); 46 | fprintf(' c1 = %sym.\n', c1); 47 | fprintf(' c2 = %sym.\n', c2); 48 | 49 | fprintf('\nDecrypting...'); 50 | 51 | ss=power(sym(c1),x); 52 | fprintf('\nComputed value (ss) [shared secret - receiver]: '); 53 | disp(ss); 54 | 55 | inv_s = compute_inverse(ss,q); 56 | if inv_s == -1 57 | disp('Wrong setup. Retry.'); 58 | return; 59 | end 60 | disp('Computed value (ss^-1): '); 61 | disp(inv_s); 62 | 63 | decrypted_message=mod(c2*inv_s,q); 64 | disp('Decrypted message '); 65 | disp(decrypted_message); 66 | 67 | function q = generate_prime(a,b) 68 | aux=round(a+(b-a)*rand(1,1)); 69 | while is_prime(aux) == 0 70 | aux=round(a+(b-a)*rand(1,1)); 71 | end 72 | q=aux; 73 | end 74 | 75 | function check=is_prime(x) 76 | check=1; 77 | for i = 2:sqrt(x) 78 | if mod(x,i) == 0 79 | check=0; 80 | return; 81 | end 82 | end 83 | end 84 | 85 | function inv = compute_inverse(a,b) 86 | [aux, aux2, ~] = gcd(a,b); 87 | if aux==1 88 | inv = mod(aux2,b); 89 | else 90 | inv = -1; 91 | end 92 | end -------------------------------------------------------------------------------- /Ch14 - Steganography/Listing14_1.m: -------------------------------------------------------------------------------- 1 | clc; 2 | clear variables; 3 | 4 | % variables 5 | binary_representation = ''; 6 | image = ''; 7 | hidden_image = ''; 8 | rValues = ''; 9 | cValues = ''; 10 | ccValues = ''; 11 | string_length = ''; 12 | ascii_representation = ''; 13 | ascii_binary_representation = ''; 14 | control_bit=''; % used to count how many bits were hidden 15 | 16 | % read the image used for hidden process, 17 | % convert it to the grayscale if the image is RGB 18 | image=imread('swan_input.jpg'); 19 | 20 | % compute the size of the image and store the 21 | % values for rows, columns and color of channels 22 | % rValues - rows number/value 23 | % cValues - columns number/value 24 | % ccValues - number of the color channels 25 | [rValues, cValues, ccValues] = size(image); 26 | 27 | % provide the conversion from color to grayscale if the color channels 28 | % number is bigger and strict with 1 29 | if ccValues > 1 30 | image = rgb2gray(image); 31 | end 32 | 33 | % initialize the image that will be used for hiding with the image obtained 34 | % due to the conversion process from color to grayscale. 35 | hidden_image=image; 36 | 37 | % read the message that will be hide within the images 38 | hidden_message=input('Provide a message that will be hidden in the image: ','s'); 39 | 40 | % very important stept is to understand that each character from the 41 | % message entered by the user is represented as 1 byte = 8 bits. 42 | % we will require to compute the length of the entire message by mutiplying 43 | % it's length with 8 (bits). 44 | string_length=strlength(hidden_message)*8; 45 | 46 | % compute the ascii values by computing unsigned integer values with only 8 47 | % bits of information for the hidden message entered above. 48 | ascii_representation=uint8(hidden_message); 49 | 50 | % convert the decimal to binary the ascii values with only 8 bits of 51 | % information and store them in 52 | ascii_binary_representation=dec2bin(ascii_representation,8); 53 | 54 | % store the binary values of ascii value as a string 55 | for i=1:strlength(hidden_message) 56 | binary_representation=append(binary_representation,ascii_binary_representation(i,:)); 57 | end 58 | 59 | for i=1:rValues % row index 60 | for j=1:cValues % column index 61 | control_bit=1; 62 | if control_bit<=string_length 63 | % compute using modulo 2 the grey level for each of the pixel 64 | modulo = mod(image(i,j),2); 65 | least_significant_bit=modulo; 66 | 67 | %Convert the bit from the message to numeric form 68 | 69 | % compute the binary representation of the bit that represents 70 | % the numeric form 71 | binaryRepresentation = binary_representation(control_bit); 72 | 73 | % convert string to double for the binary representation 74 | doubleBinaryRepresentation=str2double(binaryRepresentation); 75 | 76 | % compute the XOR for the bit and least significant bit 77 | xor_temp = xor(least_significant_bit,doubleBinaryRepresentation); 78 | xor_temp_to_double = double(xor_temp); 79 | 80 | % Change the bit of the image_hide accordingly 81 | % compute the addition between the pixel and xor double value 82 | addition_pixel_xor = image(i,j)+xor_temp_to_double; 83 | 84 | % add the addition result to a specific 85 | % pixel from the hidden image 86 | hidden_image(i,j) = addition_pixel_xor; 87 | 88 | % move to the next bit 89 | control_bit=control_bit+1; 90 | end 91 | end 92 | end 93 | 94 | % original image representation 95 | subplot(1,2,1); 96 | imshow(image); 97 | title('Original image'); 98 | 99 | % new image with the message hidden 100 | subplot(1,2,2); 101 | imshow(hidden_image); 102 | title('Hidden image'); 103 | 104 | sgt = sgtitle('Hidding information to image using LSB method','Color','blue'); 105 | sgt.FontSize = 20; 106 | 107 | imwrite(hidden_image,'swan_output.png') --------------------------------------------------------------------------------