Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Sep 7, 2025. It is now read-only.
/pythonPublic archive

Issue #67: Added new encryption algorithm#150

Open
joseph-alan-jose wants to merge2 commits intoAllAlgorithms:master
base:master
Choose a base branch
Loading
fromqburst:master
Open
Changes fromall 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
54 changes: 54 additions & 0 deletionsalgorithms/cryptography/columnar_transposition_cipher.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
import math
def encrypt(key, message):
ciphertext = []
for col in range(key):
position = col
while position < len(message):
ciphertext.append(message[position])
position += key
return ''.join(ciphertext)

def decrypt(key, message):
numOfRows = math.ceil(len(message) / key)
plaintext = []
cipher = [*message]
remainder = len(message)%key
count=0
if(remainder != 0):
for i in range(0,len(message),numOfRows):
count += 1
if(count > remainder):
cipher.insert(i+numOfRows-1,'#')
cipher.append('#')
for row in range(numOfRows):
position = row
while(position < len(cipher)):
plaintext.append(cipher[position])
position += numOfRows
plaintext = [ele for ele in plaintext if ele != '#']
else:
for row in range(numOfRows):
position = row
while(position < len(message)):
plaintext.append(message[position])
position += numOfRows
return ''.join(plaintext)
def main():
while(True):
print("1. Encrypt")
print("2. Decrypt")
print("3. Exit")
option = int(input("Enter the option: "))
if(option == 1):
text = input("Enter the plain text: ")
k = int(input("Enter the key: "))
cipher = encrypt(k,text)
print("Ciphertext: ", cipher)
if(option == 2):
text = input("Enter the ciphertext: ")
k = int(input("Enter the key: "))
plain = decrypt(k, text)
print("Plaintext: ", plain)
if(option == 3):
break
main()

[8]ページ先頭

©2009-2025 Movatter.jp