python數據加密代碼,python程序加密_Python簡單的加密程序:如何循環Z回到A

 2023-10-20 阅读 26 评论 0

摘要:我會推薦使用模數運算符來做你想要的。在python中是%字符。在模數學中。 X%Y告訴我們X/Y的剩余部分是什么。例如。 27%26是1.使用這個你可以得到你想要的包裝。這里是一個代碼示例位,如果我們喂字符“A”進入我們的加密算法的13關鍵,我們得

我會推薦使用模數運算符來做你想要的。在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'

解密函數與加密函數非常相似,除了調用解密實用程序而不是加密實用程序。

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/152764.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息