diff --git a/Encryption-Techniques/AES/AES256.py b/Encryption-Techniques/AES/AES256.py new file mode 100755 index 0000000..e87182a --- /dev/null +++ b/Encryption-Techniques/AES/AES256.py @@ -0,0 +1,150 @@ +from copy import copy +from tables import * + +def subBytes(block): + for i in range(len(block)): + block[i] = sbox[block[i]] + return block + +def subBytesInv(block): + for i in range(len(block)): + block[i] = sboxInv[block[i]] + return block + +def keyScheduleCore(word, i): + newWord = word[1:]+word[:1] + subBytes(newWord) + newWord[0] = newWord[0] ^ rCon[i] + return newWord + +def expandKey(cipherKey): + cipherKeySize = len(cipherKey) + assert cipherKeySize == 32 + expandedKey = [] + currentSize,rconIter = 0,1 + + t = [0,0,0,0] + + for i in range(cipherKeySize): + expandedKey.append(cipherKey[i]) + currentSize += cipherKeySize + + while currentSize < 240: + for i in range(4): + t[i] = expandedKey[(currentSize - 4) + i] + if currentSize % cipherKeySize == 0: + t = keyScheduleCore(t, rconIter) + rconIter += 1 + if currentSize % cipherKeySize == 16: + for i in range(4): + t[i] = sbox[t[i]] + for i in range(4): + expandedKey.append(((expandedKey[currentSize - cipherKeySize]) ^ (t[i]))) + currentSize += 1 + return expandedKey + +def createRoundKey(expandedKey,i): + return expandedKey[i*16:i*16+16] + +def addroundkey(block,roundKey): + for i in range(len(block)): + block[i] = block[i] ^ roundKey[i] + return block + +def blockfileColumn(filename): + test2 = filename + testarr = [] + for row in range(0,4): + for column in range(0,len(test2),4): + testarr.append(test2[row+column]) + return testarr + +def rotate(rotate, n): + return rotate[n:]+rotate[0:n] + +def shiftRow(blockfile): + block = blockfileColumn(blockfile) + for i in range(4): + block[i*4:i*4+4] = rotate(block[i*4:i*4+4], i) + blockDone = blockfileColumn(block) + return blockDone + +def shiftRowInv(blockfile): + block = blockfileColumn(blockfile) + for i in range(4): + block[i*4:i*4+4] = rotate(block[i*4:i*4+4], -i) + block = blockfileColumn(block) + return block + +def mixColumn(column): + col = copy(column) + column[0] = table_2[col[0]] ^ table_3[col[1]] ^ col[2] ^ col[3] + column[1] = col[0] ^ table_2[col[1]] ^ table_3[col[2]] ^ col[3] + column[2] = col[0] ^ col[1] ^ table_2[col[2]] ^ table_3[col[3]] + column[3] = table_3[col[0]] ^ col[1] ^ col[2] ^ table_2[col[3]] + return column + + +def mixColumns(col): + outCol = [] + for i in range(0,len(col),4): + outCol.append(mixColumn(col[i:i+4])) + return listoflist2singlelist(outCol) + +def mixColumnInv(column): + col = copy(column) + column[0] = table_14[col[0]] ^ table_11[col[1]] ^ table_13[col[2]] ^ table_9[col[3]] + column[1] = table_9[col[0]] ^ table_14[col[1]] ^ table_11[col[2]] ^ table_13[col[3]] + column[2] = table_13[col[0]] ^ table_9[col[1]] ^ table_14[col[2]] ^ table_11[col[3]] + column[3] = table_11[col[0]] ^ table_13[col[1]] ^ table_9[col[2]] ^ table_14[col[3]] + return column + + +def mixColumnsInv(col): + intList = [] + for i in range(0,len(col),4): + intList.append(mixColumnInv(col[i:i+4])) + return listoflist2singlelist(intList) + +def listoflist2singlelist(column): + columnList = [] + for row in column: + for i in range(0,len(row)): + columnList.append(row[i]) + return columnList + +def encrypt(block,key): + expandedKey = expandKey(key) + roundKey = createRoundKey(expandedKey,0) + block = addroundkey(block,roundKey) + + for i in range(1,14): + roundKey = createRoundKey(expandedKey,i) + block = subBytes(block) + block = shiftRow(block) + block = mixColumns(block) + block = addroundkey(block,roundKey) + + roundKey = createRoundKey(expandedKey,14) + block = subBytes(block) + block = shiftRow(block) + block = addroundkey(block,roundKey) + return block + +def decrypt(block,key): + expandedKey = expandKey(key) + roundKey = createRoundKey(expandedKey,14) + block = addroundkey(block,roundKey) + block = shiftRowInv(block) + block = subBytesInv(block) + + for i in range(13,0,-1): + roundKey = createRoundKey(expandedKey,i) + block = addroundkey(block,roundKey) + block = mixColumnsInv(block) + block = shiftRowInv(block) + block = subBytesInv(block) + + roundKey = createRoundKey(expandedKey,0) + block = addroundkey(block,roundKey) + return block diff --git a/Encryption-Techniques/AES/README b/Encryption-Techniques/AES/README new file mode 100755 index 0000000..1318531 --- /dev/null +++ b/Encryption-Techniques/AES/README @@ -0,0 +1,10 @@ +Instructions: + 1. The 16-byte hex you want to encrypt is in file 'testBlock'. + 2. The key for this encryption is in file 'testKey'. Key is 32 byte hex. + 3. To encrypt run - + python main.py encrypt + --- Encrypted text gets written in file 'encrypted_file' + 4. To decrypt run - + python main.py decrypt + --- Decrypted text gets written in file 'decrypted_file' + diff --git a/Encryption-Techniques/AES/demo.png b/Encryption-Techniques/AES/demo.png new file mode 100644 index 0000000..d97e982 Binary files /dev/null and b/Encryption-Techniques/AES/demo.png differ diff --git a/Encryption-Techniques/AES/main.py b/Encryption-Techniques/AES/main.py new file mode 100755 index 0000000..a33cd68 --- /dev/null +++ b/Encryption-Techniques/AES/main.py @@ -0,0 +1,52 @@ +from readfiles import * +from AES256 import * +import argparse +from argparse import RawTextHelpFormatter +import time + +parser = argparse.ArgumentParser(description='AES 256-bit encrypt and decrypt', formatter_class=RawTextHelpFormatter) +parser.add_argument('mode',help="encrypt or decrypt") +args = parser.parse_args() + +mode = str(args.mode).lower() + +def fileEncrypt(): + block = getEncryptBlock("testBlock") + key = getKey("testKey") + outfile = open("encrypted_file","wb") + strangen = "" + cryptLargeblock = [] + for i in range(len(block)): + cryptLargeblock.append(encrypt(block[i],key)) + + while i < len(cryptLargeblock): + for row in cryptLargeblock: + for item in row: + strangen += hex(item)[2:].zfill(2) + i += 1 + outfile.write(strangen) + outfile.close() + print strangen + +def fileDecrypt(): + key = getKey("testKey") + file = open("decrypted_file","wb") + decryptBlock = [] + decString = "" + eblock = getDecryptBlock("encrypted_file") + + for i in range(len(eblock)): + decryptBlock.append(decrypt(eblock[i],key)) + + for row in decryptBlock: + for item in row: + decString += chr(item) + + file.write(decString) + file.close() + print decString + +if mode == "encrypt" or mode == "e": + fileEncrypt() +elif mode == "decrypt" or mode == "d": + fileDecrypt() diff --git a/Encryption-Techniques/AES/readfiles.py b/Encryption-Techniques/AES/readfiles.py new file mode 100755 index 0000000..5cbdb21 --- /dev/null +++ b/Encryption-Techniques/AES/readfiles.py @@ -0,0 +1,38 @@ +def getKey(filename): + len_key = 64 + keyfile = open(filename, 'r') + hexadecimalkey = keyfile.read() + keyarray = [] + for i in range(0, len_key, 2): + keyarray.append(int(hexadecimalkey[i:i+2], 16)) + return keyarray + +def getDecryptBlock(block): + file = open(block, 'r') + byte = file.read() + blockarray = [] + block = [] + for i in range(0,len(byte),2): + blockarray.append(int(byte[i:i+2],16)) + + for i in range(0,len(blockarray),16): + block.append(blockarray[i:i+16]) + return block + +def getEncryptBlock(block): + file = open(block,'rb') + byte = file.read().encode("hex") + blockarray = [] + arr = [] + for i in range(0,len(byte),2): + arr.append(int(byte[i:i+2], 16)) + + while len(arr) >= 16: + blockarray.append(arr[0:16]) + arr = arr[16:] + if len(arr) < 16: + temparray = [0]*16 + for k in range(0,len(arr)): + temparray[k] = arr[k] + blockarray.append(temparray) + return blockarray diff --git a/Encryption-Techniques/AES/tables.py b/Encryption-Techniques/AES/tables.py new file mode 100755 index 0000000..76982a8 --- /dev/null +++ b/Encryption-Techniques/AES/tables.py @@ -0,0 +1,163 @@ +sbox = [ + 0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76, + 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0, + 0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15, + 0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75, + 0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0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02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, + 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, + 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, + 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, + 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, + 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, + 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, + 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, + 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, + 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d + ] + +vector_table = [2, 3, 1, 1, + 1, 2, 3, 1, + 1, 1, 2, 3, + 3, 1, 1, 2] + +table_2 = [ 0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c, 0x1e, + 0x20, 0x22, 0x24, 0x26, 0x28, 0x2a, 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e, + 0x40, 0x42, 0x44, 0x46, 0x48, 0x4a, 0x4c, 0x4e, 0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e, + 0x60, 0x62, 0x64, 0x66, 0x68, 0x6a, 0x6c, 0x6e, 0x70, 0x72, 0x74, 0x76, 0x78, 0x7a, 0x7c, 0x7e, + 0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c, 0x8e, 0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9c, 0x9e, + 0xa0, 0xa2, 0xa4, 0xa6, 0xa8, 0xaa, 0xac, 0xae, 0xb0, 0xb2, 0xb4, 0xb6, 0xb8, 0xba, 0xbc, 0xbe, + 0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce, 0xd0, 0xd2, 0xd4, 0xd6, 0xd8, 0xda, 0xdc, 0xde, + 0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee, 0xf0, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfc, 0xfe, + 0x1b, 0x19, 0x1f, 0x1d, 0x13, 0x11, 0x17, 0x15, 0x0b, 0x09, 0x0f, 0x0d, 0x03, 0x01, 0x07, 0x05, + 0x3b, 0x39, 0x3f, 0x3d, 0x33, 0x31, 0x37, 0x35, 0x2b, 0x29, 0x2f, 0x2d, 0x23, 0x21, 0x27, 0x25, + 0x5b, 0x59, 0x5f, 0x5d, 0x53, 0x51, 0x57, 0x55, 0x4b, 0x49, 0x4f, 0x4d, 0x43, 0x41, 0x47, 0x45, + 0x7b, 0x79, 0x7f, 0x7d, 0x73, 0x71, 0x77, 0x75, 0x6b, 0x69, 0x6f, 0x6d, 0x63, 0x61, 0x67, 0x65, + 0x9b, 0x99, 0x9f, 0x9d, 0x93, 0x91, 0x97, 0x95, 0x8b, 0x89, 0x8f, 0x8d, 0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x82, 0x81, + 0x9b, 0x98, 0x9d, 0x9e, 0x97, 0x94, 0x91, 0x92, 0x83, 0x80, 0x85, 0x86, 0x8f, 0x8c, 0x89, 0x8a, + 0xab, 0xa8, 0xad, 0xae, 0xa7, 0xa4, 0xa1, 0xa2, 0xb3, 0xb0, 0xb5, 0xb6, 0xbf, 0xbc, 0xb9, 0xba, + 0xfb, 0xf8, 0xfd, 0xfe, 0xf7, 0xf4, 0xf1, 0xf2, 0xe3, 0xe0, 0xe5, 0xe6, 0xef, 0xec, 0xe9, 0xea, + 0xcb, 0xc8, 0xcd, 0xce, 0xc7, 0xc4, 0xc1, 0xc2, 0xd3, 0xd0, 0xd5, 0xd6, 0xdf, 0xdc, 0xd9, 0xda, + 0x5b, 0x58, 0x5d, 0x5e, 0x57, 0x54, 0x51, 0x52, 0x43, 0x40, 0x45, 0x46, 0x4f, 0x4c, 0x49, 0x4a, + 0x6b, 0x68, 0x6d, 0x6e, 0x67, 0x64, 0x61, 0x62, 0x73, 0x70, 0x75, 0x76, 0x7f, 0x7c, 0x79, 0x7a, + 0x3b, 0x38, 0x3d, 0x3e, 0x37, 0x34, 0x31, 0x32, 0x23, 0x20, 0x25, 0x26, 0x2f, 0x2c, 0x29, 0x2a, + 0x0b, 0x08, 0x0d, 0x0e, 0x07, 0x04, 0x01, 0x02, 0x13, 0x10, 0x15, 0x16, 0x1f, 0x1c, 0x19, 0x1a] + +table_9 = [ 0x00, 0x09, 0x12, 0x1b, 0x24, 0x2d, 0x36, 0x3f, 0x48, 0x41, 0x5a, 0x53, 0x6c, 0x65, 0x7e, 0x77, + 0x90, 0x99, 0x82, 0x8b, 0xb4, 0xbd, 0xa6, 0xaf, 0xd8, 0xd1, 0xca, 0xc3, 0xfc, 0xf5, 0xee, 0xe7, + 0x3b, 0x32, 0x29, 0x20, 0x1f, 0x16, 0x0d, 0x04, 0x73, 0x7a, 0x61, 0x68, 0x57, 0x5e, 0x45, 0x4c, + 0xab, 0xa2, 0xb9, 0xb0, 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x2e, + 0x8c, 0x87, 0x9a, 0x91, 0xa0, 0xab, 0xb6, 0xbd, 0xd4, 0xdf, 0xc2, 0xc9, 0xf8, 0xf3, 0xee, 0xe5, + 0x3c, 0x37, 0x2a, 0x21, 0x10, 0x1b, 0x06, 0x0d, 0x64, 0x6f, 0x72, 0x79, 0x48, 0x43, 0x5e, 0x55, + 0x01, 0x0a, 0x17, 0x1c, 0x2d, 0x26, 0x3b, 0x30, 0x59, 0x52, 0x4f, 0x44, 0x75, 0x7e, 0x63, 0x68, + 0xb1, 0xba, 0xa7, 0xac, 0x9d, 0x96, 0x8b, 0x80, 0xe9, 0xe2, 0xff, 0xf4, 0xc5, 0xce, 0xd3, 0xd8, + 0x7a, 0x71, 0x6c, 0x67, 0x56, 0x5d, 0x40, 0x4b, 0x22, 0x29, 0x34, 0x3f, 0x0e, 0x05, 0x18, 0x13, + 0xca, 0xc1, 0xdc, 0xd7, 0xe6, 0xed, 0xf0, 0xfb, 0x92, 0x99, 0x84, 0x8f, 0xbe, 0xb5, 0xa8, 0xa3] + +table_13 = [0x00, 0x0d, 0x1a, 0x17, 0x34, 0x39, 0x2e, 0x23, 0x68, 0x65, 0x72, 0x7f, 0x5c, 0x51, 0x46, 0x4b, + 0xd0, 0xdd, 0xca, 0xc7, 0xe4, 0xe9, 0xfe, 0xf3, 0xb8, 0xb5, 0xa2, 0xaf, 0x8c, 0x81, 0x96, 0x9b, + 0xbb, 0xb6, 0xa1, 0xac, 0x8f, 0x82, 0x95, 0x98, 0xd3, 0xde, 0xc9, 0xc4, 0xe7, 0xea, 0xfd, 0xf0, + 0x6b, 0x66, 0x71, 0x7c, 0x5f, 0x52, 0x45, 0x48, 0x03, 0x0e, 0x19, 0x14, 0x37, 0x3a, 0x2d, 0x20, + 0x6d, 0x60, 0x77, 0x7a, 0x59, 0x54, 0x43, 0x4e, 0x05, 0x08, 0x1f, 0x12, 0x31, 0x3c, 0x2b, 0x26, + 0xbd, 0xb0, 0xa7, 0xaa, 0x89, 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 0xe2, 0xf0, 0xfe, 0xd4, 0xda, 0xc8, 0xc6, 0x9c, 0x92, 0x80, 0x8e, 0xa4, 0xaa, 0xb8, 0xb6, + 0x0c, 0x02, 0x10, 0x1e, 0x34, 0x3a, 0x28, 0x26, 0x7c, 0x72, 0x60, 0x6e, 0x44, 0x4a, 0x58, 0x56, + 0x37, 0x39, 0x2b, 0x25, 0x0f, 0x01, 0x13, 0x1d, 0x47, 0x49, 0x5b, 0x55, 0x7f, 0x71, 0x63, 0x6d, + 0xd7, 0xd9, 0xcb, 0xc5, 0xef, 0xe1, 0xf3, 0xfd, 0xa7, 0xa9, 0xbb, 0xb5, 0x9f, 0x91, 0x83, 0x8d] \ No newline at end of file diff --git a/Encryption-Techniques/AES/testBlock b/Encryption-Techniques/AES/testBlock new file mode 100755 index 0000000..55c80f2 --- /dev/null +++ b/Encryption-Techniques/AES/testBlock @@ -0,0 +1 @@ +00112233445566778899AABBCCDDEEFF \ No newline at end of file diff --git a/Encryption-Techniques/AES/testKey b/Encryption-Techniques/AES/testKey new file mode 100755 index 0000000..9c7bc66 --- /dev/null +++ b/Encryption-Techniques/AES/testKey @@ -0,0 +1 @@ +0000000000000000000000000000000000000000000000000000000000000000 \ No newline at end of file diff --git a/Encryption-Techniques/Caesar-Cipher/caesar.py b/Encryption-Techniques/Caesar-Cipher/caesar.py new file mode 100755 index 0000000..1684847 --- /dev/null +++ b/Encryption-Techniques/Caesar-Cipher/caesar.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python +import sys +from string import punctuation + +def remove_punctuation(content): + content = content.translate(None, punctuation); #removing !"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ + content = ''.join(content.split()) #removing tabs, spaces, newlines + return content.lower() + +def take_input(): + try: + k = int(raw_input("Enter key: ")) + return k + except Exception as e: + print "Error : ", e + sys.exit(0) + +def file_input(): + try: + fp = open(raw_input("Enter file: "), "r") + content = fp.read() + fp.close() + return content + except Exception as e: + print "Error : ", e + sys.exit(1) + +def display_format(message): + ''' Print the final encoded message in blocks of five letters and ten blocks per line. + The last line may be shorter than five blocks, and the last block may be shorter + than five letters. ''' + output = [] + enc = "" + while message: + output.append(message[:5]) + message = message[5:] + for index, block in enumerate(output): + if((index + 1) % 10 == 0): + enc = enc + block + "\n" + else: + enc = enc + block + " " + return enc + +def encrypt(content, k): + encrypted_message = "" + print "Discarding the punctuation..." + content = remove_punctuation(content) + print "Encrypting the text..." + for char in content: + encrypted_message += chr((ord(char) - ord('a') + k)%26 + ord('a')) #ord func gives ascii value #chr func gives character + encrypted_message = display_format(encrypted_message) + return encrypted_message + +def decrypt(content): + content = remove_punctuation(content) + print "Decryting the text with each key and writing to the file \"decrypt.output\"..." + try: + fp = open("decrypt.output", "w"); + for k in range(26): + plain_text = "" + for char in content: + plain_text += chr((ord(char) - ord('a') - (k+1))%26 + ord('a')) #ord func gives ascii value #chr func gives character + plain_text = display_format(plain_text) + fp.write("Key " + str(k+1) + " :\n") + fp.write(plain_text + "\n\n") + fp.close() + except Exception as e: + print "Error : ", e + +def main(): + try: + case = int(raw_input("1 to encrypt plain_text from file\n2 to decrypt ciphertext from file\nEnter option : ")) + if(case == 1): + k = take_input() + file_content = file_input() + encrypted_message = encrypt(file_content, k) + print "Encrypted Message..." + print encrypted_message + sys.exit(0) + if(case == 2): + encrypted_message = file_input() + decrypt(encrypted_message) + sys.exit(0) + else: + print "Wrong option" + sys.exit(0) + except KeyboardInterrupt: + print "\nClosing the program..." + sys.exit(0) + +if __name__ == '__main__': + main() diff --git a/Encryption-Techniques/Caesar-Cipher/cipher.input b/Encryption-Techniques/Caesar-Cipher/cipher.input new file mode 100755 index 0000000..a223a5c --- /dev/null +++ b/Encryption-Techniques/Caesar-Cipher/cipher.input @@ -0,0 +1,6 @@ +mrxli ievpc hecwf ymphm rkgsq tyxiv wcwxi qwaew iewca lccsy +ewofi geywi ywivw hmhrx ibtig xqygl mxmwx lswih evrih ywivw +amxlx limvi btigx exmsr wsjie wisjy wilmk ltivj svqer givip +mefmp mxcix gxlex viepp clezi pihxs eppxl iwili ehegl iwrib +xxmqi csyqi ixsri sjxls wigsq tyxiv ywivw xlero xliqj svepp +xlitv sfpiq wxlic lezig eywih diff --git a/Encryption-Techniques/Caesar-Cipher/decrypt.output b/Encryption-Techniques/Caesar-Cipher/decrypt.output new file mode 100644 index 0000000..45eff78 --- /dev/null +++ b/Encryption-Techniques/Caesar-Cipher/decrypt.output @@ -0,0 +1,208 @@ +Key 1 : +lqwkh hduob gdbve xlogl qjfrp sxwhu vbvwh pvzdv hdvbz kbbrx +dvneh fdxvh xvhuv glgqw hashf wpxfk lwlvw krvhg duqhg xvhuv +zlwkw khluh ashfw dwlrq vrihd vhrix vhklj kshui rupdq fhuho +ldelo lwbhw fwkdw uhdoo bkdyh ohgwr doowk hvhkh dgdfk hvqha +wwlph brxph hwrqh riwkr vhfrp sxwhu xvhuv wkdqn wkhpi rudoo +wkhsu reohp vwkhb kdyhf dxvhg + +Key 2 : +kpvjg gctna fcaud wknfk pieqo rwvgt uauvg ouycu gcuay jaaqw +cumdg ecwug wugtu fkfpv gzrge vowej kvkuv jqugf ctpgf wugtu +ykvjv jgktg zrgev cvkqp uqhgc ugqhw ugjki jrgth qtocp egtgn +kcdkn kvagv evjcv tgcnn ajcxg ngfvq cnnvj gugjg cfcej gupgz +vvkog aqwog gvqpg qhvjq ugeqo rwvgt wugtu vjcpm vjgoh qtcnn +vjgrt qdngo uvjga jcxge cwugf + +Key 3 : +jouif fbsmz ebztc vjmej ohdpn qvufs tztuf ntxbt fbtzx izzpv +btlcf dbvtf vtfst ejeou fyqfd unvdi jujtu iptfe bsofe vtfst +xjuiu ifjsf yqfdu bujpo tpgfb tfpgv tfijh iqfsg psnbo dfsfm +jbcjm juzfu duibu sfbmm zibwf mfeup bmmui ftfif bebdi ftofy +uujnf zpvnf fupof pguip tfdpn qvufs vtfst uibol uifng psbmm +uifqs pcmfn tuifz ibwfd bvtfe + +Key 4 : +inthe early daysb uildi ngcom puter syste mswas easyw hyyou +askbe cause users didnt expec tmuch itist hosed arned users +witht heire xpect ation sofea seofu sehig hperf orman cerel +iabil ityet cthat reall yhave ledto allth esehe adach esnex +ttime youme etone oftho secom puter users thank themf orall +thepr oblem sthey havec aused + +Key 5 : +hmsgd dzqkx czxra thkch mfbnl otsdq rxrsd lrvzr dzrxv gxxnt +zrjad bztrd trdqr chcms dwodb sltbg hshrs gnrdc zqmdc trdqr +vhsgs gdhqd wodbs zshnm rnedz rdnet rdghf godqe nqlzm bdqdk +hzahk hsxds bsgzs qdzkk xgzud kdcsn zkksg drdgd zczbg drmdw +sshld xntld dsnmd nesgn rdbnl otsdq trdqr sgzmj sgdle nqzkk +sgdoq nakdl rsgdx gzudb ztrdc + +Key 6 : +glrfc cypjw bywqz sgjbg leamk nsrcp qwqrc kquyq cyqwu fwwms +yqizc aysqc sqcpq bgblr cvnca rksaf grgqr fmqcb yplcb sqcpq +ugrfr fcgpc vncar yrgml qmdcy qcmds qcfge fncpd mpkyl acpcj +gyzgj grwcr arfyr pcyjj wfytc jcbrm yjjrf cqcfc ybyaf cqlcv +rrgkc wmskc crmlc mdrfm qcamk nsrcp sqcpq rfyli rfckd mpyjj +rfcnp mzjck qrfcw fytca ysqcb + +Key 7 : +fkqeb bxoiv axvpy rfiaf kdzlj mrqbo pvpqb jptxp bxpvt evvlr +xphyb zxrpb rpbop afakq bumbz qjrze fqfpq elpba xokba rpbop +tfqeq ebfob umbzq xqflk plcbx pblcr pbefd emboc lojxk zbobi +fxyfi fqvbq zqexq obxii vexsb ibaql xiiqe bpbeb xaxze bpkbu +qqfjb vlrjb bqlkb lcqel pbzlj mrqbo rpbop qexkh qebjc loxii +qebmo lyibj pqebv exsbz xrpba + +Key 8 : +ejpda awnhu zwuox qehze jcyki lqpan ouopa ioswo awous duukq +wogxa ywqoa qoano zezjp atlay piqyd epeop dkoaz wnjaz qoano +sepdp daena tlayp wpekj okbaw oakbq oadec dlanb kniwj yanah +ewxeh epuap ypdwp nawhh udwra hazpk whhpd aoada wzwyd aojat +ppeia ukqia apkja kbpdk oayki lqpan qoano pdwjg pdaib knwhh +pdaln kxhai opdau dwray wqoaz + +Key 9 : +diocz zvmgt yvtnw pdgyd ibxjh kpozm ntnoz hnrvn zvntr cttjp +vnfwz xvpnz pnzmn ydyio zskzx ohpxc dodno cjnzy vmizy pnzmn +rdoco czdmz skzxo vodji njazv nzjap nzcdb ckzma jmhvi xzmzg +dvwdg dotzo xocvo mzvgg tcvqz gzyoj vggoc znzcz vyvxc znizs +oodhz tjphz zojiz jaocj nzxjh kpozm pnzmn ocvif oczha jmvgg +oczkm jwgzh noczt cvqzx vpnzy + +Key 10 : +chnby yulfs xusmv ocfxc hawig jonyl msmny gmqum yumsq bssio +umevy wuomy omylm xcxhn yrjyw ngowb cncmn bimyx ulhyx omylm +qcnbn bycly rjywn uncih mizyu myizo mybca bjylz ilguh wylyf +cuvcf cnsyn wnbun lyuff sbupy fyxni uffnb ymyby uxuwb ymhyr +nncgy siogy ynihy iznbi mywig jonyl omylm nbuhe nbygz iluff +nbyjl ivfyg mnbys bupyw uomyx + +Key 11 : +bgmax xtker wtrlu nbewb gzvhf inmxk lrlmx flptl xtlrp arrhn +tldux vtnlx nlxkl wbwgm xqixv mfnva bmblm ahlxw tkgxw nlxkl +pbmam axbkx qixvm tmbhg lhyxt lxhyn lxabz aixky hkftg vxkxe +btube bmrxm vmatm kxtee ratox exwmh teema xlxax twtva xlgxq +mmbfx rhnfx xmhgx hymah lxvhf inmxk nlxkl matgd maxfy hktee +maxik huexf lmaxr atoxv tnlxw + +Key 12 : +aflzw wsjdq vsqkt madva fyuge hmlwj kqklw ekosk wskqo zqqgm +skctw usmkw mkwjk vavfl wphwu lemuz alakl zgkwv sjfwv mkwjk +oalzl zwajw phwul slagf kgxws kwgxm kwzay zhwjx gjesf uwjwd +astad alqwl ulzsl jwsdd qzsnw dwvlg sddlz wkwzw svsuz wkfwp +llaew qgmew wlgfw gxlzg kwuge hmlwj mkwjk lzsfc lzwex gjsdd +lzwhj gtdwe klzwq zsnwu smkwv + +Key 13 : +zekyv vricp urpjs lzcuz extfd glkvi jpjkv djnrj vrjpn yppfl +rjbsv trljv ljvij uzuek vogvt kdlty zkzjk yfjvu rievu ljvij +nzkyk yvziv ogvtk rkzfe jfwvr jvfwl jvyzx ygviw fidre tvivc +zrszc zkpvk tkyrk ivrcc pyrmv cvukf rccky vjvyv rurty vjevo +kkzdv pfldv vkfev fwkyf jvtfd glkvi ljvij kyreb kyvdw fircc +kyvgi fscvd jkyvp yrmvt rljvu + +Key 14 : +ydjxu uqhbo tqoir kybty dwsec fkjuh ioiju cimqi uqiom xooek +qiaru sqkiu kiuhi tytdj unfus jcksx yjyij xeiut qhdut kiuhi +myjxj xuyhu nfusj qjyed ievuq iuevk iuxyw xfuhv ehcqd suhub +yqryb yjouj sjxqj huqbb oxqlu butje qbbjx uiuxu qtqsx uidun +jjycu oekcu ujedu evjxe iusec fkjuh kiuhi jxqda jxucv ehqbb +jxufh erbuc ijxuo xqlus qkiut + +Key 15 : +xciwt tpgan spnhq jxasx cvrdb ejitg hnhit bhlph tphnl wnndj +phzqt rpjht jhtgh sxsci tmetr ibjrw xixhi wdhts pgcts jhtgh +lxiwi wtxgt metri pixdc hdutp htduj htwxv wetgu dgbpc rtgta +xpqxa xinti riwpi gtpaa nwpkt atsid paaiw thtwt psprw thctm +iixbt ndjbt tidct duiwd htrdb ejitg jhtgh iwpcz iwtbu dgpaa +iwteg dqatb hiwtn wpktr pjhts + +Key 16 : +wbhvs sofzm romgp iwzrw buqca dihsf gmghs agkog sogmk vmmci +ogyps qoigs igsfg rwrbh sldsq haiqv whwgh vcgsr ofbsr igsfg +kwhvh vswfs ldsqh ohwcb gctso gscti gsvwu vdsft cfaob qsfsz +wopwz whmsh qhvoh fsozz mvojs zsrhc ozzhv sgsvs oroqv sgbsl +hhwas mcias shcbs cthvc gsqca dihsf igsfg hvoby hvsat cfozz +hvsdf cpzsa ghvsm vojsq oigsr + +Key 17 : +vagur rneyl qnlfo hvyqv atpbz chgre flfgr zfjnf rnflj ullbh +nfxor pnhfr hfref qvqag rkcrp gzhpu vgvfg ubfrq nearq hfref +jvgug urver kcrpg ngvba fbsrn frbsh fruvt ucres bezna prery +vnovy vglrg pgung ernyy lunir yrqgb nyygu rfrur nqnpu rfark +ggvzr lbhzr rgbar bsgub frpbz chgre hfref gunax gurzs benyy +gurce boyrz fgurl unirp nhfrq + +Key 18 : +uzftq qmdxk pmken guxpu zsoay bgfqd ekefq yeime qmeki tkkag +mewnq omgeq geqde pupzf qjbqo fygot ufuef taeqp mdzqp geqde +iuftf tqudq jbqof mfuaz earqm eqarg eqtus tbqdr adymz oqdqx +umnux ufkqf oftmf dqmxx ktmhq xqpfa mxxft qeqtq mpmot qezqj +ffuyq kagyq qfazq arfta eqoay bgfqd geqde ftmzw ftqyr admxx +ftqbd anxqy eftqk tmhqo mgeqp + +Key 19 : +tyesp plcwj oljdm ftwot yrnzx afepc djdep xdhld pldjh sjjzf +ldvmp nlfdp fdpcd otoye piapn exfns tetde szdpo lcypo fdpcd +htese sptcp iapne letzy dzqpl dpzqf dpstr sapcq zcxly npcpw +tlmtw tejpe nesle cplww jslgp wpoez lwwes pdpsp lolns pdypi +eetxp jzfxp pezyp zqesz dpnzx afepc fdpcd eslyv espxq zclww +espac zmwpx despj slgpn lfdpo + +Key 20 : +sxdro okbvi nkicl esvns xqmyw zedob cicdo wcgkc okcig riiye +kculo mkeco ecobc nsnxd ohzom dwemr sdscd rycon kbxon ecobc +gsdrd rosbo hzomd kdsyx cypok coype corsq rzobp ybwkx mobov +sklsv sdiod mdrkd bokvv irkfo vondy kvvdr ocoro knkmr ocxoh +ddswo iyewo odyxo ypdry comyw zedob ecobc drkxu drowp ybkvv +drozb ylvow cdroi rkfom kecon + +Key 21 : +rwcqn njauh mjhbk drumr wplxv ydcna bhbcn vbfjb njbhf qhhxd +jbtkn ljdbn dbnab mrmwc ngynl cvdlq rcrbc qxbnm jawnm dbnab +frcqc qnran gynlc jcrxw bxonj bnxod bnqrp qynao xavjw lnanu +rjkru rchnc lcqjc anjuu hqjen unmcx juucq nbnqn jmjlq nbwng +ccrvn hxdvn ncxwn xocqx bnlxv ydcna dbnab cqjwt cqnvo xajuu +cqnya xkunv bcqnh qjenl jdbnm + +Key 22 : +qvbpm miztg ligaj cqtlq vokwu xcbmz agabm uaeia miage pggwc +iasjm kicam camza lqlvb mfxmk buckp qbqab pwaml izvml camza +eqbpb pmqzm fxmkb ibqwv awnmi amwnc ampqo pxmzn wzuiv kmzmt +qijqt qbgmb kbpib zmitt gpidm tmlbw ittbp mampm ilikp mavmf +bbqum gwcum mbwvm wnbpw amkwu xcbmz camza bpivs bpmun wzitt +bpmxz wjtmu abpmg pidmk icaml + +Key 23 : +puaol lhysf khfzi bpskp unjvt wbaly zfzal tzdhz lhzfd offvb +hzril jhbzl bzlyz kpkua lewlj atbjo papza ovzlk hyulk bzlyz +dpaoa olpyl ewlja hapvu zvmlh zlvmb zlopn owlym vythu jlyls +phips pafla jaoha ylhss fohcl slkav hssao lzlol hkhjo lzule +aaptl fvbtl lavul vmaov zljvt wbaly bzlyz aohur aoltm vyhss +aolwy vislt zaolf ohclj hbzlk + +Key 24 : +otznk kgxre jgeyh aorjo tmius vazkx yeyzk sycgy kgyec neeua +gyqhk igayk aykxy jojtz kdvki zsain ozoyz nuykj gxtkj aykxy +coznz nkoxk dvkiz gzout yulkg ykula yknom nvkxl uxsgt ikxkr +oghor ozekz izngz xkgrr engbk rkjzu grrzn kyknk gjgin kytkd +zzosk euask kzutk ulznu ykius vazkx aykxy zngtq znksl uxgrr +znkvx uhrks yznke ngbki gaykj + +Key 25 : +nsymj jfwqd ifdxg znqin slhtr uzyjw xdxyj rxbfx jfxdb mddtz +fxpgj hfzxj zxjwx inisy jcujh yrzhm nynxy mtxji fwsji zxjwx +bnymy mjnwj cujhy fynts xtkjf xjtkz xjmnl mujwk twrfs hjwjq +nfgnq nydjy hymfy wjfqq dmfaj qjiyt fqqym jxjmj fifhm jxsjc +yynrj dtzrj jytsj tkymt xjhtr uzyjw zxjwx ymfsp ymjrk twfqq +ymjuw tgqjr xymjd mfajh fzxji + +Key 26 : +mrxli ievpc hecwf ymphm rkgsq tyxiv wcwxi qwaew iewca lccsy +ewofi geywi ywivw hmhrx ibtig xqygl mxmwx lswih evrih ywivw +amxlx limvi btigx exmsr wsjie wisjy wilmk ltivj svqer givip +mefmp mxcix gxlex viepp clezi pihxs eppxl iwili ehegl iwrib +xxmqi csyqi ixsri sjxls wigsq tyxiv ywivw xlero xliqj svepp +xlitv sfpiq wxlic lezig eywih + diff --git a/Encryption-Techniques/Caesar-Cipher/demo.png b/Encryption-Techniques/Caesar-Cipher/demo.png new file mode 100644 index 0000000..c60db83 Binary files /dev/null and b/Encryption-Techniques/Caesar-Cipher/demo.png differ diff --git a/Encryption-Techniques/Caesar-Cipher/plain.input b/Encryption-Techniques/Caesar-Cipher/plain.input new file mode 100755 index 0000000..50a99bb --- /dev/null +++ b/Encryption-Techniques/Caesar-Cipher/plain.input @@ -0,0 +1,5 @@ +In the early days, building computer systems was easy. Why, you ask? +Because users didn't expect much. It is those darned users with their +expectations of "ease of use", "high performance", "reliability", etc., that +really have led to all these headaches. Next time you meet one of those +computer users, thank them for all the problems they have caused. Hi. diff --git a/Encryption-Techniques/DES/SDES.pdf b/Encryption-Techniques/DES/SDES.pdf new file mode 100755 index 0000000..405263d Binary files /dev/null and b/Encryption-Techniques/DES/SDES.pdf differ diff --git a/Encryption-Techniques/DES/demo.png b/Encryption-Techniques/DES/demo.png new file mode 100755 index 0000000..ad6b001 Binary files /dev/null and b/Encryption-Techniques/DES/demo.png differ diff --git a/Encryption-Techniques/DES/sdes.py b/Encryption-Techniques/DES/sdes.py new file mode 100755 index 0000000..0e9ba39 --- /dev/null +++ b/Encryption-Techniques/DES/sdes.py @@ -0,0 +1,156 @@ +#!/usr/bin/env python +# +# Copyright (c) 2017 by Aditya Malu adityamalu1@gmail.com. All Rights Reserved. +# +import sys +p10 = (3, 5, 2, 7, 4, 10, 1, 9, 8, 6) +p8 = (6, 3, 7, 4, 8, 5, 10, 9) +p4 = (2, 4, 3, 1) +ip = (2, 6, 3, 1, 4, 8, 5, 7) +inverseip = (4, 1, 3, 5, 7, 2, 8, 6) +ep = (4, 1, 2, 3, 2, 3, 4, 1) +s0 = (['01', '00', '11', '10'], #[1, 0, 3, 2] + ['11', '10', '01', '00'], #[3, 2, 1, 0] + ['00', '10', '01', '11'], #[0, 2, 1, 3], + ['11', '01', '11', '10']) #[3, 1, 3, 2] +s1 = (['00', '01', '10', '11'], #[0, 1, 2, 3] + ['10', '00', '01', '11'], #[2, 0, 1, 3] + ['11', '00', '01', '00'], #[3, 0, 1, 0], + ['10', '01', '00', '11']) #[2, 1, 0, 3] + +def shiftleftn(K, n): + return K[n:] + K[:n] + +def doP10(K): + K1 = [] + for i in p10: + K1.append(K[i-1]) + return K1 + +def doP8(K): + K1 = [] + for i in p8: + K1.append(K[i-1]) + return K1 + +def doEP(R): + R1 = [] + for i in ep: + R1.append(R[i-1]) + return R1 + +def doP4(R): + R1 = [] + for i in p4: + R1.append(R[i-1]) + return R1 + + +def doSBOX(A): + bintodec = {00:0, 01:1, 10:2, 11:3} + L, R = A[:4], A[4:] + l_row = bintodec[int(str(L[0])+str(L[3]))] + l_col = bintodec[int(str(L[1])+str(L[2]))] + r_row = bintodec[int(str(R[0])+str(R[3]))] + r_col = bintodec[int(str(R[1])+str(R[2]))] + return map(int, list(s0[l_row][l_col] + s1[r_row][r_col])) + +def generate_keys(K): + K1 = doP10(K) + K1L = shiftleftn(K1[:5], 1) + K1R = shiftleftn(K1[5:], 1) + K1 = K1L + K1R + K1 = doP8(K1) + K2 = doP10(K) + K2L = shiftleftn(K2[:5], 3) + K2R = shiftleftn(K2[5:], 3) + K2 = K2L + K2R + K2 = doP8(K2) + return K1, K2 + +def doXOR(l1, l2): + l3 = [] + for i in range(len(l1)): + l3.append(l1[i]^l2[i]) + return l3 + +def doIP(P): + P1 = [] + for i in ip: + P1.append(P[i-1]) + return P1 + +def doinverseIP(P): + P1 = [] + for i in inverseip: + P1.append(P[i-1]) + return P1 + +def F(R, K): + R1 = doEP(R) + xor_result = doXOR(R1, K) + sbox_result = doSBOX(xor_result) + X = doP4(sbox_result) + return X + +def do_fK(P, K): + L, R = P[:4], P[4:] #fK(L, R) = (L xor F(R, SK), R) + I = F(R, K) + J = doXOR(L, I) + return J + R + +def encrypt(P, K): + K1, K2 = generate_keys(K) + P1 = doIP(P) + P2 = do_fK(P1, K1) + P3 = P2[4:]+P2[:4] + P4 = do_fK(P3, K2) + E = doinverseIP(P4) + return E + +def decrypt(C, K): + K1, K2 = generate_keys(K) + C1 = doIP(C) + C2 = do_fK(C1, K2) + C3 = C2[4:]+C2[:4] + C4 = do_fK(C3, K1) + D = doinverseIP(C4) + return D + +def main(): + print '-'*40 + case = int(raw_input("Option 1 to encrypt\nOption 2 to decrypt \nEnter option : ")) + ## Encrypt case + if case == 1: + P = map(int, list(raw_input("Enter 8 bit plaintext (eg. 10100101) - "))) #8 bit plaintext + if len(P) != 8: + print "Enter only 8 bit plaintext!" + sys.exit(0) + + K = map(int, list(raw_input("Enter 10 bit key (eg. 1110000110) - "))) #10 bit key + if len(K) != 10: + print "Enter only 10 bit key!" + sys.exit(0) + + E = encrypt(P, K) + print "Encrypted - ", ''.join(map(str, E)) + ## + ##Decrypt case + if case == 2: + C = map(int, list(raw_input("Enter 8 bit ciphertext (eg. 10100101) - "))) #8 bit ciphertext + if len(C) != 8: + print "Enter only 8 bit ciphertext!" + sys.exit(0) + + K = map(int, list(raw_input("Enter 10 bit key (eg. 1110000110) - "))) #10 bit key + if len(K) != 10: + print "Enter only 10 bit key!" + sys.exit(0) + + D = decrypt(C, K) + print "Decrypted - ", ''.join(map(str, D)) + ## + print '-'*40 + +if __name__ == '__main__': + main() diff --git a/Encryption-Techniques/Hill-Cipher/demo.png b/Encryption-Techniques/Hill-Cipher/demo.png new file mode 100755 index 0000000..ebd5d50 Binary files /dev/null and b/Encryption-Techniques/Hill-Cipher/demo.png differ diff --git a/Encryption-Techniques/Hill-Cipher/hill.py b/Encryption-Techniques/Hill-Cipher/hill.py new file mode 100755 index 0000000..729d382 --- /dev/null +++ b/Encryption-Techniques/Hill-Cipher/hill.py @@ -0,0 +1,164 @@ +#!/usr/bin/env python +# Disclaimer : Matrix inverse code -> https://stackoverflow.com/a/39881366/5394031 +# Reference : https://en.wikipedia.org/wiki/Hill_cipher +# keysize = 9 | key_matrix = 3 x 3 + +import sys + +#alphabets = list('abcdefghijklmnopqrstuvwxyz') +alphabets = list('abcdefghijklmnopqrstuvwxyz.,!') +punctuation = '"#$%&\'()*+-/:;<=>?@[\\]^_`{|}~' +len_alpha = len(alphabets) + +def remove_punctuation(message): + message = message.translate(None, punctuation); #removing "#$%&\'()*+-/:;<=>?@[\\]^_`{|}~ + message = ''.join(message.split()) #removing tabs, spaces, newlines + return message.lower() + +def padthemessage(message): + if(len(message)%3 == 0): + return message + else: + short = 3 - len(message)%3 + for i in range(short): + message += 'x' #appending x to make message length a multiple of 3 + return message + +def display_format(message): + ''' Print the final encoded message in blocks of five letters and ten blocks per line. + The last line may be shorter than five blocks, and the last block may be shorter + than five letters. ''' + output = [] + enc = "" + while message: + output.append(message[:5]) + message = message[5:] + for index, block in enumerate(output): + if((index + 1) % 10 == 0): + enc = enc + block + "\n" + else: + enc = enc + block + " " + return enc + +def modular_multiplicative_inverse(number): + for i in range(len_alpha): + if (number*i)%len_alpha == 1: + return i + return 0 + +def matrix_transpose(matrix): + return [list(x) for x in zip(*matrix)] + +def matrix_minor(matrix,i,j): + return [row[:j] + row[j+1:] for row in (matrix[:i]+matrix[i+1:])] + +def matrix_determinant(matrix): + #base case for 2x2 matrix + if len(matrix) == 2: + return matrix[0][0]*matrix[1][1]-matrix[0][1]*matrix[1][0] + + determinant = 0 + for c in range(len(matrix)): + determinant += ((-1)**c)*matrix[0][c]*matrix_determinant(matrix_minor(matrix,0,c)) + return determinant + +def key_matrix_inverse(matrix): + determinant = matrix_determinant(matrix) + #find matrix of cofactors + cofactors = [] + for r in range(len(matrix)): + cofactorRow = [] + for c in range(len(matrix)): + minor = matrix_minor(matrix,r,c) + cofactorRow.append(((-1)**(r+c)) * matrix_determinant(minor)) + cofactors.append(cofactorRow) + cofactors = matrix_transpose(cofactors) + determinant = modular_multiplicative_inverse(determinant) # why do you need modular_multiplicative_inverse? Answer : https://wikipedia.org/wiki/Hill_cipher + for r in range(len(cofactors)): + for c in range(len(cofactors)): + cofactors[r][c] = (cofactors[r][c]*determinant)%len_alpha + return cofactors + +def matrix_multiply(matrix_1, matrix_2): + if(len(matrix_1[0]) == len(matrix_2)): ##Condition for matrix multiplication. + rows = len(matrix_1) + cols = len(matrix_2[0]) + cipher_matrix = [[0]]*rows + for i in range(rows): + row = [] + for j in range(cols): + res = 0 + for k in range(len(matrix_2)): + res += (matrix_1[i][k] * matrix_2[k][j]) + row.append(alphabets[res%len_alpha]) + cipher_matrix[i] = row + cipher_matrix = matrix_transpose(cipher_matrix) ## transpose of a matrix + return cipher_matrix + else: + print "Matrices cannnot be multiplied..." + sys.exit(0); + +def generate_matrix(content, rows, cols): + matrix = [[0]]*rows #initializing a matrix with zeros + k = 0 + for i in range(rows): + row = [] + for j in range(cols): + row.append(alphabets.index(content[k])) + k+=1 + matrix[i] = row + return matrix + +def encrypt(message, key): + key_matrix = generate_matrix(key, 3, 3) + if(matrix_determinant(key_matrix) == 0): + print "the determinant of your key is 0. you won't be able to decrypt. use another key." # determinant = 0 means inverse doesn't exist # Since inverse doesn't exist no decryption + sys.exit(0) + message_matrix = generate_matrix(message, len(message)/3 , 3) + message_matrix = matrix_transpose(message_matrix) ## transpose of a matrix + cipher_matrix = matrix_multiply(key_matrix, message_matrix) + ciphertext = ''.join([ch for row in cipher_matrix for ch in row]) + ciphertext = display_format(ciphertext) + return ciphertext + +def decrypt(ciphertext, key): + key_matrix = generate_matrix(key, 3, 3) + key_matrix = key_matrix_inverse(key_matrix) + cipher_matrix = generate_matrix(ciphertext, len(ciphertext)/3 , 3) + cipher_matrix = matrix_transpose(cipher_matrix) ## transpose of a matrix + message_matrix = matrix_multiply(key_matrix, cipher_matrix) + plaintext = ''.join([ch for row in message_matrix for ch in row]) + return plaintext + +def main(): + try: + case = int(raw_input("1 to encrypt plain_text\n2 to decrypt ciphertext\nEnter option : ")) + if(case == 1): + k = raw_input("Enter key (exactly 9 chars): ") + if(len(k) != 9): + print("Enter key of size 9 only...") + sys.exit(0) + message = padthemessage(remove_punctuation(raw_input("Message[Plain Text]: "))) + encrypted_message = encrypt(message, k) + print "Encrypted Message..." + print encrypted_message + sys.exit(0) + if(case == 2): + k = raw_input("Enter key (exactly 9 chars): ") + if(len(k) != 9): + print("Enter key of size 9 only...") + sys.exit(0) + encrypted_message = remove_punctuation(raw_input("Ciphertext: ")) + plain_text = decrypt(encrypted_message, k) + print "Decrypted Message..." + print plain_text + sys.exit(0) + else: + print "Wrong option" + sys.exit(0) + except KeyboardInterrupt: + print "\nClosing the program..." + sys.exit(0) + +if __name__ == '__main__': + main() diff --git a/Encryption-Techniques/IDEA/demo.png b/Encryption-Techniques/IDEA/demo.png new file mode 100755 index 0000000..814585d Binary files /dev/null and b/Encryption-Techniques/IDEA/demo.png differ diff --git a/Encryption-Techniques/IDEA/key b/Encryption-Techniques/IDEA/key new file mode 100755 index 0000000..f0681ee --- /dev/null +++ b/Encryption-Techniques/IDEA/key @@ -0,0 +1 @@ +11011100011011110011111101011001 \ No newline at end of file diff --git a/Encryption-Techniques/IDEA/key&plaintext.png b/Encryption-Techniques/IDEA/key&plaintext.png new file mode 100755 index 0000000..72fc149 Binary files /dev/null and b/Encryption-Techniques/IDEA/key&plaintext.png differ diff --git a/Encryption-Techniques/IDEA/plaintext b/Encryption-Techniques/IDEA/plaintext new file mode 100755 index 0000000..060c39e --- /dev/null +++ b/Encryption-Techniques/IDEA/plaintext @@ -0,0 +1 @@ +1001110010101100 \ No newline at end of file diff --git a/Encryption-Techniques/IDEA/s_idea.py b/Encryption-Techniques/IDEA/s_idea.py new file mode 100755 index 0000000..ad80d29 --- /dev/null +++ b/Encryption-Techniques/IDEA/s_idea.py @@ -0,0 +1,153 @@ +#!/usr/bin/env python + +# +# Copyright (c) 2017 by Aditya Malu adityamalu1@gmail.com. All Rights Reserved. +# + +import sys + +bin_to_int = {'0000':0, '0001':1, '0010':2, '0011':3, '0100':4, '0101':5, '0110':6 , '0111':7, '1000':8, '1001':9, '1010':10, '1011':11, + '1100':12, '1101':13, '1110':14, '1111':15 } +int_to_bin = {0:'0000', 1:'0001', 2:'0010', 3:'0011', 4:'0100', 5:'0101', 6:'0110' , 7:'0111', 8:'1000', 9:'1001', 10:'1010', 11:'1011', + 12:'1100', 13:'1101', 14:'1110', 15:'1111' } +add_inv = {'0000':'0000', '0001':'1111', '0010':'1110', '0011':'1101', '0100':'1100', '0101':'1011', '0110':'1010' , '0111':'1001', + '1000':'1000', '1001':'0111', '1010':'0110', '1011':'0101', '1100':'0100', '1101':'0011', '1110':'0010', '1111':'0001' } +mul_inv = {'0000':'0000', '0001':'0001', '0010':'1001', '0011':'0110', '0100':'1101', '0101':'0111', '0110':'0011' , '0111':'0101', + '1000':'1111', '1001':'0010', '1010':'1100', '1011':'1110', '1100':'1010', '1101':'0100', '1110':'1011', '1111':'1000' } + +def mul_mod_17(a1, a2): + '''The 16 nibbles are thought of as 0001, ..., 1111, 0000, which represent 1,..., 15, 16, for multiplication modulo 17. + Notice that 0000, which is 16, is congruent to -1 modulo 17. 0000 is its own inverse under multiplication modulo 17''' + if a1 == 0: + a1 = 16 + a3 = (a1 * a2) % 17 + if a3 == 16: + a3 = 0 + return a3 + +def readfile(filename): + try: + fp = open(filename, "r") + content = fp.read() + return content + except IOError: + print "Error opening file:", filename + sys.exit(1) + +def generate_encryption_keys(key): + key_format = [] + last_round = [] + shift_key_1 = key[6:] + key[:6] + shift_key_2 = shift_key_1[6:] + shift_key_1[:6] + shift_key_3 = shift_key_2[6:] + shift_key_2[:6] + long_key = key + shift_key_1 + shift_key_2 + shift_key_3[:16] + for i in range(4): + round_i = [] + for j in range(6): + round_i.append(long_key[:4]) + long_key = long_key[4:] + key_format.append(round_i) + for k in range(4): + last_round.append(long_key[:4]) + long_key = long_key[4:] + key_format.append(last_round) + return key_format + +def generate_decryption_keys(key): + ''' Bad function. Bad coding. Hard Coded.''' + en_keys = generate_encryption_keys(key) + de_keys, round_1, round_2, round_3, round_4, round_5 = [], [], [], [], [], [] + round_1.append(mul_inv[en_keys[4][0]]) + round_1.append(add_inv[en_keys[4][1]]) + round_1.append(add_inv[en_keys[4][2]]) + round_1.append(mul_inv[en_keys[4][3]]) + round_1.append(en_keys[3][4]) + round_1.append(en_keys[3][5]) + de_keys.append(round_1) + round_2.append(mul_inv[en_keys[3][0]]) + round_2.append(add_inv[en_keys[3][1]]) + round_2.append(add_inv[en_keys[3][2]]) + round_2.append(mul_inv[en_keys[3][3]]) + round_2.append(en_keys[2][4]) + round_2.append(en_keys[2][5]) + de_keys.append(round_2) + round_3.append(mul_inv[en_keys[2][0]]) + round_3.append(add_inv[en_keys[2][1]]) + round_3.append(add_inv[en_keys[2][2]]) + round_3.append(mul_inv[en_keys[2][3]]) + round_3.append(en_keys[1][4]) + round_3.append(en_keys[1][5]) + de_keys.append(round_3) + round_4.append(mul_inv[en_keys[1][0]]) + round_4.append(add_inv[en_keys[1][1]]) + round_4.append(add_inv[en_keys[1][2]]) + round_4.append(mul_inv[en_keys[1][3]]) + round_4.append(en_keys[0][4]) + round_4.append(en_keys[0][5]) + de_keys.append(round_4) + round_5.append(mul_inv[en_keys[0][0]]) + round_5.append(add_inv[en_keys[0][1]]) + round_5.append(add_inv[en_keys[0][2]]) + round_5.append(mul_inv[en_keys[0][3]]) + de_keys.append(round_5) + return de_keys + +def do_operations(s, round_i): + k1, k2, k3, k4, k5, k6 = bin_to_int[round_i[0]], bin_to_int[round_i[1]], bin_to_int[round_i[2]], bin_to_int[round_i[3]], bin_to_int[round_i[4]], bin_to_int[round_i[5]] + s1, s2, s3, s4 = bin_to_int[s[0]], bin_to_int[s[1]], bin_to_int[s[2]], bin_to_int[s[3]] + a1 = mul_mod_17(s1, k1) + a2 = (s2 + k2) % 16 + a3 = (s3 + k3) % 16 + a4 = mul_mod_17(s4, k4) + a5 = a1 ^ a3 + a6 = a2 ^ a4 + a7 = mul_mod_17(a5, k5) + a8 = (a6 + a7) % 16 + a9 = mul_mod_17(a8, k6) + a10 = (a7 + a9) % 16 + a11 = a1 ^ a9 + a12 = a3 ^ a9 + a13 = a2 ^ a10 + a14 = a4 ^ a10 + s1, s2, s3, s4 = int_to_bin[a11], int_to_bin[a13], int_to_bin[a12], int_to_bin[a14] + return [s1] + [s2] + [s3] + [s4] + +def do_last_round(s, last_round): + k1, k2, k3, k4 = bin_to_int[last_round[0]], bin_to_int[last_round[1]], bin_to_int[last_round[2]], bin_to_int[last_round[3]] + s1, s2, s3, s4 = bin_to_int[s[0]], bin_to_int[s[1]], bin_to_int[s[2]], bin_to_int[s[3]] + v1 = mul_mod_17(s1, k1) + v2 = (s2 + k2) % 16 + v3 = (s3 + k3) % 16 + v4 = mul_mod_17(s4, k4) + return int_to_bin[v1] + int_to_bin[v2] + int_to_bin[v3] + int_to_bin[v4] + +def encrypt_decrypt(text, keys): + s1, s2, s3, s4 = text[:4], text[4:8], text[8:12], text[12:16] + s = [s1] + [s2] + [s3] + [s4] + for r in range(len(keys)-1): + s = do_operations(s, keys[r]) + last_round = keys[len(keys)-1] + ctext = do_last_round(s, last_round) + return ctext + +def main(): + print '-'*40 + case = int(raw_input("Option 1 to encrypt\nOption 2 to decrypt \nEnter option : ")) + ## Encrypt case + if case == 1: + key = readfile(raw_input("Enter key file: ")) + keys = generate_encryption_keys(key) + plaintext = readfile(raw_input("Enter plaintext file: ")) + encrypted = encrypt_decrypt(plaintext, keys) + print "Encrypted: ", encrypted + ## Decrypt case + if case == 2: + key = readfile(raw_input("Enter key file: ")) + de_keys = generate_decryption_keys(key) + ciphertext = readfile(raw_input("Enter ciphertext file: ")) + detext = encrypt_decrypt(ciphertext, de_keys) + print "Decrypted: ", detext + print '-'*40 + +if __name__ == '__main__': + main() diff --git a/Encryption-Techniques/README.md b/Encryption-Techniques/README.md new file mode 100755 index 0000000..b8b842f --- /dev/null +++ b/Encryption-Techniques/README.md @@ -0,0 +1,17 @@ +### Encryption Techniques +* [Caesar Cipher](https://en.wikipedia.org/wiki/Caesar_cipher) + * [Code](Caesar-Cipher/) +* [Hill Cipher](https://en.wikipedia.org/wiki/Hill_cipher) + * [Code](Hill-Cipher/) +* [Transposition Cipher](https://en.wikipedia.org/wiki/Transposition_cipher) + * [Code](Transposition-Cipher/) +* [Data Encryption Standard (DES)](https://en.wikipedia.org/wiki/Data_Encryption_Standard) + * This is an implementation of [Simplified DES](https://www.cs.uri.edu/cryptography/dessimplified.htm). + * [Code](DES/) +* [Advanced Encryption Standard (AES)](https://en.wikipedia.org/wiki/Advanced_Encryption_Standard) + * [Code](AES/) +* [International Data Encryption Algorithm (IDEA)](https://en.wikipedia.org/wiki/International_Data_Encryption_Algorithm) + * This is an implementation of [Simplified IDEA](http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.501.2662&rep=rep1&type=pdf). + * [Code](IDEA/) +* [RSA (cryptosystem)](https://en.wikipedia.org/wiki/RSA_(cryptosystem)) + * [Code](RSA/) \ No newline at end of file diff --git a/Encryption-Techniques/RSA/demo.png b/Encryption-Techniques/RSA/demo.png new file mode 100755 index 0000000..24c9e3d Binary files /dev/null and b/Encryption-Techniques/RSA/demo.png differ diff --git a/Encryption-Techniques/RSA/rsa.py b/Encryption-Techniques/RSA/rsa.py new file mode 100755 index 0000000..4b9cb55 --- /dev/null +++ b/Encryption-Techniques/RSA/rsa.py @@ -0,0 +1,114 @@ +#!/usr/bin/env python +# +# Copyright (c) 2017 by Aditya Malu adityamalu1@gmail.com. All Rights Reserved. +# + +## Sample Program to show the working of RSA cryptosystem ## + +from random import * +import json +import sys + +max_of_p_q = 10000 + +def is_prime(num): + """Returns True if the number is prime + else False.""" + if num == 0 or num == 1: + return False + for x in range(2, num//2): + if num % x == 0: + return False + else: + return True + +def gcd(a, b): + """Calculate the Greatest Common Divisor of a and b. + Unless b==0, the result will have the same sign as b (so that when + b is divided by it, the result comes out positive). + """ + while b: + a, b = b, a%b + return a + +def lcm(a, b): + return (a*b) / gcd(a, b) + +def is_coprime(a, b): + if gcd(a, b) == 1: + return True + else: + return False + +def mod_mul_inv(a, num): + ''' This is a simple method. Extended Euclidean Algorithm is recommended for + modular multiplicative inverse''' + for i in xrange(num): + if (a*i)%num == 1: + return i + return 0 + +def generate_keys(): + p, q = 1, 1 + while(not is_prime(p)): + p = randint(1, max_of_p_q) + while(not is_prime(q) and p!=q): + q = randint(1, max_of_p_q) + n = p * q + lambda_n = lcm(p-1, q-1) + e = randint(1, lambda_n) + while(not is_coprime(e, lambda_n)): + e = randint(1, lambda_n) + d = mod_mul_inv(e, lambda_n) + public_key = (e, n) + private_key = (d, n) + return public_key, private_key + +def encrypt(public_key, m): + e = public_key[0] + n = public_key[1] + c = pow(m, e, n) + return c + +def decrypt(private_key, ciphertext): + d = private_key[0] + n = private_key[1] + plaintext = pow(ciphertext, d, n) + return plaintext + +def main(): + case = int(raw_input("1 to generate keys and save in file\n2 to encrypt\n3 to decrypt\n")) + if case == 1: + public_key, private_key = generate_keys() + print "Public Key (e, n) : ", public_key + print "Private Key (d, n) : ", private_key + pubkey = {} + pubkey["e"] = public_key[0] + pubkey["n"] = public_key[1] + with open("public_key.json", "w") as pubkeyfile: + json.dump(pubkey, pubkeyfile) + prikey = {} + prikey["d"] = private_key[0] + prikey["n"] = private_key[1] + with open("private_key.json", "w") as prikeyfile: + json.dump(prikey, prikeyfile) + elif case == 2: + m = int(raw_input("Enter message num: ")) + with open("public_key.json", "r") as pubkeyfile: + pubkey = json.load(pubkeyfile) + public_key = (pubkey["e"], pubkey["n"]) + ciphertext = encrypt(public_key, m) + print "Encrypted: ", ciphertext + elif case == 3: + ciphertext = int(raw_input("Enter Encrypted num: ")) + with open("private_key.json", "r") as prikeyfile: + prikey = json.load(prikeyfile) + private_key = (prikey["d"], prikey["n"]) + plaintext = decrypt(private_key, ciphertext) + print "Decrypted: ", plaintext + else: + print "Invalid option\n" + sys.exit(0) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/Encryption-Techniques/Transposition-Cipher/demo.png b/Encryption-Techniques/Transposition-Cipher/demo.png new file mode 100644 index 0000000..dc26adc Binary files /dev/null and b/Encryption-Techniques/Transposition-Cipher/demo.png differ diff --git a/Encryption-Techniques/Transposition-Cipher/trans.py b/Encryption-Techniques/Transposition-Cipher/trans.py new file mode 100755 index 0000000..70e7e24 --- /dev/null +++ b/Encryption-Techniques/Transposition-Cipher/trans.py @@ -0,0 +1,98 @@ +#!/usr/bin/env python +import sys +alphabets = list('abcdefghijklmnopqrstuvwxyz.,!') +def col_wise(matrix,i): # access the matrix column wise + return [row[i] for row in matrix] + +def creat_key_num(key): # function to create key numbering + l1 = list(key) + l1.sort() + l = {} + for i in range(len(key)): + l[key[i]] = l1.index(key[i]) + return l + +def generate_matrix(content, rows, cols): #function to generate matrix of given text + matrix = [[0]]*rows #initializing a matrix with zeros + k = 0 + for i in range(rows): + row = [] + for j in range(cols): + if(k != len(content)): + row.append(content[k]) #alphabets.index(content[k]) + k+=1 + else: + row.append(" ") + matrix[i] = row + return matrix + +def encrypt(message, key): #encryption function + cipher = '' + cols = len(key) + rows = len(message) / cols + if(len(message) % cols != 0): + rows = rows+1 + matrix = generate_matrix(message,rows,cols) + key_num = creat_key_num(key) + for i in range(len(key)): + j = key_num[key[i]] + cipher += ''.join(col_wise(matrix,j)) + return cipher + +def decrypt(ciphertext, key): #decryption function + temp = {} + cipher = [] + dicipher = '' + cols = len(key) + rows = len(ciphertext) / cols + if(len(ciphertext) % cols != 0): + rows = rows+1 + matrix = generate_matrix(ciphertext,cols,rows) + for i in range(len(key)): + temp[key[i]] = matrix[i] + for i in sorted(temp): + cipher.append(temp[i]) + for i in range(rows): + dicipher += ''.join(col_wise(cipher,i)) + return dicipher + +def main(): + try: + case = int(raw_input("1 to encrypt plain_text\n2 to decrypt ciphertext\nEnter option : ")) + if(case == 1): + k = raw_input("Enter key: ") + if(len(k) == 0): + print("Enter key atleast of size 1...") + sys.exit(0) + + message = raw_input("Message[Plain Text]: ") + + print "Key numbering: ", + key_num = creat_key_num(k) + + for i in list(k): + print key_num[i], + print ' ' + encrypted_message = encrypt(message, k) + print "Encrypted Message :" + print encrypted_message + sys.exit(0) + if(case == 2): + k = raw_input("Enter key: ") + if(len(k) == 0): + print("Enter key atleast of size 1...") + sys.exit(0) + encrypted_message = raw_input("Ciphertext: ") + plain_text = decrypt(encrypted_message, k) + print "Decrypted Message :" + print plain_text + sys.exit(0) + else: + print "Wrong option" + sys.exit(0) + except KeyboardInterrupt: + print "\nClosing the program..." + sys.exit(0) + +if __name__ == '__main__': + main() diff --git a/README.md b/README.md index 4a47dc0..5874522 100644 --- a/README.md +++ b/README.md @@ -21,10 +21,14 @@ New to Python? Take a look [here](https://github.com/prateekiiest/Code-Sleep-Pyt * [Translations of Hamlet](https://github.com/prateekiiest/Code-Sleep-Python#translations-of-hamlet) * [Classification](https://github.com/prateekiiest/Code-Sleep-Python#classification) * [Whisky Classification](https://github.com/prateekiiest/Code-Sleep-Python#whisky-classification) +* [Bird Migration](https://github.com/prateekiiest/Code-Sleep-Python#whisky-classification) +* [Social Network Analysis](https://github.com/prateekiiest/Code-Sleep-Python#whisky-classification) * [Bird Migration](https://github.com/prateekiiest/Code-Sleep-Python#Bird_mirgation) * [Social Network Analysis](https://github.com/prateekiiest/Code-Sleep-Python#social_network) * [Prime](https://github.com/prateekiiest/Code-Sleep-Python#Prime) * [Website status check](https://github.com/prateekiiest/Code-Sleep-Python#website_status_check) +* [Encryption-Techniques](#Encryption-Techniques) + ---------------------------------- ### Tic-Tac-Toe @@ -97,6 +101,7 @@ Homophily is a network characteristic. Homophily occurs when nodes that share an ---------------------------------------------------- + ### Prime number finder The implementation of Sieve of Eratosthenes is used to find prime numbers. @@ -118,6 +123,15 @@ A simple website crawler to check the return code of a website. It returns with ---------------------------------------------------- +### Encryption-Techniques + +Encryption is an interesting piece of technology that works by scrambling data so it is unreadable by unintended parties. The technology comes in many forms, with key size and strength generally being the biggest differences in one variety from the next. This repo has implementations of different encryption techniques. More [here](https://en.wikipedia.org/wiki/Encryption). + +**[CODE](Encrytion-Techniques/)** + +---------------------------------------------------- + + ## Getting started with Python