adsense

Saturday, 13 August 2016

AFFINE CIPHER

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

Sunday, 7 August 2016

WRITING OUR FIRST PROGRAM


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. 😊



1 -   The first program in any programming language is always the "HELLO WORLD". So lets begin with this simple code :- 

---------------------------------------------------------------------------------
print ("HELLO WORLD")
---------------------------------------------------------------------------------
Thats it!!!

Now the function "print" is new here . Its function is to print something on the console. 

("HELLO WORLD")

As we know HELLO WORLD is a string hence we have to put it inside two quotes " ".

2 - A program that adds two number(according to the user)  :-


--------------------------------------------------------------------------------------------------------------------------

num1 = int(input("ENTER YOUR FIRST NUMBER"))
num2 = int(input("ENTER YOUR SECOND NUMBER"))

print (num1+num2)
--------------------------------------------------------------------------------------------------------------------------

Here a new function has been introduced INPUT. What it does is that it asks the user to enter their number and then it stores that number into the variable name "num1" or "num2". So whenever the variables are called , the input enterd by the user is taken and added.  Hence, we use input whenever we need to take the data enterd by the user.

Next , inside the print function, we just add the two variables by putting  a "+" sign between them.


Keep visiting for more tutorials.