Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 63

TABLE OF CONTENT

CHAPTER No. TITLE PAGE No.

1. INTRODUCTION
1.1 Aim
1.2 Objectives
1.3 Scope of the work
1.4 Problem Formulation
1.5 AES-Encryption and
Decryption Specification

2. LITERATURE SURVEY

3. METHODOLOGY
3.1 Advance encryption process
3.1.1. AES Process
3.2. AES Encryption
3.2.1. Substitution Bytes
3.2.2. Mix Columns
3.2,3. Add Round Key
3.3. AES Decryption
3.3.1. Inverse Substitution Bytes
3.3.2. Inverse Shift Row
3.3.3. Inverse Mix Column
4.
Chapter 1
INTRODUCTION

With worldwide communication of private and confidential data over the computer
networks or the Internet, there is always a possibility of threat to data
confidentiality, data integrity and, also data availability. Data encryption maintains
data confidentiality, integrity and authentication. Information has become of the
most important assets in growing demand of need to store every single importance
of events in everyday life. Messages need to be secured from unauthorized party.

The Advanced Encryption Standard (AES) is considered as one of the strongest


published cryptographic algorithms. National Institute of Standards and
Technology (NIST) adopted Advanced Encryption Standard (AES) as the standard
for encryption and decryption of blocks of data after the failing of the Data
Encryption Standard (DES).

The draft is published under the name as FIPS-197 (Federal Information


Processing Standard number 197) [5]. Moreover, it is used in many applications
such as in text and image processing etc. AES is widely used for encryption of
audio/video data contents in real time.

The AES is an iterative algorithm and uses four operations in different rounds,
namely SubBytes, ShiftRows, MixColumns and Key Additions transformations.
SubBytes transformation is done through S-box. S-box is the vital component in
the AES architecture that decides the speed/throughput of the AES. The composite
field arithmetic is more suitable for S-box (substitution) implementation its
hardware optimization for VLSI implementation is very important to reduce the
area and power of the AES architecture.
1.1 Aim

THE AIM OF THE PROJECT IS IMAGE AND TEXT ENCRYPTION AND


DECRYPTION USING AES ALGORITHM.

1.2 Objective

The objective criteria is that to get required data, to be encrypted in form text or
images. The secret private key is given to algorithm. Then based on the key the
text or image is encrypted. To decrypt the encrypted image, encrypted data (text) is
given to the algorithm, Then the correct secret key is given, algorithm verify the
secret key, then encrypted data, is decrypted to original form.

1.3 Scope of the work

Image processing is a mechanism in which an original image will be converted


into digital image and after converting in digital form process it to get useful
information. It is a type of signal processing in which input is an image and output
may be image or characteristics/features associated with that image.

In recent years, the advances in communication technology have seen strong


interest in digital image transmission. However, growth of computer processor
possessing power and storage illegal access has become easier. Encryption
involves applying special mathematical algorithms and keys to transform digital
data into cipher code before they are transmitted and decryption involves the
application of mathematical algorithms and keys to get back the original data from
cipher code, scientific community have seen strong interest in image transmission.

Information privacy becomes a challenging issue. In order to protect valuable data


or image from undesirable readers, data or image encryption / decryption is
essential, furthermore. As such in this paper, a scheme based on encryption has
been proposed for secure image transmission over channels.
1.4 Problem formulation

The primary goal is to implement AES encryption and decryption for securing
both text and image data.

Implement AES encryption and decryption algorithms for 128-bit key size.

Develop functionalities to encrypt and decrypt both text and image files.

Ensure compatibility across different platforms and file formats.

Implement secure key management practices to safeguard encryption keys.

1.5 AES – Encryption and Decryption Specification

AES is called AES-128, AES-192 and AES-256. This classification depends on the
different key size used for cryptographic process. Those different key sizes are
used to increase the security level. As, the key size increases the security level
increases. Hence, key size is directly proportional to the security level. The input
for AES process is a single block of 128 bits. The processing is carried out in
several number of rounds where it depends on the key length: 16-byte key consists
of 10 rounds, 24 byte key consists of 12 rounds, and 32 byte key consists of 14
rounds. The first round of encryption process consists of four distinct
transformation functions:

 Substitution Bytes

 ShiftRows

 MixColumns

 AddRoundKey
The final round consists of only three transformations ignoring MixColumns. The
Decryption method is the reverse of encryption and it consists of four
transformations.

 Inverse Substitution Bytes

 Inverse ShiftRows

 Inverse MixColumns

 AddRoundKey
Chapter 2

LITERATURE SURVEY

The paper provides an overview of the AES algorithm and its significance in
cryptography, particularly in securing sensitive information. AES, adopted by the
U.S. government, offers robust protection against various forms of attacks,
ensuring data confidentiality and integrity. The method employs symmetric key
encryption, where both parties involved in communication share the same secret
key.

The document elaborates on the encryption and decryption processes using AES
for text files, images, and audio files, employing OpenSSL commands. Time
measurements are included to assess the execution efficiency of these operations.
The results demonstrate that text file encryption takes the longest time, followed
by images and audio files. Additionally, decryption generally consumes more time
compared to encryption.

The paper concludes by affirming the successful implementation of AES


encryption and decryption using OpenSSL, highlighting its effectiveness in
safeguarding data and preventing unauthorized access. It suggests future research
avenues, such as exploring audio and video encryption using different tools and
comparing their efficiency with OpenSSL. It could also discuss recent
advancements, challenges, and potential areas for further exploration in the field of
cryptography and data security.
Chapter 3

METHODOLOGY

3.1. ADVANCED ENCRYPTION PROCESS

3.1.1 AES PROCESS

The characteristics of AES algorithm are Uses common (Symmetric) key and so, It
is called as Symmetric block cipher. The size of data is 128 bits, and so the key
size varies from 128/192/256 bits. It is highly robust and 6 times faster than 3-DES
and much faster and powerful than RSA. It uses single S-box for all rounds rather
than DES, where it uses eight Sboxes. The process involved in AES is iterative
when compared with Feistel cipher. AES is generally based on Substitution and
Permutations operations.

It consists of sequence of linked operations at each round. Each round consists of


the following process. 1) Interchanging of inputs by the outputs produced by the
substitution process. 2) The bits are shifted in a cyclic manner. 3) The columns are
mixed by transformation. 4) The key has been added with input using XOR
operation. The computation of AES is performed at bytes level, so 128 bits of
plain-text is considered as a block of 16 bytes which constitute a 4x4 matrix. Each
round of AES depends on the key length. AES performs 10/12/14 rounds
depending on the key size 128/192/256 respectively. So, AES is known to be AES-
128, AES-192 and AES-256 algorithm based on key size. A unique 128-bit round
key is given as an input at each round.
Fig 1. Process of AES Encryption and Decryption

3.2 AES Encryption


3.2.1 SUBSTITUTION BYTES:
The 16 byte plain-text substitutes the corresponding value from substitution table
S-box. It is a non-linear method which performs in the following way:
Table 1. S-Box
3.2.2 MIX COLUMNS:
MixColumns transformation performs by transforming each column of four bytes.
It takes input as one column which is of 4 bytes and output as completely different
4 bytes by transforming the original column. The resultant matrix is same as the
size of plain-text. Mix Column transformation will not be carried in the last round.
Fig.2 Mix Column process

Fig.3 Mix Column Matrix

3.2.3 ADD ROUND KEY

The 16 bytes which is produced from MixColumns is equal to 128 bits which is
XORed with the round key of 128 bits. The above process has been repeated until
final round to produce the corresponding cipher text.
3.3 AES Decryption

3.3.1. INVERSE SUBSTITUTION BYTES:

Inverse Substitution Bytes is the inverse of the substitution byte transformation.


This is performed through inverse S-box [6,7]. This is obtained by applying inverse
of substitution bytes and by computing multiplicative inverse of Galois Field - GF
(2^8).

Table.2 INVERSE S-BOX

3.3.2. INVERSE SHIFTROW:

Inverse ShiftRows is the inverse of ShiftRows transformation. It carries out


circular shifts in reverse direction for each last 3 rows and for the 2nd row, it
performs one-byte circular shift to the right and it continues the process till (n-3)rd
row.
Fig.5 Inverse shift row

3.3.3. INVERSE MIXCOLUMNS:

Inverse MixColumns is the inverse of Mix columns transformation. It carries out


operations on a matrix by column-wise. Resultant columns are in the form of
polynomials

Fig.6 Inverse Mix Column Fixed Matrix


Chapter 4

IMPLIMENTATION

4.1.1 AES-ENCRYPTION ALGORITHM:

The implementation of AES-encryption is done in java. The input given to the


encryption process is a Plain-text (or) a Plain-image. The image is divided into 4*4
matrix and so there are 10 rounds performed for this process. So, there are totally
nine rounds performed with 4 transformations (i.e.) subbytes, shiftrows,
mixcolumns and addroundkey. And 10th round consists of three transformations
ignoring Mixcolumns [9,10]. The original image will be encrypted and the
encrypted image will be displayed as another random image to the sender. This
encryption process also applied on text and the encryption process is same and the
result is obtained.

4.1.2 AES DECRYPTION ALGORITHM:

The AES decryption is an inverse of encryption process. The below figure shows
the decryption process. The decryption process takes the cipher-image (or) cipher-
text as input and it performs the four transformations (i.e.) Inverse subbytes,
Inverse Shiftrows, Inverse Mixcolumns and Addroundkey until 9 rounds and the
10th round performs three transformation ignoring Inverse Mixcolumns.The
output of decryption process will produce the original image to the receiver.
Fig.7 AES ENCRYPTION.

Fig.8 AES DECRYPTION


Chapter 5

5.1Simulation and Synthesis Implementation

Source code:

module AES_TOP(clk,finalout);
input clk;
output [7:0] finalout;
wire [127:0] tempout;
aescipher u1(.clk(clk),.datain(128'h 54776f204f6e65204e696e652054776f),.key(128'h
5468617473206d79204b756e67204675),.dataout(tempout));
assign finalout = tempout[127:120];
endmodule.

module AES_TB;
Inputs reg clk;
Outputs wire [7:0] finalout;
Instantiate the Unit Under Test (UUT)
AES_TOP uut (clk(clk),finalout(finalout));
initial begin
clk = 0;
#100;
end
endmodule

module aescipher(clk,datain,key,dataout);
input clk;
input [127:0] datain;
input [127:0] key;
output[127:0] dataout;

wire [127:0] r0_out;


wire [127:0] r1_out,r2_out,r3_out,r4_out,r5_out,r6_out, r7_out,r8_out,r9_out;
wire[127:0]keyout1,keyout2,keyout3,keyout4,keyout5,keyout6,keyout7,
keyout8,keyout9;
assign r0_out = datain^key;
rounds
r1(.clk(clk),.rc(4'b0000),.data(r0_out),.keyin(key),.keyout(keyout1),.rndout(r1_out));
rounds
r2(.clk(clk),.rc(4'b0001),.data(r1_out),.keyin(keyout1),.keyout(keyout2),.rndout(r2_out));
rounds
r3(.clk(clk),.rc(4'b0010),.data(r2_out),.keyin(keyout2),.keyout(keyout3),.rndout(r3_out));
rounds
r4(.clk(clk),.rc(4'b0011),.data(r3_out),.keyin(keyout3),.keyout(keyout4),.rndout(r4_out));
rounds;wq
r5(.clk(clk),.rc(4'b0100),.data(r4_out),.keyin(keyout4),.keyout(keyout5),.rndout(r5_out));
rounds
r6(.clk(clk),.rc(4'b0101),.data(r5_out),.keyin(keyout5),.keyout(keyout6),.rndout(r6_out));
rounds
r7(.clk(clk),.rc(4'b0110),.data(r6_out),.keyin(keyout6),.keyout(keyout7),.rndout(r7_out));
rounds
r8(.clk(clk),.rc(4'b0111),.data(r7_out),.keyin(keyout7),.keyout(keyout8),.rndout(r8_out));
rounds
r9(.clk(clk),.rc(4'b1000),.data(r8_out),.keyin(keyout8),.keyout(keyout9),.rndout(r9_out));
rounndlast r10(.clk(clk),.rc(4'b1001),.rin(r9_out),.keylastin(keyout9),.fout(dataout));
endmodule
.
module aescipher_tb;
parameter CLK_PERIOD = 10; // Clock period in ns
reg clk;
reg [127:0] datain;
reg [127:0] key;
wire [127:0] dataout;
aescipher dut (.clk(clk),datain(datain), .key(key),.dataout(dataout));
initial begin
clk = 0;
forever #((CLK_PERIOD/2)) clk = ~clk;
end
initial begin
datain = 128'h00000000000000000000000000000000; // Example: Setting datain
to all zeros
key = 128'h00000000000000000000000000000000; // Example: Setting key to all
zeros
#100; // Wait for a few clock cycles
$display("datain: %h", datain);
$display("key: %h", key);
$display("dataout: %h", dataout);
$finish;
end
endmodule.
module subbytes(data,sb);
input [127:0] data;
output [127:0] sb;
sbox q0( .a(data[127:120]),.c(sb[127:120]) );
sbox q1( .a(data[119:112]),.c(sb[119:112]) );
sbox q2( .a(data[111:104]),.c(sb[111:104]) );
sbox q3( .a(data[103:96]),.c(sb[103:96]) );
sbox q4( .a(data[95:88]),.c(sb[95:88]) );
sbox q5( .a(data[87:80]),.c(sb[87:80]) );
sbox q6( .a(data[79:72]),.c(sb[79:72]) );
sbox q7( .a(data[71:64]),.c(sb[71:64]) );
sbox q8( .a(data[63:56]),.c(sb[63:56]) );
sbox q9( .a(data[55:48]),.c(sb[55:48]) );
sbox q10(.a(data[47:40]),.c(sb[47:40]) );
sbox q11(.a(data[39:32]),.c(sb[39:32]) );
sbox q12(.a(data[31:24]),.c(sb[31:24]) );
sbox q13(.a(data[23:16]),.c(sb[23:16]) );
sbox q14(.a(data[15:8]),.c(sb[15:8]) );
sbox q16(.a(data[7:0]),.c(sb[7:0]) );

endmodule
module subbytes_tb;

parameter CLK_PERIOD = 10; // Clock period in ns

reg [127:0] data;


wire [127:0] sb;
subbytes dut (.data(data), .sb(sb));
reg clk = 0;
always #((CLK_PERIOD/2)) clk = ~clk;
initial begin
data = 128'h00000000000000000000000000000000;
#10; // Wait for a few clock cycles
data = 128'h0123456789ABCDEF0123456789ABCDEF;
#10; // Wait for a few clock cycles
$display("Input: %h", data);
$display("Output: %h", sb);
$finish;
end
endmodule.

module rounndlast(clk,rc,rin,keylastin,fout);
input clk;
input [3:0]rc;
input [127:0]rin;
input [127:0]keylastin;
output [127:0]fout;
wire [127:0] sb,sr,mcl,keyout;
KeyGeneration t0(rc,keylastin,keyout);
subbytes t1(rin,sb);
shiftrow t2(sb,sr);
assign fout= keyout^sr;
endmodule
module rounndlast_tb;
parameter CLK_PERIOD = 10; // Clock period in ns

reg clk;
reg [3:0] rc;
reg [127:0] rin;
reg [127:0] keylastin;
wire [127:0] fout;
rounndlast dut (.clk(clk), .rc(rc),.rin(rin), .keylastin(keylastin),.fout(fout));
initial begin
clk = 0;
forever #((CLK_PERIOD/2)) clk = ~clk;
end
initial begin
rc = 4'b0000; // Example: Setting rc to 0
rin = 128'h09a2f07b66b1fc3b8b9ae6307865c489; // Example: Setting rin to all zeros
keylastin = 128'h28fddef86da4244accc0a4fe3b316f26; // Example: Setting keylastin to all
zeros
#100; // Wait for a few clock cycles
$display("rc: %h", rc);
$display("rin: %h", rin);
$display("keylastin: %h", keylastin);
$display("fout: %h", fout);
$finish;
end
endmodule

module rounds(clk,rc,data,keyin,keyout,rndout);
input clk;
input [3:0]rc;
input [127:0]data;
input [127:0]keyin;
output [127:0]keyout;
output [127:0]rndout;
wire [127:0] sb,sr,mcl;
KeyGeneration t0(rc,keyin,keyout);
subbytes t1(data,sb);
shiftrow t2(sb,sr);
mixcolumn t3(sr,mcl);
assign rndout= keyout^mcl;
endmodule.

module rounds_tb;
parameter CLK_PERIOD = 10; // Clock period in ns
reg clk;
reg [3:0] rc;
reg [127:0] data;
reg [127:0] keyin;
wire [127:0] keyout;
wire [127:0] rndout;
rounds dut (.clk(clk),.rc(rc), .data(data),.keyin(keyin),keyout(keyout),.rndout(rndout));
initial begin
clk = 0;
forever #((CLK_PERIOD/2)) clk = ~clk;
end
initial begin
rc = 4'b0000; // Example: Setting rc to 0
data = 128'h00000000000000000000000000000000; // Example: Setting data to all zeros
keyin = 128'h00000000000000000000000000000000; // Example: Setting keyin to all zeros
#100; // Wait for a few clock cycles
$display("rc: %h", rc);
$display("data: %h", data);
$display("keyin: %h", keyin);
$display("keyout: %h", keyout);
$display("rndout: %h", rndout);
$finish;
end
endmodule.

module KeyGeneration(rc,key,keyout);
input [3:0] rc;
input [127:0]key;
output [127:0] keyout;
wire [31:0] w0,w1,w2,w3,tem;
assign w0 = key[127:96];
assign w1 = key[95:64];
assign w2 = key[63:32];
assign w3 = key[31:0];
assign keyout[127:96]= w0 ^ tem ^ rcon(rc);
assign keyout[95:64] = w0 ^ tem ^ rcon(rc)^ w1;
assign keyout[63:32] = w0 ^ tem ^ rcon(rc)^ w1 ^ w2;
assign keyout[31:0] = w0 ^ tem ^ rcon(rc)^ w1 ^ w2 ^ w3;
sbox a1(.a(w3[23:16]),.c(tem[31:24]));
sbox a2(.a(w3[15:8]),.c(tem[23:16]));
sbox a3(.a(w3[7:0]),.c(tem[15:8]));
sbox a4(.a(w3[31:24]),.c(tem[7:0]));
function [31:0] rcon;
input [3:0] rc;
case(rc)
4'h0: rcon=32'h01_00_00_00;
4'h1: rcon=32'h02_00_00_00;
4'h2: rcon=32'h04_00_00_00;
4'h3: rcon=32'h08_00_00_00;
4'h4: rcon=32'h10_00_00_00;
4'h5: rcon=32'h20_00_00_00;
4'h6: rcon=32'h40_00_00_00;
4'h7: rcon=32'h80_00_00_00;
4'h8: rcon=32'h1b_00_00_00;
4'h9: rcon=32'h36_00_00_00;
default: rcon=32'h00_00_00_00;
endcase
endfunction
endmodule.
module KeyGeneration_tb;
parameter CLK_PERIOD = 10; // Clock period in ns
reg [3:0] rc;
reg [127:0] key;
wire [127:0] keyout;
KeyGeneration dut ( .rc(rc),.key(key),.keyout(keyout));
reg clk = 0;
always #((CLK_PERIOD/2)) clk = ~clk;
initial begin
rc = 4'b0000; // Example: Setting rc to 0
key = 128'h5468617473206d79204b756e67204675; // Example: Setting key to all zeros
#10; // Wait for a few clock cycles
$display("rc: %h", rc);
$display("key: %h", key);
$display("keyout: %h", keyout);
$finish;
end
endmodule.

module shiftrow_tb;
parameter CLK_PERIOD = 10; // Clock period in ns
reg [127:0] sb;
wire [127:0] sr;
shiftrow dut (.sb(sb), .sr(sr));
reg clk = 0;
always #((CLK_PERIOD/2)) clk = ~clk;
initial begin
sb = 128'h0123456789ABCDEF0123456789ABCDEF; // Example input value
#10; // Wait for a few clock cycles
$display("Input: %h", sb);
#10;
$display("Output: %h", sr);
$finish;
end
endmodule.
module mixcolumn(a,mcl);
input [127:0] a;
output [127:0] mcl;
assign mcl[127:120]= mixcolumn32 (a[127:120],a[119:112],a[111:104],a[103:96]);
assign mcl[119:112]= mixcolumn32 (a[119:112],a[111:104],a[103:96],a[127:120]);
assign mcl[111:104]= mixcolumn32 (a[111:104],a[103:96],a[127:120],a[119:112]);
assign mcl[103:96]= mixcolumn32 (a[103:96],a[127:120],a[119:112],a[111:104]);
assign mcl[95:88]= mixcolumn32 (a[95:88],a[87:80],a[79:72],a[71:64]);
assign mcl[87:80]= mixcolumn32 (a[87:80],a[79:72],a[71:64],a[95:88]);
assign mcl[79:72]= mixcolumn32 (a[79:72],a[71:64],a[95:88],a[87:80]);
assign mcl[71:64]= mixcolumn32 (a[71:64],a[95:88],a[87:80],a[79:72]);
assign mcl[63:56]= mixcolumn32 (a[63:56],a[55:48],a[47:40],a[39:32]);
assign mcl[55:48]= mixcolumn32 (a[55:48],a[47:40],a[39:32],a[63:56]);
assign mcl[47:40]= mixcolumn32 (a[47:40],a[39:32],a[63:56],a[55:48]);
assign mcl[39:32]= mixcolumn32 (a[39:32],a[63:56],a[55:48],a[47:40]);
assign mcl[31:24]= mixcolumn32 (a[31:24],a[23:16],a[15:8],a[7:0]);
assign mcl[23:16]= mixcolumn32 (a[23:16],a[15:8],a[7:0],a[31:24]);
assign mcl[15:8]= mixcolumn32 (a[15:8],a[7:0],a[31:24],a[23:16]);
assign mcl[7:0]= mixcolumn32 (a[7:0],a[31:24],a[23:16],a[15:8]);
function [7:0] mixcolumn32;
input [7:0] i1,i2,i3,i4;
begin
mixcolumn32[7]=i1[6] ^ i2[6] ^ i2[7] ^ i3[7] ^ i4[7];
mixcolumn32[6]=i1[5] ^ i2[5] ^ i2[6] ^ i3[6] ^ i4[6];
mixcolumn32[5]=i1[4] ^ i2[4] ^ i2[5] ^ i3[5] ^ i4[5];
mixcolumn32[4]=i1[3] ^ i1[7] ^ i2[3] ^ i2[4] ^ i2[7] ^ i3[4] ^ i4[4];
mixcolumn32[3]=i1[2] ^ i1[7] ^ i2[2] ^ i2[3] ^ i2[7] ^ i3[3] ^ i4[3];
mixcolumn32[2]=i1[1] ^ i2[1] ^ i2[2] ^ i3[2] ^ i4[2];
mixcolumn32[1]=i1[0] ^ i1[7] ^ i2[0] ^ i2[1] ^ i2[7] ^ i3[1] ^ i4[1];
mixcolumn32[0]=i1[7] ^ i2[7] ^ i2[0] ^ i3[0] ^ i4[0];
end
endfunction
endmodule.
module mixcolumn_tb;
parameter CLK_PERIOD = 10; // Clock period in ns
reg [127:0] a;
wire [127:0] mcl;
mixcolumn dut ( .a(a),.mcl(mcl));
reg clk = 0;
always #((CLK_PERIOD/2)) clk = ~clk;
initial begin
a = 128'h0123456789ABCDEF0123456789ABCDEF; // Example input value
#10; // Wait for a few clock cycles
$display("Input: %h", a);
#10;
$display("Output: %h", mcl);
$finish;
end
endmodule
module sbox(a,c);
input [7:0] a;
output [7:0] c;
reg [7:0] c;
always @(a)
case (a)
8'h00: c=8'h63;
8'h01: c=8'h7c;
8'h02: c=8'h77;
8'h03: c=8'h7b;
8'h04: c=8'hf2;
8'h05: c=8'h6b;
8'h06: c=8'h6f;
8'h07: c=8'hc5;
8'h08: c=8'h30;
8'h09: c=8'h01;
8'h0a: c=8'h67;
8'h0b: c=8'h2b;
8'h0c: c=8'hfe;
8'h0d: c=8'hd7;
8'h0e: c=8'hab;
8'h0f: c=8'h76;
8'h10: c=8'hca;
8'h11: c=8'h82;
8'h12: c=8'hc9;
8'h13: c=8'h7d;
8'h14: c=8'hfa;
8'h15: c=8'h59;
8'h16: c=8'h47;
8'h17: c=8'hf0;
8'h18: c=8'had;
8'h19: c=8'hd4;
8'h1a: c=8'ha2;
8'h1b: c=8'haf;
8'h1c: c=8'h9c;
8'h1d: c=8'ha4;
8'h1e: c=8'h72;
8'h1f: c=8'hc0;
8'h20: c=8'hb7;
8'h21: c=8'hfd;
8'h22: c=8'h93;
8'h23: c=8'h26;
8'h24: c=8'h36;
8'h25: c=8'h3f;
8'h26: c=8'hf7;
8'h27: c=8'hcc;
8'h28: c=8'h34;
8'h29: c=8'ha5;
8'h2a: c=8'he5;
8'h2b: c=8'hf1;
8'h2c: c=8'h71;
8'h2d: c=8'hd8;
8'h2e: c=8'h31;
8'h2f: c=8'h15;
8'h30: c=8'h04;
8'h31: c=8'hc7;
8'h32: c=8'h23;
8'h33: c=8'hc3;
8'h34: c=8'h18;
8'h35: c=8'h96;
8'h36: c=8'h05;
8'h37: c=8'h9a;
8'h38: c=8'h07;
8'h39: c=8'h12;
8'h3a: c=8'h80;
8'h3b: c=8'he2;
8'h3c: c=8'heb;
8'h3d: c=8'h27;
8'h3e: c=8'hb2;
8'h3f: c=8'h75;
8'h40: c=8'h09;
8'h41: c=8'h83;
8'h42: c=8'h2c;
8'h43: c=8'h1a;
8'h44: c=8'h1b;
8'h45: c=8'h6e;
8'h46: c=8'h5a;
8'h47: c=8'ha0;
8'h48: c=8'h52;
8'h49: c=8'h3b;
8'h4a: c=8'hd6;
8'h4b: c=8'hb3;
8'h4c: c=8'h29;
8'h4d: c=8'he3;
8'h4e: c=8'h2f;
8'h4f: c=8'h84;
8'h50: c=8'h53;
8'h51: c=8'hd1;
8'h52: c=8'h00;
8'h53: c=8'hed;
8'h54: c=8'h20;
8'h55: c=8'hfc;
8'h56: c=8'hb1;
8'h57: c=8'h5b;
8'h58: c=8'h6a;
8'h59: c=8'hcb;
8'h5a: c=8'hbe;
8'h5b: c=8'h39;
8'h5c: c=8'h4a;
8'h5d: c=8'h4c;
8'h5e: c=8'h58;
8'h5f: c=8'hcf;
8'h60: c=8'hd0;
8'h61: c=8'hef;
8'h62: c=8'haa;
8'h63: c=8'hfb;
8'h64: c=8'h43;
8'h65: c=8'h4d;
8'h66: c=8'h33;
8'h67: c=8'h85;
8'h68: c=8'h45;
8'h69: c=8'hf9;
8'h6a: c=8'h02;
8'h6b: c=8'h7f;
8'h6c: c=8'h50;
8'h6d: c=8'h3c;
8'h6e: c=8'h9f;
8'h6f: c=8'ha8;
8'h70: c=8'h51;
8'h71: c=8'ha3;
8'h72: c=8'h40;
8'h73: c=8'h8f;
8'h74: c=8'h92;
8'h75: c=8'h9d;
8'h76: c=8'h38;
8'h77: c=8'hf5;
8'h78: c=8'hbc;
8'h79: c=8'hb6;
8'h7a: c=8'hda;
8'h7b: c=8'h21;
8'h7c: c=8'h10;
8'h7d: c=8'hff;
8'h7e: c=8'hf3;
8'h7f: c=8'hd2;
8'h80: c=8'hcd;
8'h81: c=8'h0c;
8'h82: c=8'h13;
8'h83: c=8'hec;
8'h84: c=8'h5f;
8'h85: c=8'h97;
8'h86: c=8'h44;
8'h87: c=8'h17;
8'h88: c=8'hc4;
8'h89: c=8'ha7;
8'h8a: c=8'h7e;
8'h8b: c=8'h3d;
8'h8c: c=8'h64;
8'h8d: c=8'h5d;
8'h8e: c=8'h19;
8'h8f: c=8'h73;
8'h90: c=8'h60;
8'h91: c=8'h81;
8'h92: c=8'h4f;
8'h93: c=8'hdc;
8'h94: c=8'h22;
8'h95: c=8'h2a;
8'h96: c=8'h90;
8'h97: c=8'h88;
8'h98: c=8'h46;
8'h99: c=8'hee;
8'h9a: c=8'hb8;
8'h9b: c=8'h14;
8'h9c: c=8'hde;
8'h9d: c=8'h5e;
8'h9e: c=8'h0b;
8'h9f: c=8'hdb;
8'ha0: c=8'he0;
8'ha1: c=8'h32;
8'ha2: c=8'h3a;
8'ha3: c=8'h0a;
8'ha4: c=8'h49;
8'ha5: c=8'h06;
8'ha6: c=8'h24;
8'ha7: c=8'h5c;
8'ha8: c=8'hc2;
8'ha9: c=8'hd3;
8'haa: c=8'hac;
8'hab: c=8'h62;
8'hac: c=8'h91;
8'had: c=8'h95;
8'hae: c=8'he4;
8'haf: c=8'h79;
8'hb0: c=8'he7;
8'hb1: c=8'hc8;
8'hd5: c=8'h03;
8'hd6: c=8'hf6;
8'hd7: c=8'h0e;
8'hd8: c=8'h61;
8'hd9: c=8'h35;
8'hda: c=8'h57;
8'hdb: c=8'hb9;
8'hdc: c=8'h86;
8'hdd: c=8'hc1;
8'hde: c=8'h1d;
8'hdf: c=8'h9e;
8'he0: c=8'he1;
8'he1: c=8'hf8;
8'he2: c=8'h98;
8'he3: c=8'h11;
8'he4: c=8'h69;
8'he5: c=8'hd9;
8'he6: c=8'h8e;
8'he7: c=8'h94;
8'he8: c=8'h9b;
8'he9: c=8'h1e;
8'hea: c=8'h87;
8'heb: c=8'he9;
8'hec: c=8'hce;
8'hed: c=8'h55;
8'hee: c=8'h28;
8'hef: c=8'hdf;
8'hf0: c=8'h8c;
8'hf1: c=8'ha1;
8'hf2: c=8'h89;
8'hf3: c=8'h0d;
8'hf4: c=8'hbf;
8'hf5: c=8'he6;
8'hf6: c=8'h42;
8'hf7: c=8'h68;
8'hf8: c=8'h41;
8'hf9: c=8'h99;
8'hfa: c=8'h2d;
8'hfb: c=8'h0f;
8'hfc: c=8'hb0;
8'hfd: c=8'h54;
8'hfe: c=8'hbb;
8'hff: c=8'h16;
endcase

8'hb2: c=8'h37;
8'hb3: c=8'h6d;
8'hb4: c=8'h8d;
8'hb5: c=8'hd5;
8'hb6: c=8'h4e;
8'hb7: c=8'ha9;
8'hb8: c=8'h6c;
8'hb9: c=8'h56;
8'hba: c=8'hf4;
8'hbb: c=8'hea;
8'hbc: c=8'h65;
8'hbd: c=8'h7a;
8'hbe: c=8'hae;
8'hbf: c=8'h08;
8'hc0: c=8'hba;
8'hc1: c=8'h78;
8'hc2: c=8'h25;
8'hc3: c=8'h2e;
8'hc4: c=8'h1c;
8'hc5: c=8'ha6;
8'hc6: c=8'hb4;
8'hc7: c=8'hc6;
8'hc8: c=8'he8;
8'hc9: c=8'hdd;
8'hca: c=8'h74;
8'hcb: c=8'h1f;
8'hcc: c=8'h4b;
8'hcd: c=8'hbd;
8'hce: c=8'h8b;
8'hcf: c=8'h8a;
8'hd0: c=8'h70;
8'hd1: c=8'h3e;
8'hd2: c=8'hb5;
8'hd3: c=8'h66;
8'hd4: c=8'h48;
8'hd5: c=8'h03;
8'hd6: c=8'hf6;
8'hd7: c=8'h0e;
8'hd8: c=8'h61;
8'hd9: c=8'h35;
8'hda: c=8'h57;
8'hdb: c=8'hb9;
8'hdc: c=8'h86;
8'hdd: c=8'hc1;
8'hde: c=8'h1d;
8'hdf: c=8'h9e;
8'he0: c=8'he1;
8'he1: c=8'hf8;
8'he2: c=8'h98;
8'he3: c=8'h11;
8'he4: c=8'h69;
8'he5: c=8'hd9;
8'he6: c=8'h8e;
8'he7: c=8'h94;
8'he8: c=8'h9b;
8'he9: c=8'h1e;
8'hea: c=8'h87;
8'heb: c=8'he9;
8'hec: c=8'hce;
8'hed: c=8'h55;
8'hee: c=8'h28;
8'hef: c=8'hdf;
8'hf0: c=8'h8c;
8'hf1: c=8'ha1;
8'hf2: c=8'h89;
8'hf3: c=8'h0d;
8'hf4: c=8'hbf;
8'hf5: c=8'he6;
8'hf6: c=8'h42;
8'hf7: c=8'h68;
8'hf8: c=8'h41;
8'hf9: c=8'h99;
8'hfa: c=8'h2d;
8'hfb: c=8'h0f;
8'hfc: c=8'hb0;
8'hfd: c=8'h54;
8'hfe: c=8'hbb;
8'hff: c=8'h16;
endcase
endmodule
module sbox_tb;
parameter CLK_PERIOD = 10; // Clock period in ns
reg [7:0] a;
reg i;
wire [7:0] c;
sbox dut (.a(a),.c(c));
reg clk = 0;
always #((CLK_PERIOD/2)) clk = ~clk;
reg [7:0] test_values [0:255];
initial begin
for (i = 0; i < 256; i = i + 1) begin
test_values[i] = 8'h00;
end
end
initial begin
for (i = 0; i < 256; i = i + 1) begin
a = test_values[i];
#10; // Wait for a few clock cycles
$display("Input: %h", a);
$display("Output: %h", c);
end
$finish;
end
endmodule.

REPORTS:
Synthesis report:

Area report
Power report

Timing report
5.2 AES Decryption
Source code:
module AES(enable, e128, d128);
output wire e128;
output wire d128;
input enable;
wire[127:0] in = 128'h29c3505f571420f6402299b31a02d73a;
wire[127:0] key128 = 128'h5468617473206d79204b756e67204675;
wire[127:0] encrypted128;
wire[127:0] decrypted128;
AES_Decrypt a2(encrypted128,key128,decrypted128);
assign d128 = (decrypted128 == in && enable) ? 1'b1 : 1'b0;
endmodule

module AES_tb();
wire e128;
wire d128;
reg enable;
AES a(enable,e128,d128);
initial begin
$monitor("Encrypt128 = %b,Decrypt128 = %b",e128,d128);
enable = 1;
#10;
enable = 0;
#10;
enable = 1;
#10;
end
endmodule

module inverseSubBytes(in,out);
input [127:0] in;
output [127:0] out;
genvar i;
generate
for(i=0;i<128;i=i+8) begin :sub_Bytes
inverseSbox s(in[i +:8],out[i +:8]);
end
endgenerate
endmodule

module inverseSubBytes_tb;
reg [127:0] in;
wire [127:0]out;
inverseSubBytes isb(in,out);
initial begin
$monitor("input= %h ,output= %h",in,out);
in=128'h29c3505f571420f6402299b31a02d73a;
#10;
$finish;
end
endmodule

module inverseShiftRows (in, shifted);


input [0:127] in;
output [0:127] shifted;
assign shifted[0+:8] = in[0+:8];
assign shifted[32+:8] = in[32+:8];
assign shifted[64+:8] = in[64+:8];
assign shifted[96+:8] = in[96+:8];
assign shifted[8+:8] = in[104+:8];
assign shifted[40+:8] = in[8+:8];
assign shifted[72+:8] = in[40+:8];
assign shifted[104+:8] = in[72+:8];
assign shifted[16+:8] = in[80+:8];
assign shifted[48+:8] = in[112+:8];
assign shifted[80+:8] = in[16+:8];
assign shifted[112+:8] = in[48+:8];
assign shifted[24+:8] = in[56+:8];
assign shifted[56+:8] = in[88+:8];
assign shifted[88+:8] = in[120+:8];
assign shifted[120+:8] = in[24+:8];
endmodule

module inverseShiftRows_tb;
reg [127:0] in;
wire [127:0] out;
inverseShiftRows m (in,out);
initial begin
$monitor("input= %H , output= %h",in,out);
in = 128'h29c3505f571420f6402299b31a02d73a;
#100;
end
endmodule

module addRoundkey(data, out, key);


input [127:0] data;
input [127:0] key;
output [127:0] out;
assign out = key ^ data;
endmodule

module addRoundkey_tb;
reg [127:0] in;
reg [127:0] key;
wire [127:0] out;
addRoundKey m (in, out, key);
initial begin
$monitor("input= %H, output= %h, key = %h", in, out, key);
in = 128'h29c3505f571420f6402299b31a02d73a;
key = 128'h5468617473206d79204b756e67204675;
#100;
end
endmodule

module inverseMixColumns(state_in,state_out);
input [127:0] state_in;
output [127:0] state_out;
function[7:0] multiply(input [7:0]x,input integer n);
integer i;
begin
for(i=0;i<n;i=i+1)begin
if(x[7] == 1) x = ((x << 1) ^ 8'h1b);
else x = x << 1;
end
multiply=x;
end

endfunction
function [7:0] mb0e; //multiply by {0e}
input [7:0] x;
begin
mb0e=multiply(x,3) ^ multiply(x,2)^ multiply(x,1);
end
endfunction
function [7:0] mb0d; //multiply by {0d}
input [7:0] x;
begin
mb0d=multiply(x,3) ^ multiply(x,2)^ x;
end
endfunction
function [7:0] mb0b; //multiply by {0b}
input [7:0] x;
begin
mb0b=multiply(x,3) ^ multiply(x,1)^ x;
end
endfunction
function [7:0] mb09; //multiply by {09}
input [7:0] x;
begin
mb09=multiply(x,3) ^ x;
end
endfunction
genvar i;

generate
for(i=0;i< 4;i=i+1) begin : m_col

assign state_out[(i*32 + 24)+:8]= mb0e(state_in[(i*32 + 24)+:8]) ^


mb0b(state_in[(i*32 + 16)+:8]) ^ mb0d(state_in[(i*32 + 8)+:8]) ^ mb09(state_in[i*32+:8]);
assign state_out[(i*32 + 16)+:8]= mb09(state_in[(i*32 + 24)+:8]) ^
mb0e(state_in[(i*32 + 16)+:8]) ^ mb0b(state_in[(i*32 + 8)+:8]) ^ mb0d(state_in[i*32+:8]);
assign state_out[(i*32 + 8)+:8]= mb0d(state_in[(i*32 + 24)+:8]) ^
mb09(state_in[(i*32 + 16)+:8]) ^ mb0e(state_in[(i*32 + 8)+:8]) ^ mb0b(state_in[i*32+:8]);
assign state_out[i*32+:8]= mb0b(state_in[(i*32 + 24)+:8]) ^ mb0d(state_in[(i*32 +
16)+:8]) ^ mb09(state_in[(i*32 + 8)+:8]) ^ mb0e(state_in[i*32+:8]);

end

endgenerate

endmodule

module inverseMixColumns_tb;
reg [0:127] in;
wire [0:127] out;
inverseMixColumns m (in,out);
initial begin
$monitor("input= %H , output= %h",in,out);
in= 128'h29c3505f571420f6402299b31a02d73a;
#10;
end
endmodule.

module AES_Decrypt#(parameter N=128,parameter Nr=10,parameter Nk=4)


(in,key,out);
input [127:0] in;
input [N-1:0] key;
output [127:0] out;
wire [(128*(Nr+1))-1 :0] fullkeys;
wire [127:0] states [Nr+1:0] ;
wire [127:0] afterSubBytes;
wire [127:0] afterShiftRows;
keyExpansion #(Nk,Nr) ke (key,fullkeys);
addRoundKey addrk1 (in,states[0],fullkeys[127:0]);
genvar i;
generate
for(i=1; i<Nr ;i=i+1)begin : loop
decryptRound dr(states[i-1],fullkeys[i*128+:128],states[i]);
end
inverseShiftRows sr(states[Nr-1],afterShiftRows);
inverseSubBytes sb(afterShiftRows,afterSubBytes);
addRoundKey addrk2(afterSubBytes,states[Nr],fullkeys[((128*(Nr+1))-
1)-:128]);
assign out=states[Nr];
endgenerate
endmodule

module AES_Decrypt_tb;
reg [127:0] in1;
wire [127:0] out1;
reg [127:0] key1;

AES_Decrypt a(in1,key1,out1);

initial begin
$monitor("in128= %h, key128= %h ,out128= %h",in1,key1,out1);
in1=128'h29c3505f571420f6402299b31a02d73a;
key1=128'h5468617473206d79204b756e67204675;
#10;
end

endmodule.

Reports:
5.3.Implementation of Text and Image Encryption and
Decryption using Python code:

Python 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.

= RESTART: C:\Users\Monisha Amruthraj\AppData\Local\Programs\Python\Python311\


p2.py
Encrypted: b'\xdb\xee)\xe4\xebR(B*{Z\x84\xd9\xbdY\xf0'
Decrypted: secret message

from Crypto.Cipher import AES


from Crypto.Random import get_random_bytes
from Crypto.Util.Padding import pad, unpad

def encrypt_message(message, key):


cipher = AES.new(key, AES.MODE_CFB)
ct_bytes = cipher.encrypt(message.encode())
iv = cipher.iv
return ct_bytes, iv

def decrypt_message(ct_bytes, iv, key):


cipher = AES.new(key, AES.MODE_CFB, iv=iv)
pt_bytes = unpad(cipher.decrypt(ct_bytes), AES.block_size)
return pt_bytes.decode()

def main():
# Generate a random 128-bit (16-byte) key
key = get_random_bytes(16)

# Message to be encrypted
message = "secret message"
# Encryption
ciphertext, iv = encrypt_message(message, key)
print("Encrypted:", ciphertext.hex())

# Decryption
decrypted_message = decrypt_message(ciphertext, iv, key)
print("Decrypted:", decrypted_message)

if __name__ == "__main__":
main()

Certainly! Let’s break down the provided Python code for AES (Advanced Encryption
Standard) encryption and decryption using the CBC (Cipher Block Chaining) mode.

AES Overview:
AES is a symmetric encryption algorithm that operates on fixed-size blocks of data (usually
128 bits or 16 bytes).
It uses a secret key to encrypt and decrypt data.
CBC mode is more secure than ECB (Electronic Code Book) mode because it introduces
diffusion (spreading out the influence of one plaintext block over multiple ciphertext blocks).
Code Explanation:
The code imports necessary modules from the Crypto package.
It defines three functions: encrypt_message, decrypt_message, and main.
encrypt_message Function:
Takes two arguments: message (the data to be encrypted) and key (the secret key).
Creates an AES cipher object in CBC mode with a random initialization vector (IV).
Pads the plaintext message to a multiple of the block size (16 bytes) using PKCS7 padding.
Encrypts the padded plaintext using the AES cipher.
Returns the ciphertext and the IV.
decrypt_message Function:
Takes three arguments: ct_bytes (the encrypted data), iv (the initialization vector), and key.
Creates an AES cipher object with the same key and IV in CBC mode.
Decrypts the ciphertext using the AES cipher.
Removes the padding to obtain the original plaintext.
Returns the decrypted data.
main Function:
Generates a random 128-bit key (key) using get_random_bytes(16).
Sets the plaintext message to "secret message".
Calls the encrypt_message function to encrypt the message.
Calls the decrypt_message function to decrypt the ciphertext back to the original plaintext.
Prints the results.
Important Notes:
Initialization Vector (IV): The IV is crucial for CBC mode. It ensures that identical plaintext
blocks result in different ciphertext blocks.
Padding: PKCS7 padding ensures that the plaintext is a multiple of the block size.
Security Considerations: While CBC mode is more secure than ECB, it still has
vulnerabilities (such as padding oracle attacks). For stronger security, consider using
authenticated encryption modes like GCM (Galois/Counter Mode).
Remember to handle keys and IVs securely, and choose the appropriate mode based on your

use case. If you have any further questions, feel free to ask! 👍

The Cipher Block Chaining (CBC) mode is a typical block cipher mode of operation using
block cipher algorithm. In this version, we provide Data Encryption Standard (DES) and
Advanced Encryption Standard (AES) processing ability, the cipherkey length for DES
should be 64 bits, and 128/192/256 bits for AES.

IMAGE PROCESSING:

#!/usr/bin/env python
# ----------------- Header Files ---------------------#

from __future__ import division, print_function, unicode_literals

import sys
import random
import argparse
import logging
from tkinter import * # Changed from Tkinter to tkinter for Python 3
from tkinter import filedialog # Changed from tkFileDialog to filedialog for
Python 3
from tkinter import messagebox # Changed from tkMessageBox to messagebox for
Python 3
import os
from PIL import Image
import math
from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad
from tkinter import Tk, Button, LEFT, RIGHT, Entry, Label, Message, Canvas
import hashlib
import binascii
import numpy as np

global password

def load_image(name):
return Image.open(name)

# ----------------- Functions for encryption ---------------------#


def prepare_message_image(image, size):
if size != image.size:
image = image.resize(size, Image.ANTIALIAS)
return image

def generate_secret(size, secret_image=None):


width, height = size
new_secret_image = Image.new(mode="RGB", size=(width * 2, height * 2))

for x in range(0, 2 * width, 2):


for y in range(0, 2 * height, 2):
color1 = np.random.randint(255)
color2 = np.random.randint(255)
color3 = np.random.randint(255)
new_secret_image.putpixel((x, y), (color1, color2, color3))
new_secret_image.putpixel((x + 1, y), (255 - color1, 255 - color2,
255 - color3))
new_secret_image.putpixel((x, y + 1), (255 - color1, 255 - color2,
255 - color3))
new_secret_image.putpixel((x + 1, y + 1), (color1, color2,
color3))

return new_secret_image

def generate_ciphered_image(secret_image, prepared_image):


width, height = prepared_image.size
ciphered_image = Image.new(mode="RGB", size=(width * 2, height * 2))
for x in range(0, width * 2, 2):
for y in range(0, height * 2, 2):
sec = secret_image.getpixel((x, y))
msssg = prepared_image.getpixel((int(x / 2), int(y / 2)))
color1 = (msssg[0] + sec[0]) % 256
color2 = (msssg[1] + sec[1]) % 256
color3 = (msssg[2] + sec[2]) % 256
ciphered_image.putpixel((x, y), (color1, color2, color3))
ciphered_image.putpixel((x + 1, y), (255 - color1, 255 - color2,
255 - color3))
ciphered_image.putpixel((x, y + 1), (255 - color1, 255 - color2,
255 - color3))
ciphered_image.putpixel((x + 1, y + 1), (color1, color2, color3))

return ciphered_image

def generate_image_back(secret_image, ciphered_image):


width, height = secret_image.size
new_image = Image.new(mode="RGB", size=(int(width / 2), int(height / 2)))
for x in range(0, width, 2):
for y in range(0, height, 2):
sec = secret_image.getpixel((x, y))
cip = ciphered_image.getpixel((x, y))
color1 = (cip[0] - sec[0]) % 256
color2 = (cip[1] - sec[1]) % 256
color3 = (cip[2] - sec[2]) % 256
new_image.putpixel((int(x / 2), int(y / 2)), (color1, color2,
color3))

return new_image

#------------------------Encryption -------------------#
def level_one_encrypt(Imagename):
message_image = load_image(Imagename)
size = message_image.size
width, height = size

secret_image = generate_secret(size)
secret_image.save("secret.jpeg")

prepared_image = prepare_message_image(message_image, size)


ciphered_image = generate_ciphered_image(secret_image, prepared_image)
ciphered_image.save("2-share_encrypt.jpeg")

# -------------------- Construct Encrypted Image ----------------#


def construct_enc_image(ciphertext, relength, width, height):
asciicipher = binascii.hexlify(ciphertext)

def replace_all(text, dic):


for i, j in dic.items():
text = text.replace(i.encode(), j.encode())
return text

# use replace function to replace ascii cipher characters with numbers


reps = {'a': '1', 'b': '2', 'c': '3', 'd': '4', 'e': '5', 'f': '6', 'g':
'7', 'h': '8', 'i': '9', 'j': '10',
'k': '11', 'l': '12', 'm': '13', 'n': '14', 'o': '15', 'p': '16',
'q': '17', 'r': '18', 's': '19',
't': '20', 'u': '21', 'v': '22', 'w': '23', 'x': '24', 'y': '25',
'z': '26'}
asciiciphertxt = replace_all(asciicipher, reps)

# construct encrypted image


step = 3
encimageone = [asciiciphertxt[i:i + step] for i in range(0,
len(asciiciphertxt), step)]
# if the last pixel RGB value is less than 3-digits, add a digit a 1
if int(encimageone[len(encimageone) - 1]) < 100:
encimageone[len(encimageone) - 1] += b"1"
# check to see if we can divide the string into partitions of 3 digits.
if not, fill in with some garbage RGB values
if len(encimageone) % 3 != 0:
while (len(encimageone) % 3 != 0):
encimageone.append("101")

encimagetwo = [(int(encimageone[int(i)]), int(encimageone[int(i + 1)]),


int(encimageone[int(i + 2)])) for i in
range(0, len(encimageone), step)]
print(len(encimagetwo))
while (int(relength) != len(encimagetwo)):
encimagetwo.pop()

encim = Image.new("RGB", (int(width), int(height)))


encim.putdata(encimagetwo)
encim.save("visual_encrypt.jpeg")

#------------------------- Visual-encryption -------------------------#


def encrypt(imagename, password):
plaintext = list()
plaintextstr = b""

im = Image.open(imagename)
pix = im.load()
width = im.size[0]
height = im.size[1]

# break up the image into a list, each with pixel values and then append
to a string
for y in range(0, height):
for x in range(0, width):
print(pix[x, y])
plaintext.append(pix[x, y])
print(width)
print(height)

# add 100 to each tuple value to make sure each are 3 digits long.
for i in range(0, len(plaintext)):
for j in range(0, 3):
aa = int(plaintext[i][j]) + 100
plaintextstr += str(aa).encode()

# length save for encrypted image reconstruction


relength = len(plaintext)

# append dimensions of image for reconstruction after decryption


plaintextstr += b"h" + str(height).encode() + b"h" + b"w" +
str(width).encode() + b"w"

# make sure that plantextstr length is a multiple of 16 for AES. if not,


append "n".
while (len(plaintextstr) % 16 != 0):
plaintextstr += b"n"

# encrypt plaintext
obj = AES.new(password, AES.MODE_CBC, b'This is an IV456')
ciphertext = obj.encrypt(plaintextstr)

# write ciphertext to file for analysis


cipher_name = imagename + ".crypt"
with open(cipher_name, 'wb') as g: # Changed mode to 'wb' for writing
bytes
g.write(ciphertext)
construct_enc_image(ciphertext, relength, width, height)
print("Visual Encryption done.......")
level_one_encrypt("visual_encrypt.jpeg")
print("2-Share Encryption done.......")

# ---------------------- decryption ---------------------- #


def decrypt(ciphername, password):
# Open the secret image and the 2-share encrypted image
secret_image = Image.open("secret.jpeg")
ima = Image.open("2-share_encrypt.jpeg")
new_image = generate_image_back(secret_image, ima)
new_image.save("2-share_decrypt.jpeg")
print("2-share Decryption done....")

try:
# Open the encrypted file in binary mode
with open(ciphername, 'rb') as cipher_file:
ciphertext = cipher_file.read()

# Decrypt ciphertext with password


obj2 = AES.new(password, AES.MODE_CBC, b'This is an IV456')
decrypted = obj2.decrypt(ciphertext)

# Parse the decrypted text back into bytes


decrypted = decrypted.decode().replace("n", "").encode()

# Extract dimensions of images


newwidth = decrypted.split(b"w")[1].decode()
newheight = decrypted.split(b"h")[1].decode()

# Replace height and width indicators with empty space in decrypted


plaintext
heightr = b"h" + newheight.encode() + b"h"
widthr = b"w" + newwidth.encode() + b"w"
decrypted = decrypted.replace(heightr, b"")
decrypted = decrypted.replace(widthr, b"")

# Reconstruct the list of RGB tuples from the decrypted plaintext


step = 3
finaltextone = [decrypted[i:i + step] for i in range(0,
len(decrypted), step)]
finaltexttwo = [(int(finaltextone[i]) - 100, int(finaltextone[i + 1])
- 100, int(finaltextone[i + 2]) - 100) for i in range(0, len(finaltextone),
step)]

# Reconstruct image from list of pixel RGB tuples


newim = Image.new("RGB", (int(newwidth), int(newheight)))
newim.putdata(finaltexttwo)
newim.save("visual_decrypt.jpeg")
print("Visual Decryption done......")

except Exception as e:
print("Error during decryption:", e)

# GUI stuff
def pass_alert():
messagebox.showinfo("Password Alert", "Please enter a password.")

def image_open():
global file_path_e
enc_pass = passg.get()
if enc_pass == "":
pass_alert()
else:
password = hashlib.sha256(enc_pass.encode()).digest()
filename = filedialog.askopenfilename()
file_path_e = os.path.dirname(filename)
encrypt(filename, password)

def cipher_open():
global file_path_d
dec_pass = passg.get()
if dec_pass == "":
pass_alert()
else:
password = hashlib.sha256(dec_pass.encode()).digest()
filename = filedialog.askopenfilename()
file_path_d = os.path.dirname(filename)
decrypt(filename, password)

class App:
def __init__(self, master):
global passg
title = "Image Encryption"
author = "Made by Aditya"
msgtitle = Message(master, text=title)
msgtitle.config(font=('helvetica', 17, 'bold'), width=200)
msgauthor = Message(master, text=author)
msgauthor.config(font=('helvetica', 10), width=200)

canvas_width = 200
canvas_height = 50
w = Canvas(master,
width=canvas_width,
height=canvas_height)
msgtitle.pack()
msgauthor.pack()
w.pack()

passlabel = Label(master, text="Enter Encrypt/Decrypt Password:")


passlabel.pack()
passg = Entry(master, show="*", width=20)
passg.pack()
self.encrypt = Button(master,
text="Encrypt", fg="black",
command=image_open, width=25, height=5)
self.encrypt.pack(side=LEFT)
self.decrypt = Button(master,
text="Decrypt", fg="black",
command=cipher_open, width=25, height=5)
self.decrypt.pack(side=RIGHT)

# Main function
root = Tk()
root.wm_title("Image Encryption")
app = App(root)
root.mainloop()
name__ == "__main__":

RESULTS:
Chapter 6
6.1.CONCLUSION
The proposed work makes use of AES algorithm to encrypt and decrypt the image
and text. It makes use of 128 bit key for encryption which makes AES secure and
faster than DES.As the key size is larger, it helps to overcome several attacks such
as brute force attack and man in the middle attack. In our proposed system,
encryption image doesn’t remain the same. The encryption image is chosen in
random. So, it is difficult for intruder to differentiate the encrypted image and the
original image. So, AES algorithm is most suited for image encryption in real time
applications. As a future work, we are planning for a different encryption keys in
each round to perform encryption.

The cryptographic methodology proposed in this paper will further be tested on


different types of input images with change in size of the image and keys of AES
encryption algorithm. The report shows the study in which a system could be used
for effective image data encryption and key generation in diversified application
areas, where sensitive and confidential data needs to be transmitted along with the
image. The next step in this direction will be system implementation, and then
analyzing it for its efficiency, accuracy and reliability.

The proposed work makes use of AES algorithm to encrypt and decrypt the image
and text. It makes use of 128 bit key for encryption which makes AES secure and
faster than DES.As the key size is larger, it helps to overcome several attacks such
as brute force attack and man in the middle attack. In our proposed system,
encryption image doesn’t remain the same. The encryption image is chosen in
random. So, it is difficult for intruder to differentiate the encrypted image and the
original image. So, AES algorithm is most suited for image encryption in real time
applications. As a future work, we are planning for a different encryption keys in
each round to perform encryption.
6.2FUTURE SCOPE:

As a future work, I am going to continue this research in order generating more


secure key to get the maximum encryption speed in limited implementation area. I
will implement a novel mechanism in which AES algorithm will be apply to
encrypt and decrypt images securely for further applications in image
communication system. Future scope is, it can be used in various applications like
Military communication, Forensics, Intelligent systems etc.

You might also like