Exercise No: 01 IT18612 – INFORMATION SECURITY LABORATORY Date: Register No: 180801069 Page No: CAESAR CIPHER AIM: To implement encryption using Caesar Cipher. To implement decryption using Caesar Cipher. To apply brute-force attack and to find the key for the given encrypted text. ALGORITHM: 1. Get the string and key value as the input. 2. Iterate this through the input text and apply the below formula for each alphabet in the text to obtain the ciphertext. E n = (x + k) mod 26 , where k represents key. 3. To decrypt, subtract the key from the given Cipher text to get the original text. D n = (x – k) mod 26 , where k represents key. 4. To calculate cipher key, find the difference in the ASCII codes of the first character of the encrypted message and the original message. If the difference is negative, add 26 to it. Exercise No: 01 IT18612 – INFORMATION SECURITY LABORATORY Date: Register No: 180801069 Page No: SOURCE CODE: def encryptVignereCipher(msg, key): encryptText = "" msgLen = len(msg) for i in range(msgLen): ch = msg[i] keyVal = int(key[i]) % 26 if ch.isupper(): ch = chr(ord(ch) + keyVal) if ch > "Z": ch = chr(ord(ch) - ord("Z") + ord("A") - 1) elif ch.islower(): ch = chr(ord(ch) + keyVal) if ch > "z": ch = chr(ord(ch) - ord("z") + ord("a") - 1) encryptText += ch return encryptText def decryptVignereCipher(msg, key): decryptText = "" msgLen = len(msg) for i in range(msgLen): ch = msg[i] keyVal = int(key[i]) % 26 if ch.isupper(): ch = chr(ord(ch) - keyVal) if ch < "A": ch = chr(ord(ch) + ord("Z") - ord("A") + 1) elif ch.islower(): ch = chr(ord(ch) - keyVal) if ch < "a": ch = chr(ord(ch) + ord("z") - ord("a") + 1) decryptText += ch return decryptText Exercise No: 01 IT18612 – INFORMATION SECURITY LABORATORY Date: Register No: 180801069 Page No: def cipherKey(s1, s2): cipherKey = "" strLen = len(s1) for i in range(strLen): keyVal = ord(s2[i]) - ord(s1[i]) if keyVal < 0: keyVal = 26 + keyVal cipherKey += str(keyVal) return cipherKey str1 = input("\nEnter Message To Encrypt : ") key1 = input("Enter A Key : ") print("The encrypted text is : ", encryptVignereCipher(str1, key1)) str2 = input("\nEnter Message To Decrypt : ") key2 = input("Enter A Key : ") print("The decrypted text is : ", decryptVignereCipher(str2, key2)) originalText = input("\nEnter Original Text : ") encryptText = input("Enter Encrypted Text : ") print("The cipher key is : ", cipherKey(originalText, encryptText)) SAMPLE INPUT: Enter Message To Encrypt : nileshD Enter A Key : 3 Enter Message To Decrypt : qlohvkG Enter A Key : 3 Enter Original Text : nileshD Enter Encrypted Text : qlohvkG SAMPLE OUTPUT: The encrypted text is : qlohvkG The decrypted text is : nileshD The cipher key is : 3 RESULT : The program to implement the Caesar Cipher encryption algorithm, decryption algorithm and brute-force attack was successfully executed. Exercise No: 02 IT18612 – INFORMATION SECURITY LABORATORY Date: Register No: 180801069 Page No: PLAYFAIR CIPHER – MATRIX GENERATION AIM: To write a program to generate a key matrix for the Playfair Cipher. ALGORITHM: 1. Convert all characters in the key to uppercase. 2. Replace all occurrences of ‘J’ with ‘I’. 3. Iterate over the cipher key and insert all non-duplicate characters of the string into the matrix. 4. Iterate over the alphabets from ‘A’ to ‘Z’ and insert them into the matrix if they are not already present in the matrix. 5. When you encounter the alphabet ‘J’, insert ‘I’ into the matrix instead. 6. Return the keyMatrix. 7. Stop Exercise No: 02 IT18612 – INFORMATION SECURITY LABORATORY Date: Register No: 180801069 Page No: SOURCE CODE: def matrixGeneration(key: str): key = key.upper().replace(" ", "") res = [] for ch in key: if ch not in res: if ch == "J": res.append("I") else: res.append(ch) flag = 0 for i in range(ord("A"), ord("Z") + 1): if chr(i) not in res: if chr(i) == "J" and "I" not in res: res.append("I") flag = 1 elif flag == 0 and chr(i) == "I" or chr(i) == "J": pass else: res.append(chr(i)) k = 0 rows, cols = (5, 5) keyMatrix = [[0 for j in range(cols)] for i in range(rows)] for i in range(rows): for j in range(cols): keyMatrix[i][j] = res[k] k += 1 return keyMatrix key = input("\nEnter Cipher Key : ") keyMatrix = matrixGeneration(key) for i in range(5): print() for j in range(5): print(keyMatrix[i][j], end=" ") SAMPLE INPUT: Enter Cipher Key : PROGRAMMING SAMPLE OUTPUT: P R O G A M I N B C D E F H K L Q S T U V W X Y Z RESULT : The programs to generate the matrix for Playfair cipher for the given key, encryption algorithm and decryption algorithm were successfully implemented. Exercise No: 03 IT18612 – INFORMATION SECURITY LABORATORY Date: Register No: 180801069 Page No: PLAYFAIR CIPHER – ENCRYPTION AND DECRYPTION AIM: To encrypt the given plain text with the given key using Playfair cipher. To decrypt the given encrypted text with the given key using Playfair cipher. ALGORITHM: 1. Read the plain text. 2. The plaintext is split into pairs of two letters (digraphs). A pair cannot be made with same letter. Break the letter in single and add a ‘X’ to the previous letter. 3. If there is an odd number of letters, a ‘X’ is added to the last letter. 4. These digraphs will be substituted using the key matrix. 5. The substitution process uses the following encryption rules: - a. If both the letters are in the same column, take the letter below each one (going back to the top if at the bottom). b. If both letters are in the same row, take the letter to the right of each one (going back to the left if at the farthest right). c. If neither of the preceding two rules are true, form a rectangle with the two letters and take the letters on the horizontal opposite corner of the rectangle. 6. Display the encrypted message generated using the key matrix. 7. Reverse of encryption is decryption. Exercise No: 03 IT18612 – INFORMATION SECURITY LABORATORY Date: Register No: 180801069 Page No: SOURCE CODE: def findInMatrix(ch, keyMatrix): # Replace the 'J' with 'I' if ch == "J": ch = "I" for i, j in enumerate(keyMatrix): for k, l in enumerate(j): if ch == l: return i, k def encrypt(msg: str, keyMatrix) -> str: msg = msg.upper() msg = msg.replace(" ", "") msgLen = len(msg) encryptStr = "" for s in range(msgLen): if s < msgLen - 1: if msg[s] == msg[s + 1]: msg = msg[: s + 1] + "X" + msg[s + 1 :] if msgLen % 2 != 0: msg = msg[:] + "X" for i in range(0, msgLen, 2): (w, x) = findInMatrix(msg[i], keyMatrix) (y, z) = findInMatrix(msg[i + 1], keyMatrix) if w == y: x = (x + 1) % 5 z = (z + 1) % 5 encryptStr += keyMatrix[w][x] encryptStr += keyMatrix[y][z] elif x == z: w = (w + 1) % 5 y = (y + 1) % 5 encryptStr += keyMatrix[w][x] encryptStr += keyMatrix[y][z] else: encryptStr += keyMatrix[w][z] encryptStr += keyMatrix[y][x] i += 2 return encryptStr def decrypt(msg: str, keyMatrix) -> str: msg = msg.upper() msg = msg.replace(" ", "") msgLen = len(msg) decryptStr = "" for i in range(0, msgLen, 2): (w, x) = findInMatrix(msg[i], keyMatrix) (y, z) = findInMatrix(msg[i + 1], keyMatrix) Exercise No: 03 IT18612 – INFORMATION SECURITY LABORATORY Date: Register No: 180801069 Page No: if w == y: x = ((x - 1) % 5 + 5) % 5 z = ((z - 1) % 5 + 5) % 5 decryptStr += keyMatrix[w][x] decryptStr += keyMatrix[y][z] elif x == z: w = ((w - 1) % 5 + 5) % 5 y = ((y - 1) % 5 + 5) % 5 decryptStr += keyMatrix[w][x] decryptStr += keyMatrix[y][z] else: decryptStr += keyMatrix[w][z] decryptStr += keyMatrix[y][x] i += 2 return decryptStr while True: print("\n1. Playfair Cipher Encryption") print("2. Playfair Cipher Decryption") print("3. Exit") choice = int(input("Enter your choice : ")) if choice == 1: str1 = input("\nEnter Message To Encrypt : ") print("Enter Cipher Key : ") keyMatrix = [] for i in range(5): row = input().split(' ') keyMatrix.append(row) print("The encrypted text is : ", encrypt(str1, keyMatrix)) elif choice == 2: str2 = input("\nEnter Message To Decrypt : ") print("Enter Cipher Key : ") keyMatrix = [] for i in range(5): row = input().split(' ') keyMatrix.append(row) print( "The decrypted text is : ", decrypt(str2, keyMatrix), "(Ignore Unnecessary X)", ) else: break Exercise No: 03 IT18612 – INFORMATION SECURITY LABORATORY Date: Register No: 180801069 Page No: SAMPLE INPUT: 1. Playfair Cipher Encryption 2. Playfair Cipher Decryption 3. Exit Enter your choice : 1 Enter Message To Encrypt : CORONAVIRUS Enter Cipher Key : S V C E A B D F G H I K L M N O P Q R T U W X Y Z Enter your choice : 2 Enter Message To Decrypt : SQTPTHSKOYCU Enter Cipher Key : S V C E A B D F G H I K L M N O P Q R T U W X Y Z Enter your choice : 3 SAMPLE OUTPUT: The encrypted text is : SQTPTHSKOYCU The decrypted text is : CORONAVIRUSX (Ignore Unnecessary X) RESULT : The Python program to implement encryption and decryption using Playfair cipher was successfully executed. Exercise No: 04 IT18612 – INFORMATION SECURITY LABORATORY Date: Register No: 180801069 Page No: RAIL FENCE CIPHER AIM: To implement encryption using Rail Fence Cipher. To implement decryption using Rail Fence Cipher. To apply brute-force attack and to find the key for the given encrypted text. ALGORITHM: encryptRailFence(str, key) 1. Populate a matrix with the number of rows as the cipher key and the number of columns as the length of the text with the ‘ \ n’ character. 2. Iterate over each character of the string. 3. If we have just filled the top or bottom rail, change the direction 4. Populate rail[row][col] with ch. 5. Increment row and col according to the direction. decryptRailFence(str, key) 1. Populate a matrix with the number of rows as the cipher key and the number of columns as the length of the text with the ‘ \ n’ character. 2. Iterate from 0 till the length of the string. 3. Change the direction appropriately when you hit the top or bottom of the rail 4. Mark the place where we have to place the text with ‘*’ 5. Increment row and col according to the direction. 6. Iterate over the matrix populated with ‘*’ and fill it with the characters of the encrypted cipher. 7. Iterate over the matrix in a zigzag manner to obtain the encrypted text. cipherKey(str1, str2) 1. Brute-force the encrypted string by trying to decrypt it with a key in range of 2 to N, where N is the length of the decrypted string. 2. If the decrypted string matches the original string, that integer is the correct key. Exercise No: 04 IT18612 – INFORMATION SECURITY LABORATORY Date: Register No: 180801069 Page No: SOURCE CODE: def encryptRailFence(msg, key) -> str: rail = [["\n" for j in range(len(msg))] for i in range(key)] dir_down = False row, col = 0, 0 for ch in msg: if row == 0 or row == key - 1: dir_down = not dir_down rail[row][col] = ch col += 1 if dir_down: row += 1 else: row -= 1 encryptText = "" for i in range(key): for j in range(len(msg)): if rail[i][j] != "\n": encryptText += rail[i][j] return encryptText def decryptRailFence(msg, key) -> str: rail = [["\n" for j in range(len(msg))] for i in range(key)] dir_down = None row, col = 0, 0 for ch in msg: if row == 0: dir_down = True if row == key - 1: dir_down = False rail[row][col] = "*" col += 1 if dir_down: row += 1 else: row -= 1 Exercise No: 04 IT18612 – INFORMATION SECURITY LABORATORY Date: Register No: 180801069 Page No: index = 0 for i in range(key): for j in range(len(msg)): if rail[i][j] == "*" and index < len(msg): rail[i][j] = msg[index] index += 1 decryptText = "" row, col = 0, 0 for i in range(len(msg)): if row == 0: dir_down = True if row == key - 1: dir_down = False if rail[row][col] != "*": decryptText += rail[row][col] col += 1 if dir_down: row += 1 else: row -= 1 return decryptText def cipherKey(s1, s2): strLen = len(s2) for i in range(2, strLen + 1): if decryptRailFence(s2, i) == s1: return i return None str1 = input("\nEnter Message To Encrypt : ") key1 = int(input("Enter A Key : ")) print("The encrypted text is : ", encryptRailFence(str1, key1)) str2 = input("\nEnter Message To Decrypt : ") key2 = int(input("Enter A Key : ")) print("The decrypted text is : ", decryptRailFence(str2, key2)) originalText = input("\nEnter Original Text : ") encryptText = input("Enter Encrypted Text : ") print("The cipher key is : ", cipherKey(originalText, encryptText)) Exercise No: 04 IT18612 – INFORMATION SECURITY LABORATORY Date: Register No: 180801069 Page No: SAMPLE INPUT: Enter Message To Encrypt : CORONAVIRUS Enter A Key : 3 Enter Message To Decrypt : CNROOAIURVS Enter A Key : 3 Enter Original Text : CORONAVIRUS Enter Encrypted Text : CNROOAIURVS SAMPLE OUTPUT: The encrypted text is : CNROOAIURVS The decrypted text is : CORONAVIRUS The cipher key is : 3 RESULT : The program to implement the Rail Fence Cipher encryption algorithm, decryption algorithm and brute-force attack was successfully executed. Exercise No: 05 IT18612 – INFORMATION SECURITY LABORATORY Date: Register No: 180801069 Page No: VIGENÈRE CIPHER AIM: To implement encryption using Vigenère Cipher. To implement decryption using Vigenère Cipher. To apply brute-force attack and to find the key for the given encrypted text. ALGORITHM: 1. Get the string and key value as the input. Make sure the length of the string and key are same. 2. Iterate this through the input text and apply and apply the below formula for each alphabet in the text to obtain the ciphertext. E n = (s[i] + k[i]) mod 26 , where k[i] represents the key at the i th position. 3. To decrypt, subtract the key from the given Cipher text to get the original text. D n = (s[i] – k[i]) mod 26, where k[i] represents the key at the i th position. 4. To calculate cipher key, find the difference in the ASCII codes of each character of the encrypted message and the original message. If the difference is negative for a pair of characters, add 26 to it. Exercise No: 05 IT18612 – INFORMATION SECURITY LABORATORY Date: Register No: 180801069 Page No: SOURCE CODE: def encryptVignereCipher(msg, key): encryptText = "" msgLen = len(msg) for i in range(msgLen): ch = msg[i] keyVal = int(key[i]) % 26 if ch.isupper(): ch = chr(ord(ch) + keyVal) if ch > "Z": ch = chr(ord(ch) - ord("Z") + ord("A") - 1) elif ch.islower(): ch = chr(ord(ch) + keyVal) if ch > "z": ch = chr(ord(ch) - ord("z") + ord("a") - 1) encryptText += ch return encryptText def decryptVignereCipher(msg, key): decryptText = "" msgLen = len(msg) for i in range(msgLen): ch = msg[i] keyVal = int(key[i]) % 26 if ch.isupper(): ch = chr(ord(ch) - keyVal) if ch < "A": ch = chr(ord(ch) + ord("Z") - ord("A") + 1) elif ch.islower(): ch = chr(ord(ch) - keyVal) if ch < "a": ch = chr(ord(ch) + ord("z") - ord("a") + 1) decryptText += ch return decryptText Exercise No: 05 IT18612 – INFORMATION SECURITY LABORATORY Date: Register No: 180801069 Page No: def cipherKey(s1, s2): cipherKey = "" strLen = len(s1) for i in range(strLen): keyVal = ord(s2[i]) - ord(s1[i]) if keyVal < 0: keyVal = 26 + keyVal cipherKey += str(keyVal) return cipherKey str1 = input("\nEnter Message To Encrypt : ") key1 = input("Enter A Key : ") print("The encrypted text is : ", encryptVignereCipher(str1, key1)) str2 = input("\nEnter Message To Decrypt : ") key2 = input("Enter A Key : ") print("The decrypted text is : ", decryptVignereCipher(str2, key2)) originalText = input("\nEnter Original Text : ") encryptText = input("Enter Encrypted Text : ") print("The cipher key is : ", cipherKey(originalText, encryptText)) SAMPLE INPUT: Enter Message To Encrypt : nileshD Enter A Key : 2132789 Enter Message To Decrypt : pjogzpM Enter A Key : 2132789 Enter Original Text : nileshD Enter Encrypted Text : pjogzpM SAMPLE OUTPUT: The encrypted text is : pjogzpM The decrypted text is : nileshD The cipher key is : 2132789 RESULT : The program to implement the Vigenère Cipher encryption algorithm, decryption algorithm and brute-force attack was successfully executed. Exercise No: 06 IT18612 – INFORMATION SECURITY LABORATORY Date: Register No: 180801069 Page No: PASSWORD VALIDATION AIM: To write a program to check for the validity of a password. A password is valid if: - Password must have at least 6 characters and at most 8 characters. The password has at least one uppercase character. The password has at least one numeric character. The password has at least one special character. The password must not have duplicate characters. ALGORITHM: 1. Accept input for the password 2. Check if the length of the password in the range of 6<=len<=8. 3. Iterate over the password string and count the number of uppercase characters, special characters and numeric characters. 4. Calculate the frequency of each character in the string and check if any duplicates are present in the string. 5. Display appropriate error messages if any of the parameters is not satisfied 6. Return true if the password passes all the parameters 7. Stop Exercise No: 06 IT18612 – INFORMATION SECURITY LABORATORY Date: Register No: 180801069 Page No: SOURCE CODE: def passwordValidation(password: str): specialChars = ["!", "@", "#", "$", "%", "^", "&", "*"] flag = True if len(password) < 6 or len(password) > 8: print( "The length of the password must be atleast 6 characters and atmost 8 characters" ) flag = False if not any(char.isupper() for char in password): print("The password must have atleast one uppercase letter") flag = False if not any(char.isnumeric() for char in password): print("The password must have atleast one numeric character") flag = False if not any(char in specialChars for char in password): print("The password must have atleast one special character") flag = False if len(set(password)) < len(password): print("The password must not have duplicate characters") flag = False return flag password = input("\nEnter A Password : ") if passwordValidation(password): print("Valid Password!") else: print("Invalid Password!") Exercise No: 06 IT18612 – INFORMATION SECURITY LABORATORY Date: Register No: 180801069 Page No: SAMPLE INPUT: Enter A Password : test@123 Enter A Password : Testing123 Enter A Password : testtest Enter A Password : Test@123 SAMPLE OUTPUT: The password must have atleast one uppercase letter The password must not have duplicate characters Invalid Password! The length of the password must be atleast 6 characters and atmost 8 characters. The password must have atleast one special character. Invalid Password! The password must have atleast one uppercase letter The password must have atleast one numeric character The password must have atleast one special character The password must not have duplicate characters Invalid Password! Valid Password! RESULT : The program to check the validity of an entered password based on certain pre-defined parameters was successfully implemented. Exercise No: 07 IT18612 – INFORMATION SECURITY LABORATORY Date: Register No: 180801069 Page No: PASSWORD MANAGEMENT AND AUTHENTICATION AIM: To write a program to implement Password Management and authentication. A file should contain at least 10 user id and encrypted password (SHA 256) Provision must be given to add or update user-id and password. Give a message if user id and password is correct else give error message. ALGORITHM: addUser(userId, password) 1. Import the hashlib library. 2. Hash the password entered by the user using SHA256 algorithm. 3. Append to the text file. updateUser(userId, oldPassword, newPassword) 1. Hash oldPassword using SHA256 algorithm 2. Check if the hash of the old password matches the hash of the user’s existing password. 3. Hash the new password using SHA256 algorithm 4. Replace the old password with the new password. 5. Write to the text file. isValid(userId, password) 1. Get the hashed password of the entered userId from the text file. 2. Hash the password entered by the user using SHA256 algorithm. 3. Check if the hashed password matches the hashed password present in the file.