我會推薦使用模數運算符來做你想要的。在python中是%字符。在模數學中。 X%Y告訴我們X/Y的剩余部分是什么。例如。 27%26是1.使用這個你可以得到你想要的包裝。這里是一個代碼示例位,如果我們喂字符“A”進入我們的加密算法的13關鍵,我們得到“N”,如到現在為止加密單個字符
def encrypt_character(valToEncrypt, keyVal):
# Update the character to be our standard Alphabet mapping
# A -> 0; B->1 ... Z -> 25
python數據加密代碼,x = ord(valToEncrypt) - ord('A')
# Perform the Encryption
retVal = (x + keyVal) % 26
# Translate back to the standard ASCII mapping of the character
# for display in python and translate it back into a string
Python加密?retVal = chr(retVal + ord('A'))
return retVal
# end encrypt_character
:
>>> encrypt_character("A", 13)
python編譯文件加密。'N'
解密算法是非常相似的,除非你做減法而不是addtion
def decrypt_character(valToDecrypt, keyVal):
# Update the character to be our standard Alphabet mapping
# A -> 0; B->1 ... Z -> 25
pythonwhile循環3次。x = ord(valToDecrypt) - ord('A')
retVal = (x - keyVal) % 26
# Translate back to the standard ASCII mapping of the character
# for display in python and translate it back into a string
retVal = chr(retVal + ord('A'))
python遍歷循環、return retVal
要加密,你可以使用下面的函數的字符串: 從重新進口蘇b def encrypt_message(消息,密鑰): #將消息文本轉換為純文本,其中包含所有空格,并刪除 #標點符號。 明文=子(R '[^ AZ]', '',message.upper()) 密文= “”
charIndex = 0
# Encrypt the message 1 character at a time
while charIndex < len(plainText):
python雙重for循環、cipherText += \
encrypt_character(plainText[charIndex], key)
charIndex += 1
return cipherText
此功能可被稱為:
python循環結構,>>> encrypt_message("HELLO World!", key=23)
'EBIILTLOIA'
解密函數與加密函數非常相似,除了調用解密實用程序而不是加密實用程序。
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态