[英]very simple string encryption
I was wanting to create my own extremely simple encryption for some text in python.我想为 python 中的一些文本创建我自己的极其简单的加密。 Its only to deter people if they find the file i want to encrypt.它只是为了阻止人们找到我想要加密的文件。 I have this code below that could do that however it would take forever to code it the way im doing it and im sure its not the most efficient.我下面有这段代码可以做到这一点,但是按照我的方式对其进行编码需要很长时间,而且我确信它不是最有效的。 I was wondering if there is a way to do this using much less code.我想知道是否有办法使用更少的代码来做到这一点。 (a,b,c are not the only charicters i will use btw. Also im fine copying/pasting the random text myself) (a,b,c 不是我将使用的唯一字符顺便说一句。我也可以自己复制/粘贴随机文本)
def encrypt(text):
return str(text).replace("a", "3tpkDWCiXw").replace("b", "moOK4LWTUx").replace("c", "qqN9zTb9nR")
def decrypt(text):
return str(text).replace("3tpkDWCiXw", "a").replace("moOK4LWTUx", "b").replace("qqN9zTb9nR", "c")
encrypted = encrypt("abc")
print(encrypted)
decrypted = decrypt(encrypted)
print(decrypted)
Thanks in advance as it will also help me with other things, not just this small project.提前致谢,因为它还可以帮助我处理其他事情,而不仅仅是这个小项目。 PS i just want this to be simple like the example given because i dont want/need more complicated encryption. PS我只是希望这像给出的例子一样简单,因为我不想要/需要更复杂的加密。
Edit: Another reason I'm not using more complicated encryption is because whenever i copy the code and try to do it myself i get an error.编辑:我不使用更复杂的加密的另一个原因是,每当我复制代码并尝试自己执行时,我都会收到错误消息。 Also i did install cryptography but for some reason it didn't work.我也确实安装了密码学,但由于某种原因它不起作用。
Here is Caesar Cipher:这是凯撒密码:
#! /usr/bin/env python
# Caesar Cipher Encryption and Decryption tool.
# Introduction
print('\nHow to use:\n\n')
print('Encrypt:\n')
print('\tEnter your encryption key (1-25)\n')
print('\tThen, enter the message to encrypt.\n\n')
print('Decrypt:\n')
print('\tEnter the decryption key.\n')
print('\tThen, paste the encrypted message.')
print('\n\nEnter "q" or "quit" to exit.\n')
# Every possible symbol that can be encrypted/decrypted:
SYMBOLS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 !?.~`@#$%^&*)("
message = ''
mode = ''
# User enters the mode
currentMode = input("Mode: [encrypt] or [decrypt]: ")
# ENCRYPT settings
if currentMode == 'e'.lower() or currentMode == 'encrypt'.lower():
mode = 'encrypt'
key = int(input('Encryption Key: '))
while key >= len(SYMBOLS) or key <= 0:
print('Please enter an encryption key between 1-' + str(len(SYMBOLS) - 1))
key = int(input('Encryption Key: '))
message = input('What would you like to encrypt?\n')
# DECRYPT settings
elif currentMode == 'D' or currentMode == 'd' or currentMode == 'decrypt' or currentMode == 'DECRYPT':
mode = 'decrypt'
key = int(input('Decryption Key: '))
while key >= len(SYMBOLS) or key <= 0:
print('Please enter a decryption key between 1-' + str(len(SYMBOLS) - 1))
key = int(input('Decryption Key: '))
message = input('Paste the encrypted text below:\n')
# Quit
elif currentMode == 'Q' or currentMode == 'q' or currentMode == 'quit' or currentMode == 'QUIT':
exit()
# Store encrypted / decrypted message:
translated = ''
for symbol in message:
if symbol in SYMBOLS:
symbolIndex = SYMBOLS.find(symbol)
# Perform encryption / decryption
if mode == 'encrypt':
translatedIndex = symbolIndex + key
if mode == 'decrypt':
translatedIndex = symbolIndex - key
# Handle wraparound if needed:
if translatedIndex >= len(SYMBOLS):
translatedIndex = translatedIndex - len(SYMBOLS)
elif translatedIndex < 0:
translatedIndex = translatedIndex + len(SYMBOLS)
translated = translated + SYMBOLS[translatedIndex]
else:
# Append the symbol without encrypting / decrypting:
translated = translated + symbol
# Output text
if mode == 'encrypt':
print('\nYour ENCRYPTED message:')
elif mode == 'decrypt':
print('\nThe secret message is:')
# Ciphertext
print('\n\t' + translated + '\n')
You can also check out the Affine or Transposition Cipher which are both fairly simple (but more complex than Caesar Cipher)您还可以查看 Affine 或 Transposition Cipher,它们都相当简单(但比 Caesar Cipher 更复杂)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.