当前位置:   article > 正文

python实现凯撒加密和暴力破解_python字符串加密 凯撒密码

python字符串加密 凯撒密码

编写程序,要求输入一个字符串,然后输入一个整数作为凯撒加密算法的密钥,输出该字符串加密后的结果。(提示:凯撒加密是一种替换加密的技术,明文中的所有字母都在字母表上向后(或向前)按照一个固定数目n进行偏移后被替换成密文,此处的n就是密钥。)

  1. '''
  2. import string
  3. def kaisa(s,k):
  4. lower=string.ascii_lowercase
  5. upper=string.ascii_uppercase
  6. before=string.ascii_letters
  7. after=lower[k:]+lower[:k]+upper[k:]+upper[:k]
  8. table=''.maketrans(before,after)
  9. return s.translate(table)
  10. s=input('input string:')
  11. k=int(input('input int security-key:'))
  12. print(kaisa(s,k))
  13. '''
  14. '''
  15. #凯撒加密
  16. import string
  17. #upper and lower letters
  18. print(string.ascii_letters)
  19. #0123456789
  20. print(string.digits)
  21. #a string containing all ASCII punctuation characters
  22. print(string.punctuation)
  23. #upper letters
  24. print(string.ascii_uppercase)
  25. #lower letters
  26. print(string.ascii_lowercase)
  27. uInput=input('input string:')
  28. secKey=int(input('input int security-key:'))
  29. #secKey=6
  30. lower=string.ascii_lowercase
  31. upper=string.ascii_uppercase
  32. before=string.ascii_letters
  33. after=lower[secKey:]+lower[:secKey]+upper[secKey:]+upper[:secKey]
  34. #print(after)
  35. table=''.maketrans(before,after)
  36. #print(table)
  37. print(uInput.translate(table))
  38. '''
  39. '''
  40. #凯撒加密
  41. key=int(input('input caesar encrypt key( key>0 ):'))
  42. inputstr=input('input string:')
  43. inputStrLower=inputstr.lower()
  44. #print(key)
  45. #print(inputstr)
  46. encryptstr=''
  47. letters='abcdefghijklmnopqrstuvwxyz'
  48. for i in inputStrLower:
  49. if i in letters:
  50. num1=letters.index(i)+1
  51. encryptkey=num1+key
  52. if encryptkey>26:
  53. encryptkey=encryptkey%26
  54. #print(encryptkey)
  55. encryptstr=encryptstr+letters[encryptkey-1]
  56. print(encryptstr)
  57. '''
  58. '''
  59. #凯撒加密破解
  60. encryptStr='klmnopqrstuvwxyzabcdefghij'
  61. letters='abcdefghijklmnopqrstuvwxyz'
  62. for key in range(1,27):
  63. decryptstr=''
  64. print(key)
  65. for j in encryptStr:
  66. if j in letters:
  67. num2=letters.index(j)
  68. decryptkey=num2+key
  69. if decryptkey>26:
  70. decryptkey=decryptkey%26
  71. decryptstr=decryptstr+letters[decryptkey-1]
  72. print(decryptstr)
  73. print('---------------------')
  74. '''
  75. '''
  76. #凯撒加密-python密码学编程
  77. message='guvF vF FrpErG zrFFntr.'
  78. key=13
  79. mode='encrypt'
  80. LETTERS='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
  81. translated=''
  82. messagge=message.upper()
  83. for symbol in message:
  84. print(symbol)
  85. if symbol in LETTERS:
  86. num=LETTERS.find(symbol)
  87. if mode=='encrypt':
  88. num=num+key
  89. elif mode=='decrypt':
  90. num=num-key
  91. if num>=len(LETTERS):
  92. num=num-len(LETTERS)
  93. elif num<0:
  94. num=num+len(LETTERS)
  95. translated=translated+LETTERS[num]
  96. else:
  97. translated=translated+symbol
  98. print(translated)
  99. '''

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/496472
推荐阅读
相关标签
  

闽ICP备14008679号