0% found this document useful (0 votes)
25 views3 pages

EX - NO: 1C Date:: IT8761 Laboratory

The document describes the Hill cipher algorithm for encrypting and decrypting messages. It defines key matrices for encryption and decryption. It includes methods for encoding and decoding groups of three characters. The main method initializes strings for the original, padded, encrypted, and decrypted messages. It encrypts the padded message in groups of three characters and decrypts the encrypted message to retrieve the original.

Uploaded by

Paper Art
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views3 pages

EX - NO: 1C Date:: IT8761 Laboratory

The document describes the Hill cipher algorithm for encrypting and decrypting messages. It defines key matrices for encryption and decryption. It includes methods for encoding and decoding groups of three characters. The main method initializes strings for the original, padded, encrypted, and decrypted messages. It encrypts the padded message in groups of three characters and decrypts the encrypted message to retrieve the original.

Uploaded by

Paper Art
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

KRITHIK CS 710720205023

EX.NO: 1C
DATE: Hill Cipher

AIM:

ALGORITHM:
1. Create a class named `hillCipher`.
2. Create a array named `keymat` to store the encryption key matrix.
3. Create a array named `invkeymat` to store the inverse of the encryption key matrix.
4. Create a string `key` containing the alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
used for encryption and decryption.
5. Create a static method named `encode` that takes three characters as input (a, b, c) and
returns the encrypted string for these characters:
 Convert the characters a, b, and c to their respective positions in the alphabet

(0 for 'A',for 'B', ..., 25 for 'Z').


 Calculate the encoded positions x, y, and z using the `keymat` matrix.

 Convert x, y, and z back to characters using the `key` string and the modulo operation

(% 26).
 Return the encrypted string as a concatenation of the three characters.

6. Create a static method named `decode` that takes three characters as input (a, b, c) and
returns the decrypted string for these characters:
 Convert the characters a, b, and c to their respective positions in the alphabet.

 Calculate the decoded positions x, y, and z using the `invkeymat` matrix.

 Convert x, y, and z back to characters using the `key` string and the modulo operation

(%26). Handle negative results by adding 26 to the result.


 Return the decrypted string as a concatenation of the three characters.

7. In the `main` method:


 Initialize a string `msg` with the plaintext message.

 Initialize an empty string `enc` for the encrypted message and an empty string `dec` for

the decrypted message.


 Pad the `msg` with 'X' characters to ensure its length is a multiple of 3.

 Convert the `msg` to uppercase and remove any whitespace characters.

 Iterate through the characters of the `msg` in groups of three and encrypt each group

using the `encode` method. Append the result to the `enc` string.
 Iterate through the characters of the encrypted message (`enc`) in groups of three and

decrypt each group using the `decode` method. Append the result to the `dec` string.
 Print the original `msg`, the padded `msg`, the encrypted message `enc`, and the

decrypted message `dec`.

IT8761
KRITHIK CS 710720205023

PROGRAM:
class hill{
public static int[][] keymat = new int[][]{{ 1, 2, 1 },{ 2, 3, 2 },{ 2, 2, 1 } };
public static int[][] invkeymat = new int[][]{{ -1, 0, 1 },{ 2, -1, 0 },{ -2, 2, -1 }};
public static String key = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static String encode(char a, char b, char c){
String ret = "";
int x, y, z;
int posa = (int) a - 65;
int posb = (int) b - 65;
int posc = (int) c - 65;
x = posa * keymat[0][0] + posb * keymat[1][0] + posc * keymat[2][0];
y = posa * keymat[0][1] + posb * keymat[1][1] + posc * keymat[2][1];
z = posa * keymat[0][2] + posb * keymat[1][2] + posc * keymat[2][2];
a = key.charAt(x % 26);
b = key.charAt(y % 26);
c = key.charAt(z % 26);
ret = "" + a + b + c;
return ret;}
private static String decode(char a, char b, char c){
String ret = "";
int x, y, z;
int posa = (int) a - 65;
int posb = (int) b - 65;
int posc = (int) c - 65;
x = posa * invkeymat[0][0] + posb * invkeymat[1][0] + posc * invkeymat[2][0];
y = posa * invkeymat[0][1] + posb * invkeymat[1][1] + posc * invkeymat[2][1];
z = posa * invkeymat[0][2] + posb * invkeymat[1][2] + posc * invkeymat[2][2];
a = key.charAt((x % 26 < 0) ? (26 + x % 26) : (x % 26));
b = key.charAt((y % 26 < 0) ? (26 + y % 26) : (y % 26));
c = key.charAt((z % 26 < 0) ? (26 + z % 26) : (z % 26));
ret = "" + a + b + c;
return ret;}
public static void main(String[] args) throws java.lang.Exception
{
String msg;
String enc = "";
String dec = "";

IT8761
KRITHIK CS 710720205023

int n;
msg = ("coimbatore");
System.out.println("------------| 20IT023 |------------");
System.out.println("Input message : " + msg);
msg = msg.toUpperCase();
msg = msg.replaceAll("\\s", "");
n = msg.length() % 3;
if (n != 0){
for (int i = 1; i <= (3 - n); i++){
msg += 'X';}}
System.out.println("padded message : " + msg);
char[] pdchars = msg.toCharArray();
for (int i = 0; i < msg.length(); i += 3){
enc += encode(pdchars[i], pdchars[i + 1], pdchars[i + 2]);}
System.out.println("encoded message : " + enc);
char[] dechars = enc.toCharArray();
for (int i = 0; i < enc.length(); i += 3){
dec += decode(dechars[i], dechars[i + 1], dechars[i + 2]);}
System.out.println("decoded message : " + dec);}}

OUTPUT:

RESULT:

IT8761

You might also like