mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
increment 1
This commit is contained in:
54
ciphers/brute_force_caesar_cipher.py
Normal file
54
ciphers/brute_force_caesar_cipher.py
Normal file
@@ -0,0 +1,54 @@
|
||||
from __future__ import print_function
|
||||
def decrypt(message):
|
||||
"""
|
||||
>>> decrypt('TMDETUX PMDVU')
|
||||
Decryption using Key #0: TMDETUX PMDVU
|
||||
Decryption using Key #1: SLCDSTW OLCUT
|
||||
Decryption using Key #2: RKBCRSV NKBTS
|
||||
Decryption using Key #3: QJABQRU MJASR
|
||||
Decryption using Key #4: PIZAPQT LIZRQ
|
||||
Decryption using Key #5: OHYZOPS KHYQP
|
||||
Decryption using Key #6: NGXYNOR JGXPO
|
||||
Decryption using Key #7: MFWXMNQ IFWON
|
||||
Decryption using Key #8: LEVWLMP HEVNM
|
||||
Decryption using Key #9: KDUVKLO GDUML
|
||||
Decryption using Key #10: JCTUJKN FCTLK
|
||||
Decryption using Key #11: IBSTIJM EBSKJ
|
||||
Decryption using Key #12: HARSHIL DARJI
|
||||
Decryption using Key #13: GZQRGHK CZQIH
|
||||
Decryption using Key #14: FYPQFGJ BYPHG
|
||||
Decryption using Key #15: EXOPEFI AXOGF
|
||||
Decryption using Key #16: DWNODEH ZWNFE
|
||||
Decryption using Key #17: CVMNCDG YVMED
|
||||
Decryption using Key #18: BULMBCF XULDC
|
||||
Decryption using Key #19: ATKLABE WTKCB
|
||||
Decryption using Key #20: ZSJKZAD VSJBA
|
||||
Decryption using Key #21: YRIJYZC URIAZ
|
||||
Decryption using Key #22: XQHIXYB TQHZY
|
||||
Decryption using Key #23: WPGHWXA SPGYX
|
||||
Decryption using Key #24: VOFGVWZ ROFXW
|
||||
Decryption using Key #25: UNEFUVY QNEWV
|
||||
"""
|
||||
LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
for key in range(len(LETTERS)):
|
||||
translated = ""
|
||||
for symbol in message:
|
||||
if symbol in LETTERS:
|
||||
num = LETTERS.find(symbol)
|
||||
num = num - key
|
||||
if num < 0:
|
||||
num = num + len(LETTERS)
|
||||
translated = translated + LETTERS[num]
|
||||
else:
|
||||
translated = translated + symbol
|
||||
print("Decryption using Key #%s: %s" % (key, translated))
|
||||
|
||||
def main():
|
||||
message = raw_input("Encrypted message: ")
|
||||
message = message.upper()
|
||||
decrypt(message)
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
main()
|
||||
32
ciphers/onepad_cipher.py
Normal file
32
ciphers/onepad_cipher.py
Normal file
@@ -0,0 +1,32 @@
|
||||
from __future__ import print_function
|
||||
|
||||
import random
|
||||
|
||||
|
||||
class Onepad:
|
||||
def encrypt(self, text):
|
||||
'''Function to encrypt text using psedo-random numbers'''
|
||||
plain = [ord(i) for i in text]
|
||||
key = []
|
||||
cipher = []
|
||||
for i in plain:
|
||||
k = random.randint(1, 300)
|
||||
c = (i+k)*k
|
||||
cipher.append(c)
|
||||
key.append(k)
|
||||
return cipher, key
|
||||
|
||||
def decrypt(self, cipher, key):
|
||||
'''Function to decrypt text using psedo-random numbers.'''
|
||||
plain = []
|
||||
for i in range(len(key)):
|
||||
p = (cipher[i]-(key[i])**2)/key[i]
|
||||
plain.append(chr(p))
|
||||
plain = ''.join([i for i in plain])
|
||||
return plain
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
c, k = Onepad().encrypt('Hello')
|
||||
print(c, k)
|
||||
print(Onepad().decrypt(c, k))
|
||||
7193
ciphers/prehistoric_men.txt
Normal file
7193
ciphers/prehistoric_men.txt
Normal file
File diff suppressed because it is too large
Load Diff
36
ciphers/transposition_cipher_encrypt_decrypt_file.py
Normal file
36
ciphers/transposition_cipher_encrypt_decrypt_file.py
Normal file
@@ -0,0 +1,36 @@
|
||||
from __future__ import print_function
|
||||
import time, os, sys
|
||||
import transposition_cipher as transCipher
|
||||
|
||||
def main():
|
||||
inputFile = 'Prehistoric Men.txt'
|
||||
outputFile = 'Output.txt'
|
||||
key = int(raw_input('Enter key: '))
|
||||
mode = raw_input('Encrypt/Decrypt [e/d]: ')
|
||||
|
||||
if not os.path.exists(inputFile):
|
||||
print('File %s does not exist. Quitting...' % inputFile)
|
||||
sys.exit()
|
||||
if os.path.exists(outputFile):
|
||||
print('Overwrite %s? [y/n]' % outputFile)
|
||||
response = raw_input('> ')
|
||||
if not response.lower().startswith('y'):
|
||||
sys.exit()
|
||||
|
||||
startTime = time.time()
|
||||
if mode.lower().startswith('e'):
|
||||
content = open(inputFile).read()
|
||||
translated = transCipher.encryptMessage(key, content)
|
||||
elif mode.lower().startswith('d'):
|
||||
content = open(outputFile).read()
|
||||
translated =transCipher .decryptMessage(key, content)
|
||||
|
||||
outputObj = open(outputFile, 'w')
|
||||
outputObj.write(translated)
|
||||
outputObj.close()
|
||||
|
||||
totalTime = round(time.time() - startTime, 2)
|
||||
print(('Done (', totalTime, 'seconds )'))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
209
ciphers/xor_cipher.py
Normal file
209
ciphers/xor_cipher.py
Normal file
@@ -0,0 +1,209 @@
|
||||
"""
|
||||
author: Christian Bender
|
||||
date: 21.12.2017
|
||||
class: XORCipher
|
||||
|
||||
This class implements the XOR-cipher algorithm and provides
|
||||
some useful methods for encrypting and decrypting strings and
|
||||
files.
|
||||
|
||||
Overview about methods
|
||||
|
||||
- encrypt : list of char
|
||||
- decrypt : list of char
|
||||
- encrypt_string : str
|
||||
- decrypt_string : str
|
||||
- encrypt_file : boolean
|
||||
- decrypt_file : boolean
|
||||
"""
|
||||
class XORCipher(object):
|
||||
|
||||
def __init__(self, key = 0):
|
||||
"""
|
||||
simple constructor that receives a key or uses
|
||||
default key = 0
|
||||
"""
|
||||
|
||||
#private field
|
||||
self.__key = key
|
||||
|
||||
def encrypt(self, content, key):
|
||||
"""
|
||||
input: 'content' of type string and 'key' of type int
|
||||
output: encrypted string 'content' as a list of chars
|
||||
if key not passed the method uses the key by the constructor.
|
||||
otherwise key = 1
|
||||
"""
|
||||
|
||||
# precondition
|
||||
assert (isinstance(key,int) and isinstance(content,str))
|
||||
|
||||
key = key or self.__key or 1
|
||||
|
||||
# make sure key can be any size
|
||||
while (key > 255):
|
||||
key -= 255
|
||||
|
||||
# This will be returned
|
||||
ans = []
|
||||
|
||||
for ch in content:
|
||||
ans.append(chr(ord(ch) ^ key))
|
||||
|
||||
return ans
|
||||
|
||||
def decrypt(self,content,key):
|
||||
"""
|
||||
input: 'content' of type list and 'key' of type int
|
||||
output: decrypted string 'content' as a list of chars
|
||||
if key not passed the method uses the key by the constructor.
|
||||
otherwise key = 1
|
||||
"""
|
||||
|
||||
# precondition
|
||||
assert (isinstance(key,int) and isinstance(content,list))
|
||||
|
||||
key = key or self.__key or 1
|
||||
|
||||
# make sure key can be any size
|
||||
while (key > 255):
|
||||
key -= 255
|
||||
|
||||
# This will be returned
|
||||
ans = []
|
||||
|
||||
for ch in content:
|
||||
ans.append(chr(ord(ch) ^ key))
|
||||
|
||||
return ans
|
||||
|
||||
|
||||
def encrypt_string(self,content, key = 0):
|
||||
"""
|
||||
input: 'content' of type string and 'key' of type int
|
||||
output: encrypted string 'content'
|
||||
if key not passed the method uses the key by the constructor.
|
||||
otherwise key = 1
|
||||
"""
|
||||
|
||||
# precondition
|
||||
assert (isinstance(key,int) and isinstance(content,str))
|
||||
|
||||
key = key or self.__key or 1
|
||||
|
||||
# make sure key can be any size
|
||||
while (key > 255):
|
||||
key -= 255
|
||||
|
||||
# This will be returned
|
||||
ans = ""
|
||||
|
||||
for ch in content:
|
||||
ans += chr(ord(ch) ^ key)
|
||||
|
||||
return ans
|
||||
|
||||
def decrypt_string(self,content,key = 0):
|
||||
"""
|
||||
input: 'content' of type string and 'key' of type int
|
||||
output: decrypted string 'content'
|
||||
if key not passed the method uses the key by the constructor.
|
||||
otherwise key = 1
|
||||
"""
|
||||
|
||||
# precondition
|
||||
assert (isinstance(key,int) and isinstance(content,str))
|
||||
|
||||
key = key or self.__key or 1
|
||||
|
||||
# make sure key can be any size
|
||||
while (key > 255):
|
||||
key -= 255
|
||||
|
||||
# This will be returned
|
||||
ans = ""
|
||||
|
||||
for ch in content:
|
||||
ans += chr(ord(ch) ^ key)
|
||||
|
||||
return ans
|
||||
|
||||
|
||||
def encrypt_file(self, file, key = 0):
|
||||
"""
|
||||
input: filename (str) and a key (int)
|
||||
output: returns true if encrypt process was
|
||||
successful otherwise false
|
||||
if key not passed the method uses the key by the constructor.
|
||||
otherwise key = 1
|
||||
"""
|
||||
|
||||
#precondition
|
||||
assert (isinstance(file,str) and isinstance(key,int))
|
||||
|
||||
try:
|
||||
with open(file,"r") as fin:
|
||||
with open("encrypt.out","w+") as fout:
|
||||
|
||||
# actual encrypt-process
|
||||
for line in fin:
|
||||
fout.write(self.encrypt_string(line,key))
|
||||
|
||||
except:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def decrypt_file(self,file, key):
|
||||
"""
|
||||
input: filename (str) and a key (int)
|
||||
output: returns true if decrypt process was
|
||||
successful otherwise false
|
||||
if key not passed the method uses the key by the constructor.
|
||||
otherwise key = 1
|
||||
"""
|
||||
|
||||
#precondition
|
||||
assert (isinstance(file,str) and isinstance(key,int))
|
||||
|
||||
try:
|
||||
with open(file,"r") as fin:
|
||||
with open("decrypt.out","w+") as fout:
|
||||
|
||||
# actual encrypt-process
|
||||
for line in fin:
|
||||
fout.write(self.decrypt_string(line,key))
|
||||
|
||||
except:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
|
||||
|
||||
# Tests
|
||||
# crypt = XORCipher()
|
||||
# key = 67
|
||||
|
||||
# # test enrcypt
|
||||
# print crypt.encrypt("hallo welt",key)
|
||||
# # test decrypt
|
||||
# print crypt.decrypt(crypt.encrypt("hallo welt",key), key)
|
||||
|
||||
# # test encrypt_string
|
||||
# print crypt.encrypt_string("hallo welt",key)
|
||||
|
||||
# # test decrypt_string
|
||||
# print crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key)
|
||||
|
||||
# if (crypt.encrypt_file("test.txt",key)):
|
||||
# print "encrypt successful"
|
||||
# else:
|
||||
# print "encrypt unsuccessful"
|
||||
|
||||
# if (crypt.decrypt_file("encrypt.out",key)):
|
||||
# print "decrypt successful"
|
||||
# else:
|
||||
# print "decrypt unsuccessful"
|
||||
Reference in New Issue
Block a user