Example of simple reversible text encryption algorithm implemented by Python
- 2020-06-01 10:14:46
- OfStack
In this paper, a simple reversible text encryption algorithm implemented by Python is illustrated. I will share it with you for your reference as follows:
In fact, it is very simple to change each character of 1 paragraph of text in some way (such as adding 1).
In this way, the text encryption operation is realized, and decryption is the reverse operation
# -*-coding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf8')
# encryption
def jiami():
filename=raw_input('please input file:\n')
while True:
try:
password=int(raw_input('input number pass word:\n'))
break
except:
print 'please input number:\n'
fileword=open(filename,'r')
num=filename.rfind('.')
newfilename=filename[:num]+'[ encryption ]'.encode('gbk')+filename[num:]
content=fileword.read(1)
newfileword=open(newfilename,'a+')
while len(content)>0:
contentInt=ord(content)
newContent=contentInt+password
c=chr(newContent)
newfileword.write(c)
content=fileword.read(1)
newfileword.close()
fileword.close()
# decryption
def jiemi():
filename=raw_input('please input file:\n')
while True:
try:
password=int(raw_input('input number pass word:\n'))
break
except:
print 'please input number:\n'
fileword=open(filename,'r')
num=filename.rfind('.')
num2=filename.rfind('[')
newfilename=filename[:num2]+'[ decryption ]'.encode('gbk')+filename[num:]
content=fileword.read(1)
newfileword=open(newfilename,'a+')
while len(content)>0:
contentInt=ord(content)
newContent=contentInt-password
c=chr(newContent)
newfileword.write(c)
content=fileword.read(1)
newfileword.close()
fileword.close()
while True:
index=int(raw_input('--- Please enter the command ,1 For encryption 2 To decrypt 3 To quit ---\n'.encode('gbk')))
if index==1:
jiami()
elif index==2:
jiemi()
elif index==3:
exit(0)
else:
pass
Note:
If there is a Chinese coding problem can be through.encode,.decode encoding and decoding
newfilename=filename[:num]+'[encryption]'.encode('gbk')+filename[num:]
The most important!! This encryption method simply adds 1 +password to the text character, which is very unreasonable, because if the number is too large, chr bytes will not be enough (for example, you type 1 1000).
Therefore, this code is only suitable for beginners to practice, not as a real processing algorithm
PS: about encryption and decryption interested friends can also refer to the website online tools:
MD5 online encryption tool:
http://tools.ofstack.com/password/CreateMD5Password
Thunderbolt, express, and whirlwind encryption/decryption tools:
http://tools.ofstack.com/password/urlrethunder
Online hash/hash algorithm encryption tool:
http://tools.ofstack.com/password/hash_encrypt
MD5/hash/ SHA-1 / SHA-2 / SHA-256 / SHA-512 / SHA-3 / RIPEMD-160 encryption tools:
http://tools.ofstack.com/password/hash_md5_sha
Online sha1 sha224 / sha256 sha384 / sha512 encryption tools:
http://tools.ofstack.com/password/sha_encode
More about Python related topics: interested readers to view this site "Python coding skills summary", "Python data structure and algorithm tutorial", "Python Socket programming skills summary", "Python function using techniques", "Python string skills summary", "Python introduction and advanced tutorial" and "Python file and directory skills summary"
I hope this article is helpful to you Python programming.