zoukankan      html  css  js  c++  java
  • Caesarencrypt and caesarHacker

    关于凯撒加密算法和他的暴力破解


    凯撒加密算法是一种不安全的算法:

    对字母字符加密

    加密过程及结果的字符都是大写,加密前需要将明文转化为大写的字符串

    工具为索引轮盘或是St.Cyr滑条,在计算机中即‘ABCDEFGHIJKLMONOPQRSTUVWXYZ’,

    密钥匙0到25的一个数字

    要用到模运算的思想

    简单的、有规律的替换,非常不安全!

    加密算法:

    # Caesar Cipher
    # http://inventwithpython.com/hacking (BSD Licensed)
    
    import pyperclip
    
    # the string to be encrypted/decrypted
    #message = 'This is my secret message.'
    #message = "You can show black is white by argument,' said Filby, 'but you will never convince me."
    #message = '1234567890'
    #message = "This is still a silly example."
    message = '''
    {
        // IntelliSense  
        //
        //  https://go.microsoft.com/fwlink/?linkid=830387
        "version": "0.2.0",
        "configurations": [
            {"name":"Python: ","type":"python","request":"launch","program":"${file}","console":"integratedTerminal"},
            {
                "name": "Python: ",
                "type": "python",
                "request": "launch",
                "program": "${file}",
                "console": "integratedTerminal"
            }
        ]
    }'''
    
    #message = 'GUVF VF ZL FRPERG ZRFFNTR.'
    #message = "'Kv uqwpfu rncwukdng gpqwij.'"
    #message = 'Xqp whh ahoa kb pda sknhz swo ejreoexha.'
    # the encryption/decryption key
    key = 13
    
    # tells the program to encrypt or decrypt
    mode = 'encrypt' # set to 'encrypt' or 'decrypt'
    #mode = 'decrypt'
    # every possible symbol that can be encrypted
    LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    
    # stores the encrypted/decrypted form of the message
    translated = ''
    
    # capitalize the string in message
    message = message.upper()
    
    # run the encryption/decryption code on each symbol in the message string
    for symbol in message:
        if symbol in LETTERS:
            # get the encrypted (or decrypted) number for this symbol
            num = LETTERS.find(symbol) # get the number of the symbol
            if mode == 'encrypt':
                num = num + key
            elif mode == 'decrypt':
                num = num - key
    
            # handle the wrap-around if num is larger than the length of
            # LETTERS or less than 0
            if num >= len(LETTERS):
                num = num - len(LETTERS)
            elif num < 0:
                num = num + len(LETTERS)
    
            # add encrypted/decrypted number's symbol at the end of translated
            translated = translated + LETTERS[num]
    
        else:
            # just add the symbol without encrypting/decrypting
            translated = translated + symbol
    
    # print the encrypted/decrypted string to the screen
    print(translated)
    
    # copy the encrypted/decrypted string to the clipboard   666666    真尼玛离谱
    pyperclip.copy(translated)

    这边参考了http://inventwithpython.com/hacking/chapters/书籍,密码学之python编程。可以从网上下载其源码

    其中pyperclip是希望程序结束后我们能得到这个程序的输出,原理是提供函数可以让程序把文本复制到剪贴板(若未下载注释到这行和import就好)

    破解时遍历25个数值,从结果中分析加密的明文是比较明显、简单的。

    # Caesar Cipher Hacker
    # http://inventwithpython.com/hacking (BSD Licensed)
    
    #message = 'GUVF VF ZL FRPERG ZRFFNTR.'
    '''
    message = 'R UXEN VH TRCCH.'                                9
    message = 'FR DBMMR EHOXL FX.'                              19
    message = "CXPNCQNA FN'AN BX QJYYH."                        9
    message = 'OBR OZKOMG QOFSTFSS.'                            14
    message = 'PDKQCD IU DAWZ DWO OQOLEYEKJO.'                  22
    message = 'FTMF U WQQB GZPQD YK TMF.'                       12
    message = 'AR ITMF YUSTF TMBBQZ.'                           12
    message = 'DA D NCMVIF OJ OCZ NDUZ JA V MVO.'               21
    message = "ZFBI. J'N QSFUUZ TVSF NZ DBU XPVME FBU NF."      1
    '''
    #this line is for you copy.
    message = "ZFBI. J'N QSFUUZ TVSF NZ DBU XPVME FBU NF."
    
    LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    
    # loop through every possible key
    for key in range(len(LETTERS)):
    
        # It is important to set translated to the blank string so that the
        # previous iteration's value for translated is cleared.
        translated = ''
    
        # The rest of the program is the same as the original Caesar program:
    
        # run the encryption/decryption code on each symbol in the message
        for symbol in message:
            if symbol in LETTERS:
                num = LETTERS.find(symbol) # get the number of the symbol
                num = num - key
    
                # handle the wrap-around if num is 26 or larger or less than 0
                if num < 0:
                    num = num + len(LETTERS)
    
                # add number's symbol at the end of translated
                translated = translated + LETTERS[num]
    
            else:
                # just add the symbol without encrypting/decrypting
                translated = translated + symbol
    
        # display the current key being tested, along with its decryption
        print('Key #%s: %s' % (key, translated))
  • 相关阅读:
    Docker 版本升级
    Docker Swarm 常用命令
    Docker Swarm 介绍 or 工作原理
    Docker OpenvSwitch 应用部署
    Docker OpenvSwitch 介绍 or 工作原理
    Solr配置与简单Demo[转]
    使用solrj操作solr索引库,solr是lucene服务器
    Apache Solr配置
    使用solr搭建你的全文检索
    [转]flume-ng+Kafka+Storm+HDFS 实时系统搭建
  • 原文地址:https://www.cnblogs.com/PiaYie/p/13463237.html
Copyright © 2011-2022 走看看