Python實現(xiàn)的文本簡單可逆加密算法示例
本文實例講述了Python實現(xiàn)的文本簡單可逆加密算法。分享給大家供大家參考,具體如下:
其實很簡單,就是把一段文本每個字符都通過某種方式改變(比如加1)
這樣就實現(xiàn)了文本的加密操作,解密就是其逆運算
# -*-coding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf8')
#加密
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]+'[加密]'.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()
#解密
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]+'[解密]'.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('---請輸入命令,1為加密 2為解密 3為退出---\n'.encode('gbk')))
if index==1:
jiami()
elif index==2:
jiemi()
elif index==3:
exit(0)
else:
pass
注意:
①如果出現(xiàn)中文編碼問題可以通過.encode,.decode編碼解碼
②可以通過Python的切片操作處理文件名,很方便,例如: newfilename=filename[:num]+'[加密]'.encode('gbk')+filename[num:]
③最重要的!!本加密方法只是簡單的給文本字符做一個+password處理,其方法非常不合理,因為加的數(shù)如果過大會造成chr字節(jié)不夠(比如你輸一個1000)
所以本代碼只適用于新手練習(xí),而不能作為真正的處理算法
PS:關(guān)于加密解密感興趣的朋友還可以參考本站在線工具:
MD5在線加密工具:
http://tools.jb51.net/password/CreateMD5Password
迅雷、快車、旋風(fēng)URL加密/解密工具:
http://tools.jb51.net/password/urlrethunder
在線散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt
在線MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha
在線sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.jb51.net/password/sha_encode
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
- Python實現(xiàn)ElGamal加密算法的示例代碼
- Python實現(xiàn)常見的幾種加密算法(MD5,SHA-1,HMAC,DES/AES,RSA和ECC)
- python實現(xiàn)換位加密算法的示例
- Python實現(xiàn)的HMacMD5加密算法示例
- Python使用MD5加密算法對字符串進行加密操作示例
- Python實現(xiàn)的rsa加密算法詳解
- Python md5與sha1加密算法用法分析
- python實現(xiàn)的DES加密算法和3DES加密算法實例
- python使用rsa加密算法模塊模擬新浪微博登錄
- Python 實現(xiàn)國產(chǎn)SM3加密算法的示例代碼
相關(guān)文章
在?pytorch?中實現(xiàn)計算圖和自動求導(dǎo)
這篇文章主要介紹了在?pytorch?中實現(xiàn)計算圖和自動求導(dǎo),文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-06-06
python return實現(xiàn)匯率轉(zhuǎn)換器教程示例
這篇文章主要為大家介紹了python return實現(xiàn)匯率轉(zhuǎn)換器教程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06
pandas系列之DataFrame 行列數(shù)據(jù)篩選實例
下面小編就為大家分享一篇pandas系列之DataFrame 行列數(shù)據(jù)篩選實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04
Python批量將Word文件轉(zhuǎn)為PDF文件的實現(xiàn)示例
如果想要批量把Word文檔轉(zhuǎn)換為PDF文檔,我們可以使用第三方模塊win32com,本文就來詳細的介紹一下Python批量將Word文件轉(zhuǎn)為PDF文件的實現(xiàn)示例,感興趣的可以了解一下2023-08-08
Python打包工具PyInstaller的安裝與pycharm配置支持PyInstaller詳細方法
這篇文章主要介紹了Python打包工具PyInstaller的安裝與pycharm配置支持PyInstaller詳細方法,需要的朋友可以參考下2020-02-02
使用darknet框架的imagenet數(shù)據(jù)分類預(yù)訓(xùn)練操作
這篇文章主要介紹了使用darknet框架的imagenet數(shù)據(jù)分類預(yù)訓(xùn)練操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07

