Skip to content
This repository was archived by the owner on Sep 7, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions algorithms/cryptography/Hill_cipher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import numpy as np
s=list(input("Enter a string"))
c=[]
for i in range(len(s)):
c.append(ord(s[i])-65)

arr= np.array(c)

a1=np.transpose(arr)
print(a1)
a1= a1.reshape(3,1)
print(a1.shape)


#key input
print("Enter the key for the encryption")
R = int(input("rows:"))
C = int(input("columns:"))
matrix = []
print("Enter the key:")

for i in range(R):
a =[]
for j in range(C):
a.append(int(input()))
matrix.append(a)

for i in range(R):
for j in range(C):
print(matrix[i][j], end = " ")
matrix = np.array(matrix)
print(matrix.shape)
print(matrix[1][1])

mul=np.matmul(matrix,a1)
mul = np.array(mul)
print(mul.shape)
print(mul)
for i in range(R):
mul[i]=mul[i]%26

print(mul)
78 changes: 78 additions & 0 deletions algorithms/cryptography/railfence_cipher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
def railencrypt(st,k):
c = 0
x = 0
m =[[0] * (len(st)) for i in range(k)]
for r in range(len(st)):
m[c][r] = st[r]
if x == 0:
if c == (k-1):
x = 1
c -= 1
else:
c += 1
else:
if c == 0:
x = 0
c += 1
else:
c -= 1

result = []
for i in range(k):
for j in range(len(st)):
if m[i][j] != 0:
result.append(m[i][j])
print("CipherText:","" . join(result))

def raildecrypt(st,k):
c , x = 0 , 0
m =[[0] * (len(st)) for i in range(k)]
for r in range(len(st)):
m[c][r] = 1
if x == 0:
if c == (k-1):
x = 1
c -= 1
else:
c += 1
else:
if c == 0:
x = 0
c += 1
else:
c -= 1
result = []
c , x = 0 , 0
for i in range(k):
for j in range(len(st)):
if m[i][j] == 1:
m[i][j] = st[x]
x += 1
for r in range(len(st)):
if m[c][r] != 0:
result.append(m[c][r])
if x == 0:
if c == (k-1):
x = 1
c -= 1
else:
c += 1
else:
if c == 0:
x = 0
c += 1
else:
c -= 1
print("PlainText:","" . join(result))

if __name__ == "__main__":
string = input("Enter the Message:")
string = string.upper()
key = int(input("Enter the Key:"))
n = int(input("1.Encryption\n2.Decryption\nInput Your choice:"))
if(n == 1):
railencrypt(string,key)
elif(n == 2):
raildecrypt(string,key)
else:
print("Error")