caesar加密_如何编写Caesar密码:基本加密简介
caesar加密
by Brendan Massey
由布倫丹·梅西(Brendan Massey)
The Caesar Cipher is a famous implementation of early day encryption. It would take a sentence and reorganize it based on a key that is enacted upon the alphabet. Take, for example, a key of 3 and the sentence, “I like to wear hats.”
凱撒密碼是早期加密的著名實現。 它需要一個句子,并根據字母上的鍵重新組織它。 例如,以鍵3和句子“我喜歡戴帽子”為例。
When this sentence is encrypted using a key of 3, it becomes:
當使用3密鑰加密此句子時,它變為:
L olnh wr zhdu kdwv.
L olnh wr zhdu kdwv。
This makes it difficult to read and allows messages to be passed undetected.
這使閱讀變得困難,并且使郵件無法被檢測到傳遞。
While this is a very simple example of encryption, it is a perfect project for someone learning to code to practice on.
盡管這是一個非常簡單的加密示例,但對于學習編程的人來說,這是一個完美的項目。
了解密碼 (Understanding the cipher)
To implement this code, at least in JAVA, you would need to think through what is actually being done. So, let’s look at the steps necessary to take in order to code this.
要至少在JAVA中實現此代碼,您需要仔細考慮實際要做的事情。 因此,讓我們看看進行編碼的必要步驟。
Step 1: Identify the character within the sentence.
步驟1:識別句子中的字符。
Step 2: Find that character’s location within the alphabet.
第2步:找到該字符在字母表中的位置。
Step 3: Identify that characters location + the key in the alphabet.
步驟3:確定字符位置+字母中的鍵。
Note* if the location + key > 26, loop back around and begin counting at one.
注意*如果位置+鍵> 26,則返回并開始計數。
Step 4: Build a new sentence using the new characters in place of the original characters.
步驟4:使用新字符代替原始字符來構建新句子。
Step 5: repeat until sentence length is reached. (For loop).
步驟5:重復直到達到句子長度。 (用于循環)。
Step 6: return result.
步驟6:返回結果。
加密密碼 (Coding the cipher)
While those are pretty good steps to follow through with, we should think of what we would need to do in code.
盡管這些都是非常好的步驟,但是我們應該考慮在代碼中需要做什么。
Step 0: Establish a function that reads in a message and a key.
步驟0:建立讀取消息和密鑰的功能。
Something like this:
像這樣:
public String Encrypt(String message, int key) {}Step 1: Identify the character within the sentence.
步驟1:識別句子中的字符。
To do this, we will need to establish an alphabet to look at.
為此,我們需要建立一個字母進行查看。
Establish a variable “alphabet” that consists of the 26 letters of the alphabet.
建立一個由字母的26個字母組成的變量“字母”。
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";String alphabet2 = alphabet.toLowerCase();Step 2: Find that character’s location within the alphabet.
第2步:找到該字符在字母表中的位置。
Then create a for loop that runs through every character within the message. It will be easier to do this if we establish a StringBuilder.
然后創建一個遍歷消息中每個字符的for循環。 如果我們建立一個StringBuilder,這樣做會更容易。
StringBuilder encrypted = new StringBuilder(message);for (int q = 0; q < encrypted.length(); q++) { char currchar = encrypted.charAt(q); int index = alphabet.indexOf(currchar);}At this point, we should make sure that the spot is a letter.
在這一點上,我們應該確保現貨是字母。
if (index != -1) {}Step 3: Identify that character’s location + the key in the alphabet.
步驟3:確定人物的位置+字母中的鍵。
If it is a letter, then we have to find the spot in the modified alphabet. We have not yet established a modified alphabet variable, so we should do that now.
如果是字母,則必須在修改后的字母中找到該點。 我們尚未建立修改后的字母變量,因此我們現在應該這樣做。
Step 4: Build a new sentence using the new characters in place of the original characters.
步驟4:使用新字符代替原始字符來構建新句子。
Once we have found the value in the modified alphabet, we should set it to the same location in the StringBuilder we created.
在修改后的字母中找到該值后,應將其設置為我們創建的StringBuilder中的相同位置。
public String Encryption(String input, int key){ StringBuilder encrypted = new StringBuilder(input); String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; String alphabet2 = alphabet.toLowerCase(); String keyedalphabet = alphabet.substring(key) + alphabet.substring(0, key);for (int q = 0; q < encrypted.length(); q++) { char currChar = encrypted.charAt(q); int index = alphabet.indexOf(currChar); if (index != -1) { char newChar = keyedalphabet.charAt(index); encrypted.setCharAt(q, newChar); }Step 5: repeat until sentence length is reached. (For loop)
步驟5:重復直到達到句子長度。 (用于循環)
Now, we have checked if the character is upper-case, but we also need to check if the character is lower-case. To do this, we need to access alphabet2 that we established earlier on.
現在,我們檢查了字符是否為大寫字母,但我們還需要檢查字符是否為小寫字母。 為此,我們需要訪問我們之前建立的alphabet2。
index = alphabet2.indexOf(currChar); if (index != -1) { String keyedalphabet2 = keyedalphabet.toLowerCase(); char newChar = keyedalphabet2.charAt(index); encrypted.setCharAt(q, newChar); }Step 6: return result.
步驟6:返回結果。
Now, we have completed the For loop. All we have left is to exit it and return the String.
現在,我們已經完成了For循環。 我們剩下的就是退出它并返回String。
public String Encryption(String input, int key){ StringBuilder encrypted = new StringBuilder(input); String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; String alphabet2 = alphabet.toLowerCase(); String keyedalphabet = alphabet.substring(key) + alphabet.substring(0, key); for (int q = 0; q < encrypted.length(); q++) { char currChar = encrypted.charAt(q); int index = alphabet.indexOf(currChar); if (index != -1) { char newChar = keyedalphabet.charAt(index); encrypted.setCharAt(q, newChar); } index = alphabet2.indexOf(currChar); if (index != -1) { String keyedalphabet2 = keyedalphabet.toLowerCase(); char newChar = keyedalphabet2.charAt(index); encrypted.setCharAt(q, newChar); } } return encrypted }Step 7: Debug.
第7步:調試。
But wait! That won’t work! encrypted is not a String, it is a StringBuilder and this function specifically requires a String to be returned!
可是等等! 那行不通! 加密的不是字符串,而是StringBuilder,此函數特別要求返回字符串!
Luckily, there is a very simple function to remedy this oversight.
幸運的是,有一個非常簡單的功能可以彌補這種疏忽。
public String Encryption(String input, int key){ StringBuilder encrypted = new StringBuilder(input); String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; String alphabet2 = alphabet.toLowerCase(); String keyedalphabet = alphabet.substring(key) + alphabet.substring(0, key); for (int q = 0; q < encrypted.length(); q++) { char currChar = encrypted.charAt(q); int index = alphabet.indexOf(currChar); if (index != -1) { char newChar = keyedalphabet.charAt(index); encrypted.setCharAt(q, newChar); } index = alphabet2.indexOf(currChar); if (index != -1) { String keyedalphabet2 = keyedalphabet.toLowerCase(); char newChar = keyedalphabet2.charAt(index); encrypted.setCharAt(q, newChar); } } return encrypted.toString(); }That is how you get the encrypted version of your original sentence. Try it for yourself!
這就是您獲取原始句子的加密版本的方式。 自己嘗試一下!
Thank you for reading!
感謝您的閱讀!
翻譯自: https://www.freecodecamp.org/news/how-to-code-the-caesar-cipher-an-introduction-to-basic-encryption-3bf77b4e19f7/
caesar加密
總結
以上是生活随笔為你收集整理的caesar加密_如何编写Caesar密码:基本加密简介的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到什么预示考试成绩好
- 下一篇: 梦到被蟒蛇吃了是什么意思