0% found this document useful (0 votes)
21 views17 pages

Key: 7 Plaintext: HELLO Ciphertext: OLSSV Decrypted: HELLO

The document outlines various encryption techniques implemented in Java, including additive, multiplicative, affine, hill, playfair, transposition, and SDES ciphers. Each section provides code examples for encryption and decryption processes, along with sample outputs demonstrating the functionality of each cipher. The aim of each practical is to implement and understand different cipher algorithms.

Uploaded by

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

Key: 7 Plaintext: HELLO Ciphertext: OLSSV Decrypted: HELLO

The document outlines various encryption techniques implemented in Java, including additive, multiplicative, affine, hill, playfair, transposition, and SDES ciphers. Each section provides code examples for encryption and decryption processes, along with sample outputs demonstrating the functionality of each cipher. The aim of each practical is to implement and understand different cipher algorithms.

Uploaded by

yuvrajhimself1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Practical 1

Aim:- To implement additive cipher

Code:-
import [Link].*;
public class AdditiveCipher {
static String encrypt(String text, int key) {
StringBuilder result = new StringBuilder();
for (char c : [Link]()) {
if ([Link](c)) {
char base = [Link](c) ? 'A' : 'a';
[Link]((char) ((c - base + key) % 26 + base));
} else {
[Link](c);
} }
return [Link]();
} static String decrypt(String text, int key) {
return encrypt(text, 26 - (key % 26));
} public static void main(String[] args) {
String plaintext = "HELLO";
int key = 7;
String ciphertext = encrypt(plaintext, key);
String decrypted = decrypt(ciphertext, key);
[Link]("Key: " + key);
[Link]("Plaintext: " + plaintext);
[Link]("Ciphertext: " + ciphertext);
[Link]("Decrypted: " + decrypted);}}
Output

Key: 7
Plaintext: HELLO
Ciphertext: OLSSV
Decrypted: HELLO
Aim:- To implement multiplicative cipher

Code:-
public class MultiplicativeCipher {
static int modInverse(int a, int m) {
a = a % m;
for (int x = 1; x < m; x++) if ((a * x) % m == 1) return x;
return 1;
} static String encrypt(String text, int key) {
StringBuilder result = new StringBuilder();
for (char c : [Link]()) {
if ([Link](c)) {
char base = [Link](c) ? 'A' : 'a';
[Link]((char) (((c - base) * key) % 26 + base));
} else [Link](c);
}
return [Link]();
} static String decrypt(String text, int key) {
int inv = modInverse(key, 26);
StringBuilder result = new StringBuilder();
for (char c : [Link]()) {
if ([Link](c)) {
char base = [Link](c) ? 'A' : 'a';
[Link]((char) ((inv * (c - base)) % 26 + base));
} else [Link](c);
}
return [Link]();
} public static void main(String[] args) {
String plaintext = "HELLO";
int key = 3;
String ciphertext = encrypt(plaintext, key);
String decrypted = decrypt(ciphertext, key);
[Link]("Key: " + key);
[Link]("Plaintext: " + plaintext);
[Link]("Ciphertext: " + ciphertext);
[Link]("Decrypted: " + decrypted);
}
}

Output

Key: 3
Plaintext: HELLO
Ciphertext: VSZZC
Decrypted: HELLO

Practical 2

Aim:- To implement affine cipher

Code:-
public class AffineCipher {
static int modInverse(int a, int m) {
a = a % m;
for (int x = 1; x < m; x++) if ((a * x) % m == 1) return x;
return 1;
} static String encrypt(String text, int a, int b) {
StringBuilder result = new StringBuilder();
for (char c : [Link]()) {
if ([Link](c)) {
char base = [Link](c) ? 'A' : 'a';
[Link]((char) (((a * (c - base) + b) % 26) + base));
} else [Link](c);
}
return [Link]();
} static String decrypt(String text, int a, int b) {
int inv = modInverse(a, 26);
StringBuilder result = new StringBuilder();
for (char c : [Link]()) {
if ([Link](c)) {
char base = [Link](c) ? 'A' : 'a';
[Link]((char) ((inv * ((c - base - b + 26)) % 26) + base));
} else [Link](c);
}
return [Link]();
} public static void main(String[] args) {
String plaintext = "HELLO";
int a = 5, b = 8;
String ciphertext = encrypt(plaintext, a, b);
String decrypted = decrypt(ciphertext, a, b);
[Link]("Key: a=" + a + ", b=" + b);
[Link]("Plaintext: " + plaintext);
[Link]("Ciphertext: " + ciphertext);
[Link]("Decrypted: " + decrypted);
}
}

Output

Key: a=5, b=8


Plaintext: HELLO
Ciphertext: RCLLA
Decrypted: HELLO
Aim:- To implement hill cipher
CODE :-

import [Link].*;

public class HillCipher {


static int[][] keyMatrix = { {3, 3}, {2, 5} };

static String encrypt(String text) {


text = [Link]().replaceAll("[^A-Z]", "");
if ([Link]() % 2 != 0) text += "X";
StringBuilder result = new StringBuilder();
for (int i = 0; i < [Link](); i += 2) {
int a = [Link](i) - 'A';
int b = [Link](i + 1) - 'A';
int x = (keyMatrix[0][0] * a + keyMatrix[0][1] * b) % 26;
int y = (keyMatrix[1][0] * a + keyMatrix[1][1] * b) % 26;
[Link]((char) (x + 'A')).append((char) (y + 'A'));
}
return [Link]();
}

static int modInverse(int a, int m) {


a = a % m;
for (int x = 1; x < m; x++) if ((a * x) % m == 1) return x;
return 1;
}

static String decrypt(String cipher) {


int det = (keyMatrix[0][0] * keyMatrix[1][1] - keyMatrix[0][1] *
keyMatrix[1][0]) % 26;
if (det < 0) det += 26;
int invDet = modInverse(det, 26);
int[][] invKey = new int[2][2];
invKey[0][0] = (keyMatrix[1][1] * invDet) % 26;
invKey[1][1] = (keyMatrix[0][0] * invDet) % 26;
invKey[0][1] = (-keyMatrix[0][1] * invDet) % 26;
invKey[1][0] = (-keyMatrix[1][0] * invDet) % 26;
for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) if (invKey[i][j] < 0)
invKey[i][j] += 26;
StringBuilder result = new StringBuilder();
for (int i = 0; i < [Link](); i += 2) {
int a = [Link](i) - 'A';
int b = [Link](i + 1) - 'A';
int x = (invKey[0][0] * a + invKey[0][1] * b) % 26;
int y = (invKey[1][0] * a + invKey[1][1] * b) % 26;
[Link]((char) (x + 'A')).append((char) (y + 'A'));
}
return [Link]();
}

public static void main(String[] args) {


String plaintext = "HELP";
String ciphertext = encrypt(plaintext);
String decrypted = decrypt(ciphertext);
[Link]("Key Matrix: [[3,3],[2,5]]");
[Link]("Plaintext: " + plaintext);
[Link]("Ciphertext: " + ciphertext);
[Link]("Decrypted: " + decrypted);
}
}

Output

Key Matrix: [[3,3],[2,5]]


Plaintext: HELP
Ciphertext: HIAT
Decrypted: HELP
Practical 3
Aim:- To implement play fair cipher
CODE:-
import [Link].*;
public class PlayfairCipher {
static char[][] keyTable = new char[5][5];
static void generateKeyTable(String key) {
key = [Link]().replace("J", "I");
boolean[] used = new boolean[26];
int k = 0;
for (char c : [Link]()) {
if (c < 'A' || c > 'Z') continue;
if (!used[c - 'A']) {
keyTable[k / 5][k % 5] = c;
used[c - 'A'] = true;
k++;
}
}
for (char c = 'A'; c <= 'Z'; c++) {
if (c == 'J') continue;
if (!used[c - 'A']) {
keyTable[k / 5][k % 5] = c;
used[c - 'A'] = true;
k++;
}
}
} static String formatText(String text) {
text = [Link]().replace("J", "I").replaceAll("[^A-Z]", "");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < [Link](); i++) {
[Link]([Link](i));
if (i + 1 < [Link]() && [Link](i) == [Link](i + 1))
[Link]('X');
}
if ([Link]() % 2 != 0) [Link]('X');
return [Link]();
}

static String encryptPair(char a, char b) {


int[] posA = findPos(a), posB = findPos(b);
if (posA[0] == posB[0]) return "" + keyTable[posA[0]][(posA[1] + 1) % 5] +
keyTable[posB[0]][(posB[1] + 1) % 5];
else if (posA[1] == posB[1]) return "" + keyTable[(posA[0] + 1) %
5][posA[1]] + keyTable[(posB[0] + 1) % 5][posB[1]];
else return "" + keyTable[posA[0]][posB[1]] + keyTable[posB[0]][posA[1]];
}
static String decryptPair(char a, char b) {
int[] posA = findPos(a), posB = findPos(b);
if (posA[0] == posB[0]) return "" + keyTable[posA[0]][(posA[1] + 4) % 5] +
keyTable[posB[0]][(posB[1] + 4) % 5];
else if (posA[1] == posB[1]) return "" + keyTable[(posA[0] + 4) %
5][posA[1]] + keyTable[(posB[0] + 4) % 5][posB[1]];
else return "" + keyTable[posA[0]][posB[1]] + keyTable[posB[0]][posA[1]];
}

static int[] findPos(char c) {


for (int i = 0; i < 5; i++) for (int j = 0; j < 5; j++) if (keyTable[i][j] == c) return
new int[]{i, j};
return null;
}
static String encrypt(String text) {
text = formatText(text);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < [Link](); i += 2) [Link](encryptPair([Link](i),
[Link](i + 1)));
return [Link]();
}
static String decrypt(String text) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < [Link](); i += 2) [Link](decryptPair([Link](i),
[Link](i + 1)));
return [Link]();
}
public static void main(String[] args) {
String key = "MONARCHY";
String plaintext = "HELLO";
generateKeyTable(key);
String ciphertext = encrypt(plaintext);
String decrypted = decrypt(ciphertext);
[Link]("Key: " + key);
[Link]("Plaintext: " + plaintext);
[Link]("Ciphertext: " + ciphertext);
[Link]("Decrypted: " + decrypted);
}
}
Output

Key: MONARCHY
Plaintext: HELLO
Ciphertext: KCBIQX
Decrypted: HELXLO

Aim :- To implement Transposition cipher


CODE:-
import [Link].*;
public class TranspositionCipher {
static String encrypt(String text, String key) {
int col = [Link]();
int row = (int) [Link]((double) [Link]() / col);
char[][] matrix = new char[row][col];
int k = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (k < [Link]()) matrix[i][j] = [Link](k++);
else matrix[i][j] = 'X';
}
}
Integer[] order = new Integer[col];
for (int i = 0; i < col; i++) order[i] = i;
[Link](order, [Link](i -> [Link](i)));
StringBuilder cipher = new StringBuilder();
for (int j : order) for (int i = 0; i < row; i++) [Link](matrix[i][j]);
return [Link]();
}
static String decrypt(String cipher, String key) {
int col = [Link]();
int row = (int) [Link]((double) [Link]() / col);
char[][] matrix = new char[row][col];
Integer[] order = new Integer[col];
for (int i = 0; i < col; i++) order[i] = i;
[Link](order, [Link](i -> [Link](i)));
int k = 0;
for (int j : order) for (int i = 0; i < row; i++) matrix[i][j] = [Link](k++);
StringBuilder plain = new StringBuilder();
for (int i = 0; i < row; i++) for (int j = 0; j < col; j++) [Link](matrix[i][j]);
return [Link]().replace("X", "");
}

public static void main(String[] args) {


String plaintext = "HELLOWORLD";
String key = "ZEBRAS";
String ciphertext = encrypt(plaintext, key);
String decrypted = decrypt(ciphertext, key);
[Link]("Key: " + key);
[Link]("Plaintext: " + plaintext);
[Link]("Ciphertext: " + ciphertext);
[Link]("Decrypted: " + decrypted);
}
}

Output

Key: ZEBRAS
Plaintext: HELLOWORLD
Ciphertext: EOHLLWRLOD
Decrypted: HELLOWORLD
Practical 4
Aim:- To implement SDES encryption
technique

Code:-
import [Link].*;
public class SDES {
static int[] P10 = {3,5,2,7,4,10,1,9,8,6};
static int[] P8 = {6,3,7,4,8,5,10,9};
static int[] P4 = {2,4,3,1};
static int[] IP = {2,6,3,1,4,8,5,7};
static int[] IP_INV = {4,1,3,5,7,2,8,6};
static int[] EP = {4,1,2,3,2,3,4,1};
static int[][] S0 = {
{1,0,3,2},
{3,2,1,0},
{0,2,1,3},
{3,1,3,2}
};
static int[][] S1 = {
{0,1,2,3},
{2,0,1,3},
{3,0,1,0},
{2,1,0,3}
};
static String permute(String k, int[] arr) {
StringBuilder sb = new StringBuilder();
for (int i : arr) [Link]([Link](i-1));
return [Link]();
}
static String leftShift(String s, int n) {
return [Link](n) + [Link](0,n);
}
static String[] generateKeys(String key) {
String p10 = permute(key,P10);
String left = [Link](0,5);
String right = [Link](5,10);
left = leftShift(left,1);
right = leftShift(right,1);
String k1 = permute(left+right,P8);
left = leftShift(left,2);
right = leftShift(right,2);
String k2 = permute(left+right,P8);
return new String[]{k1,k2};
}
static String xor(String a, String b) {
StringBuilder sb = new StringBuilder();
for(int i=0;i<[Link]();i++)
[Link]([Link](i)==[Link](i)?'0':'1');
return [Link]();
}
static String sBox(String in, int[][] s) {
int row = [Link](""+[Link](0)+[Link](3),2);
int col = [Link](""+[Link](1)+[Link](2),2);
int val = s[row][col];
return [Link]("%2s",[Link](val)).replace(' ','0');
}
static String f(String right, String key) {
String ep = permute(right,EP);
String xored = xor(ep,key);
String left = sBox([Link](0,4),S0);
String rightPart = sBox([Link](4),S1);
String sOut = left+rightPart;
return permute(sOut,P4);
}
static String round(String text, String key) {
String left = [Link](0,4);
String right = [Link](4,8);
String fOut = f(right,key);
String leftXor = xor(left,fOut);
return leftXor+right;
}
static String encrypt(String pt, String[] keys) {
String ip = permute(pt,IP);
String r1 = round(ip,keys[0]);
String swapped = [Link](4,8)+[Link](0,4);
String r2 = round(swapped,keys[1]);
return permute(r2,IP_INV);
}
static String decrypt(String ct, String[] keys) {
String ip = permute(ct,IP);
String r1 = round(ip,keys[1]);
String swapped = [Link](4,8)+[Link](0,4);
String r2 = round(swapped,keys[0]);
return permute(r2,IP_INV);
}
static String charTo8Bit(char c) {
return [Link]("%8s", [Link](c)).replace(' ','0');
}
static char bitToChar(String bits) {
return (char)[Link](bits,2);
}
public static void main(String[] args) {
String plaintext = "A";
String key10 = "1010000010";
String[] keys = generateKeys(key10);
StringBuilder ctBits = new StringBuilder();
StringBuilder ctChars = new StringBuilder();
StringBuilder decChars = new StringBuilder();
for(char c: [Link]()) {
String bits = charTo8Bit(c);
String encBits = encrypt(bits,keys);
String decBits = decrypt(encBits,keys);
[Link](encBits).append(" ");
[Link](bitToChar(encBits));
[Link](bitToChar(decBits));
} [Link]("Plaintext: " + plaintext);
[Link]("Key (10-bit): " + key10);
[Link]("Ciphertext (bits): " + [Link]().trim());
[Link]("Ciphertext (chars): " + [Link]());
[Link]("Decrypted: " + [Link]()); }}
Output
Plaintext: A
Key (10-bit): 1010000010
Ciphertext (bits): 01101100
Ciphertext (chars): l
Decrypted: A

Practical 5

Aim:- To implement SAES encryption


technique

Code:-
import [Link].*;
public class SAES {
static int[] SBOX = {9,4,10,11,13,1,8,5,6,2,0,3,12,14,15,7};
static int[] INV_SBOX = {10,5,9,11,1,7,8,15,6,0,2,3,4,13,12,14};
static int[][] MIXCOL = {
{1,4},{4,1} };
static int[][] INV_MIXCOL = {
{9,2},{2,9} };
static int RCON1 = 0b10000000;
static int RCON2 = 0b00110000;

static int mult(int a, int b) {


int res = 0;
while(b>0) {
if((b&1)==1) res^=a;
a<<=1;
if((a&0x10)!=0) a^=0b10011;
b>>=1;
}
return res&0xF;
} static int subNib(int x) {
return (SBOX[(x>>4)&0xF]<<4)|SBOX[x&0xF];
} static int invSubNib(int x) {
return (INV_SBOX[(x>>4)&0xF]<<4)|INV_SBOX[x&0xF];
} static int[] keyExpansion(int key) {
int w0 = (key>>8)&0xFF;
int w1 = key&0xFF;
int w2 = w0 ^ RCON1 ^ subNib(((w1>>4)&0xF)|((w1&0xF)<<4));
int w3 = w2 ^ w1;
int w4 = w2 ^ RCON2 ^ subNib(((w3>>4)&0xF)|((w3&0xF)<<4));
int w5 = w4 ^ w3;
int k0 = (w0<<8)|w1;
int k1 = (w2<<8)|w3;
int k2 = (w4<<8)|w5;
return new int[]{k0,k1,k2};
} static int addKey(int state,int key) {
return state^key;
}
static int subNibble(int state) {
int out=0;
for(int i=0;i<4;i++) {
out |= (SBOX[(state>>(i*4))&0xF]<<(i*4));
} return out; }
static int invSubNibble(int state) {
int out=0;
for(int i=0;i<4;i++) {
out |= (INV_SBOX[(state>>(i*4))&0xF]<<(i*4));
} return out; }
static int shiftRow(int state) {
int s0 = state&0xF;
int s1 = (state>>4)&0xF;
int s2 = (state>>8)&0xF;
int s3 = (state>>12)&0xF;
return (s0)|(s3<<4)|(s2<<8)|(s1<<12);
}
static int mixColumns(int state) {
int s0 = state&0xF;
int s1 = (state>>4)&0xF;
int s2 = (state>>8)&0xF;
int s3 = (state>>12)&0xF;
int t0 = mult(MIXCOL[0][0],s0)^mult(MIXCOL[0][1],s2);
int t2 = mult(MIXCOL[1][0],s0)^mult(MIXCOL[1][1],s2);
int t1 = mult(MIXCOL[0][0],s1)^mult(MIXCOL[0][1],s3);
int t3 = mult(MIXCOL[1][0],s1)^mult(MIXCOL[1][1],s3);
return (t3<<12)|(t2<<8)|(t1<<4)|t0;
}
static int invMixColumns(int state) {
int s0 = state&0xF;
int s1 = (state>>4)&0xF;
int s2 = (state>>8)&0xF;
int s3 = (state>>12)&0xF;
int t0 = mult(INV_MIXCOL[0][0],s0)^mult(INV_MIXCOL[0][1],s2);
int t2 = mult(INV_MIXCOL[1][0],s0)^mult(INV_MIXCOL[1][1],s2);
int t1 = mult(INV_MIXCOL[0][0],s1)^mult(INV_MIXCOL[0][1],s3);
int t3 = mult(INV_MIXCOL[1][0],s1)^mult(INV_MIXCOL[1][1],s3);
return (t3<<12)|(t2<<8)|(t1<<4)|t0;
}
static int encryptBlock(int plaintext,int[] keys) {
int state = addKey(plaintext,keys[0]);
state = subNibble(state);
state = shiftRow(state);
state = mixColumns(state);
state = addKey(state,keys[1]);
state = subNibble(state);
state = shiftRow(state);
state = addKey(state,keys[2]); return state; }
static int decryptBlock(int ciphertext,int[] keys) {
int state = addKey(ciphertext,keys[2]);
state = invSubNibble(state);
state = shiftRow(state);
state = addKey(state,keys[1]);
state = invMixColumns(state);
state = invSubNibble(state);
state = shiftRow(state);
state = addKey(state,keys[0]);
return state;
}
static int charsTo16Bits(String s) {
return ([Link](0)<<8)|([Link](1));
}
static String bitsToChars(int val) {
char c1 = (char)((val>>8)&0xFF);
char c2 = (char)(val&0xFF);
return ""+c1+c2;
} static String toBinaryString(int val) {
return [Link]("%16s",[Link](val)).replace(' ','0');
} static String toHexString(int val) {
return [Link]("%04X",val);
} public static void main(String[] args) {
String plaintext = "HI";
int key = 0b0100101101101011; // example 16-bit key
int[] keys = keyExpansion(key);
int ptBlock = charsTo16Bits(plaintext);
int ctBlock = encryptBlock(ptBlock,keys);
int decBlock = decryptBlock(ctBlock,keys);
[Link]("Plaintext: " + plaintext);
[Link]("Key (16-bit): " + toBinaryString(key));
[Link]("Ciphertext (bits): " + toBinaryString(ctBlock));
[Link]("Ciphertext (hex): " + toHexString(ctBlock));
[Link]("Ciphertext (chars): " + bitsToChars(ctBlock));
[Link]("Decrypted: " + bitsToChars(decBlock)); }}

Output
Plaintext: HI
Key (16-bit): 0100101101101011
Ciphertext (bits): 1010110000110010
Ciphertext (hex): AC32
Ciphertext (chars): ¬2
Decrypted: HI

You might also like