欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)的文本簡單可逆加密算法示例

 更新時(shí)間:2017年05月18日 10:31:22   作者:九日王朝  
這篇文章主要介紹了Python實(shí)現(xiàn)的文本簡單可逆加密算法,結(jié)合完整實(shí)例形式分析了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ì)有所幫助。

相關(guān)文章

最新評論