Showing posts with label System Security. Show all posts
Showing posts with label System Security. Show all posts

Friday, April 26, 2019

Encryption and decryption using Hill Cipher Technique - System Security | Python3

import numpy as np
from itertools import islice, chain, repeat
list2 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
         'w', 'x', 'y','z']
l3=[]
l4=[]
lis4=[]
def chunk_pad(it, size, padval=23):
    it = chain(iter(it), repeat(padval))
    return iter(lambda: tuple(islice(it, size)), (padval,) * size)

#Key-matrix = a
a=[[6,24,1],[13,16,10],[20,17,15]]
x = np.array(a)
p=np.matrix(a)
msg= input("Enter msg: ")
list1=list(msg)
l2=[]

for i in range(len(list1)):
    for j in range((26)):
        if(list1[i]==list2[j]):
            l2.append(j)

b=list(chunk_pad(l2, 3, 23))
for e in range(len(b)):
    list0=b[e]
    list0=list(list0)
    mul=np.dot(a,list0)
    CT=mul%26
    for i in range(len(CT)):
        for j in range(26):
            if(CT[i]==j):
                l3.append(list2[j])
                lis4.append(j)
enc=''.join(l3)
print("\nEncrypted Message: "+str(enc))


#print("\n\n====Receiver Side====")
det1=np.linalg.det(x)
c=list(chunk_pad(lis4,3,23))
adj=np.linalg.inv(x).T * np.linalg.det(x)

PT1=[]
for e in range(len(c)):
    list11=c[e]
    list11=list(list11)
    for i in range(26):
        if (i*det1)%26==1:
            det2=i
            inverse=(det2 * adj % 26).T
            mul1 = np.dot(inverse, list11)
            PT = mul1 % 26
            PT1=PT %26
            PT3=[]
            for q in range(len(PT1)):
                PT3.append(int(round(PT1[q])))

            for o in range(len(PT3)):
                if(PT3[o]==26.):
                    PT3[o]=0
            for i in range(len(PT3)):
                for j in range(26):
                    if PT3[i] == j:
                        l4.append(list2[j])
            dec=''.join(l4)
            break
        else:
            continue
print("\nDecrypted Messge: " + str(dec))

Screenshot:

Thursday, April 25, 2019

Encryption and decryption using Polyalphabetic Cipher (Vigenere Cipher) Technique - System Security | Python3

msg = input("Enter Message: ")
key = input("Enter Key: ")
list1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
list2 = list(msg)
l1 = []
l2 = list(key)
l3=[]

for i in range(len(list2)):
    for k in range(len(list1)):
        if(l2[i%(len(l2))]==list1[k]):
            key1=k
            break

    for j in range(len(list1)):
        if (list2[i] == list1[j]):
            CT = (j + key1) % 26
            l1.append(list1[CT])
str = ''.join(l1)
print("Encrypted Message: "+str)

for i in range(len(str)):
    for k in range(len(list1)):
        if(l2[i%(len(l2))]==list1[k]):
            key1=k
            break

    for j in range(len(list1)):
        if (str[i] == list1[j]):
            PT = (j - key1) % 26
            l3.append(list1[PT])
dec = ''.join(l3)
print("Decrypted Message: " + dec)


Screenshot:


Encryption and decryption using Monoalphabetic Cipher Technique by using Auto Generated Letters - System Security | Python3

import random,string

a=input("Enter the msg: ")
pt=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
d=[]
c=[]
ct=[]

while (len(ct)<26):
    r=random.choice(string.ascii_letters)
    if r not in ct:
        ct.append(r)

for i in a:
    b=pt.index(i)
    d.append(ct[b])
str1 = ''.join(d)
print ("Encrypted Text: "+str1)

for j in d:
    b=ct.index(j)
    c.append(pt[b])
str2 = ''.join(c)
print ('Decrypted Text: '+str2)

Screenshot:

Encryption and Decryption using Monoalphabetic Cipher Technique - System Security | Python3

a=input("Enter the msg: ")
pt=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
ct=['r','s','t','u','v','w','x','y','z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q']
d=[]
c=[]

for i in a:
    b=pt.index(i)
    d.append(ct[b])
str1 = ''.join(d)
print ("Encrypted Text: "+str1)

for j in d:
    b=ct.index(j)
    c.append(pt[b])
str2 = ''.join(c)
print ('Decrypted Text: '+str2)

Screenshot:

Find plain text messages and key information corresponds to following cipher text messages using Brute-Force technique on Caesar cipher - System Security | Python3

a=input("Enter the msg: ")
pt=[]

print ("Decrypted Data")
for key in range(1,27):
    for i in a:
        if ord(i)>=97 and ord(i)<=122:  
            d=(((ord(i)-key)-97)%26)+97
            pt.append(chr(d))
        elif ord(i)>=62 and ord(i)<=90:
            d=(((ord(i)-key)-65)%26)+65
            pt.append(chr(d))    
    str2 = ''.join(pt)
    print ("Key %d = %s" % (key,str2))
    pt=[]

Screenshot:


Encryption and Decryption using Variable Caesar Cipher - System Security | Python3

a=input("Enter the msg: ")
ct=[]
pt=[]
y=2
x=0
key=0
z=3
k=0

for i in a:
    key=int(2*x+3*y+z)
    if ord(i)>=97 and ord(i)<=122:
        d=(((ord(i)+key)-97)%26)+97
        ct.append(chr(d))        
    elif ord(i)>=62 and ord(i)<=90:
        d=(((ord(i)+key)-62)%26)+62
        ct.append(chr(d))
    else:
        ct.append(i)
    x=x+1
    key=0
        
print ("\nEncrypted Data")
str1 = ''.join(ct)
print (str1)

for i in ct:
    key=2*k+3*y+z
    if ord(i)>=97 and ord(i)<=122:    
        d=(((ord(i)-key)-97)%26)+97
        pt.append(chr(d))   
    elif ord(i)>=62 and ord(i)<=90:
        d=(((ord(i)-key)-62)%26)+62
        pt.append(chr(d))
    else:
        pt.append(i)
    key=0
    k=k+1
            
print ("\nDecrypted Data")
str2 = ''.join(pt)
print (str2)

Screenshot:

Encryption and Decryption using Caesar Cipher with Odd Even System - System Security | Python3

-> Encryption procedure: If the alphabet index is even then increase the value by one else decrease the key value by one.
-> Decryption Procedure: If the alphabet index is even then decrypting the value by one else increase the key value by one.
-> Code:
a=input("Enter the msg: ")
key=int(input("Enter the key: "))
ct=[]
pt=[]

for i in a:
    if ord(i)>=97 and ord(i)<=122:
        if(ord(i)%2) == 0:    
            d=(((ord(i)+key)-97)%26)+97
            ct.append(chr(d))
        else:
            d=(((ord(i)-key)-97)%26)+97
            ct.append(chr(d))
    elif ord(i)>=62 and ord(i)<=90:
        if(ord(i)%2) == 0:    
            d=(((ord(i)+key)-62)%26)+62
            ct.append(chr(d))
        else:
            d=(((ord(i)-key)-62)%26)+62
            ct.append(chr(d))
    else:
        ct.append(i)
        
print ("Encrypted Data")  
str1 = ''.join(ct)
print (str1)

for i in ct:
    if ord(i)>=97 and ord(i)<=122:
        if(ord(i)%2) == 0:    
            d=(((ord(i)+key)-97)%26)+97
            pt.append(chr(d))
        else:
            d=(((ord(i)-key)-97)%26)+97
            pt.append(chr(d))   
    elif ord(i)>=62 and ord(i)<=90:
        if(ord(i)%2) == 0:    
            d=(((ord(i)+key)-62)%26)+62
            pt.append(chr(d))
        else:
            d=(((ord(i)-key)-62)%26)+62
            pt.append(chr(d))
    else:
        pt.append(i)
            
print ("Decrypted Data")    
str2 = ''.join(pt)
print (str2)

Screenshot:


Encryption and Decryption using Caesar cipher algorithm - System Security | Python3

a = input("Enter the msg: ")
key = int(input("Enter the key: "))
ct = []
pt = []

for i in a:
    if ord(i) >= 97 and ord(i) <= 122:
        d = (((ord(i) + key) - 97) % 26) + 97
        ct.append(chr(d))
    elif ord(i) >= 62 and ord(i) <= 90:
        d = (((ord(i) + key) - 62) % 26) + 62
        ct.append(chr(d))
    else:
        ct.append(i)

print("Encrypted Data")
str1 = ''.join(ct)
print(str1)

for i in ct:
    if ord(i) >= 97 and ord(i) <= 122:
        d = (((ord(i) - key) - 97) % 26) + 97
        pt.append(chr(d))
    elif ord(i) >= 62 and ord(i) <= 90:
        d = (((ord(i) - key) - 62) % 26) + 62
        pt.append(chr(d))
    else:
        pt.append(i)

print("Decrypted Data")
str2 = ''.join(pt)
print(str2)

Screenshot: