Python encryption function and global variables

This question already has an answer here:

  • Using global variables in a function other than the one that created them 18 answers

  • To protect programmers from globals, globals are not considered to exist in a function until you declare they do with global variablename . In maincrypt you do not declare global newtext nor do you create a local newtext by assigning to it. The += operator requires the left hand to already exist - it doesn't yet, so an exception is thrown.

    By the way, if you plan to reuse this program anywhere else, it is better practice to use class variables instead of globals, so 1) if someone else uses the same globals name all is OK 2) you can have two instances of the encryptor/decryptor code running and encrypting/decrypting different things, even in parallel on different threads, without clobbering each others' globals.

    Also, this code: ord(i.upper()) - ord('A') will give you 0 for 'a' and 'A', 1 for 'b' and 'B', 2 for 'c' and 'C'... and so on, so you don't need such immense if/elif chains. (Why this works is because 'A', 'B', 'C' etc are contiguous in ASCII, so 'B' - 'A' is 1 and so on)


    First off, there are pretty strong and virtually unbreakable encryption algorithms some of which come prepackaged with python such as hmac

    import hmac
    
    def Hash_string(ref):
            cypher = hmac.new('you secret word', 'plain-text')
            return cypher.hexdigest()
    

    Simple enough to use and much stronger. You could also use blowfish for much more advanced and secure projects, though it doesn't come with python by default but it's simple enough to setup.

    Now to your code, here's how to use global variables in python

    # declare variables as global
    global num1
    global num2
    num2=22    # give initial value to num2
    
    def foo():
       # bind variable names to the ones in global scope
       global num2
       print num2   # output: 22
       num2 = 88
       print num2   # output: 88
       global num1
       num1 = num2
    
    def bar():
       print num1   # output: 88
       print num2   # output 88
    
    foo()
    bar()
    

    So you need to bind newtext to the one declared as gobal

    I also suggest that you rewrite encrypt to look something like this

    alpha = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

    numeric = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]

    def map (ch):
       chr = ch.lower()
       return numeric[alpha.index(chr)]
    

    this creates a mapping between numbers and alphabets so you won't need a long list of elifs

    Hope this helps.

    EDIT you can access globals with using the global keyword, but if you want to modify them, you first need to bind to the global var before you can change them.

    EDIT changed return numeric[index(chr)] to return numeric[alpha.index(chr)] sorry for the mistake :)

    链接地址: http://www.djcxy.com/p/23888.html

    上一篇: 为Python添加变量内部函数

    下一篇: Python加密函数和全局变量