To all my viewers I'm currently a member of the internship program for a human welfare council and I need your help to assist the people who need us by donating any amount you can. YOUR SMALL GESTURE WILL HELP A FAMILY SMILE. Follow the link and you will get to our campaign page :
Please after funding whatever amount you can share me the screenshot of the receipt to my email - sumanmukherjee2001@gmail.com
So that I can send you a receipt for the contribution you made and tell the happiness you shared. 😊
The affine cipher is the multiplicative cipher, which is then encrypted by the Caesar cipher on top of that. The affine cipher needs two keys: one for the multiplicative cipher multiplication and the other for the Caesar cipher addition. For the affine cipher program, we will use a single integer for the key. We will use some simple math to split this key into the two keys, which we will call Key A and Key B.Its working needs to explained . If you want me to explain how it works ,let me know in the comments and I'll explain it .
First of all we need to make a python file and save it as cryptomath.We will need to call this later in our program.
now create a new folder and save the cryptomath and affine cipher there.
NOTE :- both the python file should be in the same folder or it would be diificult to call the crpytomath module later in our cipher.
SOURCE CODE (Cryptomath) :-
source- www.inventwithpython.com
-------------------------------------------------------------------------------------------------------------------------
# Cryptomath Module def gcd(a, b): # Return the GCD of a and b using Euclid's Algorithm while a != 0: a, b = b % a, a return b def findModInverse(a, m): # Returns the modular inverse of a % m, which is # the number x such that a*x % m = 1 if gcd(a, m) != 1: return None # no mod inverse if a & m aren't relatively prime # Calculate using the Extended Euclidean Algorithm: u1, u2, u3 = 1, 0, a v1, v2, v3 = 0, 1, m while v3 != 0: q = u3 // v3 # // is the integer division operator v1, v2, v3, u1, u2, u3 = (u1 - q * v1), (u2 - q * v2), (u3 - q * v3), v1, v2, v3 return u1 % m
--------------------------------------------------------------------------------------------------------------------------
After you've created the cryptomath, proceed to the cipher.
The source code for the same is below.
SOURCE CODE :-
--------------------------------------------------------------------------------------------------------------------------
import sys, pyperclip, cryptomath, random SYMBOLS = """ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~""" # note the space at the front def main(): myMessage = str(input("ENTER YOUR MESSAGE NOW")) myKey = int(input("ENTER YOUR KEY")) myMode = 'encrypt' # set to 'encrypt' or 'decrypt' if myMode == 'encrypt': translated = encryptMessage(myKey, myMessage) elif myMode == 'decrypt': translated = decryptMessage(myKey, myMessage) print('Key: %s' % (myKey)) print('%sed text:' % (myMode.title())) print(translated) pyperclip.copy(translated) print('Full %sed text copied to clipboard.' % (myMode)) def getKeyParts(key): keyA = key // len(SYMBOLS) keyB = key % len(SYMBOLS) return (keyA, keyB) def checkKeys(keyA, keyB, mode): if keyA == 1 and mode == 'encrypt': sys.exit('The affine cipher becomes incredibly weak when key A is set to 1. Choose a different key.') if keyB == 0 and mode == 'encrypt': sys.exit('The affine cipher becomes incredibly weak when key B is set to 0. Choose a different key.') if keyA < 0 or keyB < 0 or keyB > len(SYMBOLS) - 1: sys.exit('Key A must be greater than 0 and Key B must be between 0 and %s.' % (len(SYMBOLS) - 1)) if cryptomath.gcd(keyA, len(SYMBOLS)) != 1: sys.exit('Key A (%s) and the symbol set size (%s) are not relatively prime. Choose a different key.' % (keyA, len(SYMBOLS))) def encryptMessage(key, message): keyA, keyB = getKeyParts(key) checkKeys(keyA, keyB, 'encrypt') ciphertext = '' for symbol in message: if symbol in SYMBOLS: # encrypt this symbol symIndex = SYMBOLS.find(symbol) ciphertext += SYMBOLS[(symIndex * keyA + keyB) % len(SYMBOLS)] else: ciphertext += symbol # just append this symbol unencrypted return ciphertext def decryptMessage(key, message): keyA, keyB = getKeyParts(key) checkKeys(keyA, keyB, 'decrypt') plaintext = '' modInverseOfKeyA = cryptomath.findModInverse(keyA, len(SYMBOLS)) for symbol in message: if symbol in SYMBOLS: # decrypt this symbol symIndex = SYMBOLS.find(symbol) plaintext += SYMBOLS[(symIndex - keyB) * modInverseOfKeyA % len(SYMBOLS)] else: plaintext += symbol # just append this symbol undecrypted return plaintext def getRandomKey(): while True: keyA = random.randint(2, len(SYMBOLS)) keyB = random.randint(2, len(SYMBOLS)) if cryptomath.gcd(keyA, len(SYMBOLS)) == 1: return keyA * len(SYMBOLS) + keyB # If affineCipher.py is run (instead of imported as a module) call # the main() function. if __name__ == '__main__': main()
--------------------------------------------------------------------------------------------------------------------------
source- www.inventwithpython.com