Encrypt string in Python

后端 未结 6 944
孤街浪徒
孤街浪徒 2021-01-06 11:29

I need to encrypt a small string in Python. Is it possible to use a secret key to encrypt the string? Is there a good way to do this and achieve a reasonable encryption leve

相关标签:
6条回答
  • 2021-01-06 12:06

    I would do this with the easy to use cryptocode library (https://github.com/gdavid7/cryptocode).

    Encrypting and decrypting is relatively simple compared to other libraries:

    ## Encrypting:
    >>> import cryptocode
    >>> myEncryptedMessage = cryptocode.encrypt("I like trains", "password123")
    >>> print(myEncryptedMessage)
    M+Wykmlub0z7FhEdmA==*PvAbXRNx0SiSDHHxLsKZ5w==*ihQM/fdkgrX3G+yOItyAUQ==*QFNDmuUP1ysgo01/P2MNpg==
    
    ## Decrypting:
    >>> import cryptocode
    >>> myDecryptedMessage = cryptocode.decrypt("M+Wykmlub0z7FhEdmA==*PvAbXRNx0SiSDHHxLsKZ5w==*ihQM/fdkgrX3G+yOItyAUQ==*QFNDmuUP1ysgo01/P2MNpg==", "password123")
    >>> print(myDecryptedMessage)
    I like trains
    

    The only large disadvantage to this library over others is that it does not have many options to change around your string compared to other cryptography libraries. But it is perfect for a lot of people who just want an abstraction, and don't need to configure custom settings.

    0 讨论(0)
  • 2021-01-06 12:14

    I recently created a piece of code that does pretty much what your saying. It takes a codeword such as 'abc'gets the values (1, 2, 3) and then adds them to each letter in the word to encrypt. So if 'abc' was the codeword and 'bcd' the text to encrypt. (1+2 =3 2+3 =5 and 3+4 = 7) so the output would then be 'ceg'

    codeword = input('Enter codeword : ')
    codeword = codeword.replace(" ", "")  
    
    encrypt = input('Enter text to encrypt : ')
    encrypt = encrypt.replace(" ", "")
    
    j = 0
    for i in codeword:
        print(chr(ord(encrypt[j])+ ord(codeword[j])-96))
        j+=1
    
    0 讨论(0)
  • 2021-01-06 12:14

    Here's my code:

    from cryptography.fernet import Fernet
    import pyperclip
    
    print("For this program to work, please send the file named 'pwkey' and the encrypted code to the other user.")
    key = Fernet.generate_key()
    file = open('pwkey', 'wb')
    file.write(key)
    file.close()
    print('File Generated')
    original = input('Enter message>>>')
    message = original.encode()
    f = Fernet(key)
    encrypted = f.encrypt(message)
    encrypted = encrypted.decode("ascii")
    print('Encrypted:', encrypted)
    pyperclip.copy(encrypted)
    print('Please tell the other user to input the encrypted code in the Decrypt program')
    print('(Code copied to Clipboard)')
    print("Note: Please delete the 'pwkey' file after sending it to the other user. It 
        is for one-time use only.")
    

    And decrypting

    # Decrypt
    from cryptography.fernet import Fernet
    
    print("For this program to work, make sure you have the file named 'pwkey' in your Python folder and the encrypted "
            "code.")
    file = open('pwkey', 'rb')
    key = file.read()
    file.close()
    print('Key Retrieved')
    encrypted = input('Please input your encrypted code>>>')
    encrypted = bytes(encrypted, 'utf-8')
    f = Fernet(key)
    decrypted = f.decrypt(encrypted)
    decrypted = decrypted.decode()
    print('Decrypted Message:')
    print(decrypted)
    print("Note: Please delete the 'pwkey' file after getting the decrypted message. It is for one-time use only.")
    
    0 讨论(0)
  • 2021-01-06 12:15

    KeyCzar has a nice interface and should meet your requirements. From the home page:

    Keyczar is an open source cryptographic toolkit designed to make it easier and safer for devlopers to use cryptography in their applications. Keyczar supports authentication and encryption with both symmetric and asymmetric keys

    crypter = Crypter.Read("/path/to/your/keys")
    ciphertext = crypter.Encrypt("Secret message")
    
    0 讨论(0)
  • 2021-01-06 12:24

    I solved this by using a lightweight XTEA library that i found on ASPN. It doesn't require any additional Python libraries and is quite simple to implement whilst acheiving a resonable encryption level.

    0 讨论(0)
  • 2021-01-06 12:25

    Take a look at py-bcrypt. Perhaps it will meet your needs. From the web site:

    py-bcrypt is a Python wrapper of OpenBSD's Blowfish password hashing code, as described in "A Future-Adaptable Password Scheme" by Niels Provos and David Mazières

    0 讨论(0)
提交回复
热议问题