Python實(shí)現(xiàn)的文本簡單可逆加密算法示例
本文實(shí)例講述了Python實(shí)現(xiàn)的文本簡單可逆加密算法。分享給大家供大家參考,具體如下:
其實(shí)很簡單,就是把一段文本每個(gè)字符都通過某種方式改變(比如加1)
這樣就實(shí)現(xiàn)了文本的加密操作,解密就是其逆運(yù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:]
③最重要的?。”炯用芊椒ㄖ皇呛唵蔚慕o文本字符做一個(gè)+password處理,其方法非常不合理,因?yàn)榧拥臄?shù)如果過大會造成chr字節(jié)不夠(比如你輸一個(gè)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ìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計(jì)有所幫助。
- Python實(shí)現(xiàn)ElGamal加密算法的示例代碼
- Python實(shí)現(xiàn)常見的幾種加密算法(MD5,SHA-1,HMAC,DES/AES,RSA和ECC)
- python實(shí)現(xiàn)換位加密算法的示例
- Python實(shí)現(xiàn)的HMacMD5加密算法示例
- Python使用MD5加密算法對字符串進(jìn)行加密操作示例
- Python實(shí)現(xiàn)的rsa加密算法詳解
- Python md5與sha1加密算法用法分析
- python實(shí)現(xiàn)的DES加密算法和3DES加密算法實(shí)例
- python使用rsa加密算法模塊模擬新浪微博登錄
- Python 實(shí)現(xiàn)國產(chǎn)SM3加密算法的示例代碼
相關(guān)文章
在?pytorch?中實(shí)現(xiàn)計(jì)算圖和自動求導(dǎo)
這篇文章主要介紹了在?pytorch?中實(shí)現(xiàn)計(jì)算圖和自動求導(dǎo),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06python return實(shí)現(xiàn)匯率轉(zhuǎn)換器教程示例
這篇文章主要為大家介紹了python return實(shí)現(xiàn)匯率轉(zhuǎn)換器教程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06pandas系列之DataFrame 行列數(shù)據(jù)篩選實(shí)例
下面小編就為大家分享一篇pandas系列之DataFrame 行列數(shù)據(jù)篩選實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04Python批量將Word文件轉(zhuǎn)為PDF文件的實(shí)現(xiàn)示例
如果想要批量把Word文檔轉(zhuǎn)換為PDF文檔,我們可以使用第三方模塊win32com,本文就來詳細(xì)的介紹一下Python批量將Word文件轉(zhuǎn)為PDF文件的實(shí)現(xiàn)示例,感興趣的可以了解一下2023-08-08Python打包工具PyInstaller的安裝與pycharm配置支持PyInstaller詳細(xì)方法
這篇文章主要介紹了Python打包工具PyInstaller的安裝與pycharm配置支持PyInstaller詳細(xì)方法,需要的朋友可以參考下2020-02-02python性能測試手機(jī)號驗(yàn)證碼登錄壓測示例詳解
這篇文章主要為大家介紹了python性能測試手機(jī)號驗(yàn)證碼登錄壓測的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07使用darknet框架的imagenet數(shù)據(jù)分類預(yù)訓(xùn)練操作
這篇文章主要介紹了使用darknet框架的imagenet數(shù)據(jù)分類預(yù)訓(xùn)練操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07如何用Python實(shí)現(xiàn)自動發(fā)送微博
大家好,本篇文章主要講的是如何用Python實(shí)現(xiàn)自動發(fā)送微博,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏2022-01-01