Abhinav Stephen Crypto Assignment 1

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

CRYPTOGRAPHY ASSIGNMENT 1

Name:M.Abhinav Stephen

ID:190C2030035

Code in C or C++ or java/Python for implementing the following functions required in a cipher: split,
combine, swap, circular shift, tables P-box and S-box (both sides) and one Round of the non-Feistel
cipher.

class Crypto {

public static void main(String[] args) {

Crypto object = new Crypto();

String splitInput = "100010101";

String[] splitOutput = object.split(splitInput, 3);

System.out.println("\n\nSplit Input: " + splitInput);

System.out.println("Split Output: " + splitOutput[0] + " " + splitOutput[1]);

System.out.println("Combine Output: " + object.combine(splitOutput));

String circularInput = "100010101";

String circularOutput = object.circularShift(circularInput, 1, "right");

System.out.println("\nCircular Shift Input: " + circularInput);

System.out.println("Circular Shift Output: " + circularOutput);

int[] pBoxMapping = new int[] { 1, 4, 3, 2, 0 };

String pBoxInput = "01234567";

System.out.println("\nPBox Input: " + pBoxInput);

String pBoxOutput = object.pBox(pBoxInput, pBoxMapping);

System.out.println("PBox Output: " + pBoxOutput);

String sbox1[][] = { { "01", "10" }, { "00", "11" } };

String bit = "01";


System.out.println("SBox Output: " + object.sbox(sbox1,bit));

System.out.println("nonfiestel Output: " + object.nonfiestel("10101010","00110011"));

String[] split(String inword, int splitIndex) {

String[] output = new String[2];

output[0] = inword.substring(0, splitIndex);

output[1] = inword.substring(splitIndex);

return output;

String combine(String[] outword) {

String output = "";

output += outword[0];

output += outword[1];

return output;

String swap(String inword, int swapIndex) {

return inword.substring(swapIndex) + inword.substring(0, swapIndex);

String circularShift(String inword, int shift, String direction) {

for (int i = 0; i < shift; i++) {

if (direction == "left") {

char temp = inword.charAt(0);

inword = inword.substring(1) + temp;

} else {

char temp = inword.charAt(inword.length() - 1);


inword = temp + inword.substring(0, inword.length() - 1);

return inword;

String pBox(String inword, int[] box) {

int boxLength = box.length;

String output = "";

int j = 0;

for (int i = 0; output.length() - j != inword.length(); i++) {

int index = box[i % boxLength];

int shift = i / boxLength;

if (inword.length() > (shift * boxLength + index)) {

output += inword.charAt(shift * boxLength + index);

} else {

output += "\0";

j++;

return output;

public String sbox(String[][] sbox, String input) {

char p1 = input.charAt(0);

char p2 = input.charAt(1);

int a = Integer.parseInt(String.valueOf(p1));
int b = Integer.parseInt(String.valueOf(p2));

return (sbox[a][b]);

public String nonfiestel(String word,String key){

String whitener = "";

for (int i = 0; i < 8; i++) {

int v1 = Integer.parseInt(String.valueOf(word.charAt(i)));

int v2 = Integer.parseInt(String.valueOf(key.charAt(i)));

int result = v1 ^ v2;

String res = String.valueOf(result);

whitener = whitener + res;

String sbox1[][] = { { "01", "10" }, { "00", "11" } };

String sbox2[][] = { { "10", "11" }, { "01", "00" } };

String sbox3[][] = { { "11", "00" }, { "01", "10" } };

String sbox4[][] = { { "11", "10" }, { "01", "00" } };

String p1 = sbox(sbox1, whitener.substring(0, 2));

String p2 = sbox(sbox2, whitener.substring(2, 4));

String p3 = sbox(sbox3, whitener.substring(4, 6));

String p4 = sbox(sbox4, whitener.substring(6, 8));

String mid = p1 + p2 + p3 + p4;

int[] pbox = { 1, 3, 6, 7, 5, 2, 0, 4 };

String output = "";

for (int i = 0; i < 8; i++) {

int pos = pbox[i];


char c = mid.charAt(pos);

output = output + c;

return output;

You might also like