xor在线解密_解密单字节XOR密文
xor在線解密
Encryption is a process of encoding messages such that it can only be read and understood by the intended parties. The process of extracting the original message from an encrypted one is called Decryption. Encryption usually scrambles the original message using a key, called encryption key, that the involved parties agree on.
加密是對消息進行編碼的過程,以使其只能由預期的參與者讀取和理解。 從加密的消息中提取原始消息的過程稱為解密。 加密通常使用涉及各方同意的稱為加密密鑰的密鑰來加密原始消息。
The strength of an encryption algorithm is determined by how hard it would be to extract the original message without knowing the encryption key. Usually, this depends on the number of bits in the key — bigger the key, the longer it takes to decrypt the enciphered data.
加密算法的強度取決于在不知道加密密鑰的情況下提取原始消息的難度。 通常,這取決于密鑰中的位數-密鑰越大,解密加密數據所需的時間越長。
In this essay, we will work with a very simple cipher (encryption algorithm) that uses an encryption key with a size of one byte, and try to decipher the ciphered text and retrieve the original message without knowing the encryption key. The problem statement, defined above, is based on Cryptopals Set 1 Challenge 3.
在本文中,我們將使用一個非常簡單的密碼(加密算法),該密碼使用大小為一個字節的加密密鑰,并嘗試對加密的文本進行解密,并在不知道加密密鑰的情況下檢索原始消息。 上面定義的問題陳述是基于Cryptopals Set 1 Challenge 3的 。
單字節XOR密碼 (Single-byte XOR cipher)
The Single-byte XOR cipher algorithm works with an encryption key of size 1 byte — which means the encryption key could be one of the possible 256 values of a byte. Now we take a detailed look at how the encryption and decryption processes look like for this cipher.
單字節XOR密碼算法可使用大小為1字節的加密密鑰-這意味著該加密密鑰可以是一個字節的256個值之一。 現在,我們詳細研究此密碼的加密和解密過程。
加密 (Encryption)
As part of the encryption process, the original message is iterated bytewise and every single byte b is XORed with the encryption key key and the resultant stream of bytes is again translated back as characters and sent to the other party. These encrypted bytes need not be among the usual printable characters and should ideally be interpreted as a stream of bytes. Following is the python-based implementation of the encryption process.
作為加密過程的一部分,原始消息將按字節進行迭代,并且每個單個字節b與加密密鑰key進行XOR運算,然后再將生成的字節流轉換為字符并發送給另一方。 這些加密的字節不必在通常的可打印字符中,并且理想情況下應解釋為字節流。 以下是基于python的加密過程的實現。
As an example, we can try to encrypt the plain text — abcd - with encryption key 69 and as per the algorithm, we perform XOR bytewise on the given plain text. For character a, the byte i.e. ASCII value is 97 which when XORed with 69 results in 36 whose character equivalent is $, similarly for b the encrypted byte is ', for c it is & and for d it is !. Hence when abcd is encrypted using single-byte XOR cipher and encryption key 69, the resultant ciphertext i.e. the encrypted message is $'&!.
例如,我們可以嘗試使用加密密鑰69對純文本abcd進行加密,并按照算法對給定的純文本按字節執行XOR。 對于字符a ,字節(即ASCII值)為97 ,當與69進行異或運算時,將得出36字符等效為$的字符,類似地,對于b ,加密字節為' ,對于c為& ,對于d為! 。 因此,當使用單字節XOR密碼和加密密鑰69加密abcd時,得到的密文,即加密消息為$'&! 。
解密 (Decryption)
Decryption is the process of extracting the original message from the encrypted ciphertext given the encryption key. XOR has a property — if a = b ^ c then b = a ^ c, hence the decryption process is exactly the same as the encryption i.e. we iterate through the encrypted message bytewise and XOR each byte with the encryption key - the resultant will be the original message.
解密是從給定了加密密鑰的加密密文中提取原始消息的過程。 XOR具有一個屬性 -如果a = b ^ c則b = a ^ c ,因此解密過程與加密完全相同,即我們逐字節遍歷加密的消息,然后對每個字節與加密密鑰進行XOR-結果將是原始消息。
Since encryption and decryption both have an exact same implementation — we pass the ciphertext to the function single_byte_xor, defined above, to get the original message back.
由于加密和解密都具有完全相同的實現-我們將密文傳遞給上面定義的函數single_byte_xor ,以取回原始消息。
沒有加密密鑰的解密 (Deciphering without the encryption key)
Things become really interesting when we have to recover the original message given the ciphertext and having no knowledge of the encryption key; although we do know the encryption algorithm.
當我們必須在給定密文且不知道加密密鑰的情況下恢復原始消息時,事情變得非常有趣。 盡管我們確實知道加密算法。
As a sample plain text, we take the last couple of messages, sent across on their German military radio network during World War II. These messages were intercepted and decrypted by the British troops. During wartime, the messages were encrypted using Enigma Machine and Alan Turing famously cracked the Enigma Code (similar to encryption key) that was used to encipher German messages.
作為純文本的示例,我們以在第二次世界大戰期間通過其德國軍事廣播網絡發送的最后幾條消息為例。 這些消息被英軍截獲并解密。 在戰時,消息使用Enigma Machine進行了加密, Alan Turing著名地破解了用于加密德國消息的Enigma Code (類似于加密密鑰)。
In this essay, instead of encrypting the message using the Enigma Code, we are going to use Single-byte XOR cipher and try to recover the original message back without any knowledge of the encryption key.
在本文中,我們將使用單字節XOR密碼并嘗試在不了解加密密鑰的情況下恢復原始消息,而不是使用Enigma Code對消息進行加密。
Here, we assume that the original message, to be encrypted, is a genuine English lowercased sentence. The ciphertext that we would try to decipher can be obtained as
在這里,我們假設要加密的原始消息是一個純正的英語小寫句子。 我們將嘗試解密的密文可以作為
蠻力 (Bruteforce)
There are a very limited number of possible encryption keys — 256 to be exact — we can, very conveniently, go for the Bruteforce approach and try to decrypt the ciphered text with every single one of it. So we start iterating over all keys in the range [0, 256) and decrypt the ciphertext and see which one resembles the original message the most.
可能的加密密鑰數量非常有限(準確地說是256個),我們可以非常方便地采用Bruteforce方法,并嘗試對每個加密文本進行解密。 因此,我們開始對[0, 256) 0,256]范圍內的所有密鑰進行迭代[0, 256)并對密文進行解密,然后看看哪個密鑰與原始消息最相似。
In the illustration above, we see that the message decrypted through key 82 is, in fact, our original message, while the other retrieved plain texts look scrambled and garbage. Doing this visually is very easy; we, as humans, are able to comprehend familiarity but how will a computer recognize this?
在上面的插圖中,我們看到通過密鑰82解密的消息實際上是我們的原始消息,而其他檢索到的純文本看上去卻是混亂而無用的。 視覺上做到這一點非常容易; 作為人類,我們能夠理解熟悉度,但是計算機將如何識別它?
We need a way to quantify the closeness of a text to a genuine English sentence. Closer the decrypted text is to be a genuine English sentence, the closer it would be to our original plain text.
我們需要一種量化文本與真實英語句子的接近程度的方法。 解密后的文本越接近真實的英語句子,就越接近我們的原始純文本。
We can do this only because of our assumption — that the original plain text is a genuine English sentence.
我們只能基于我們的假設才能做到這一點-原始純文本是正版的英語句子。
依托因 (ETAOIN SHRDLU)
Letter Frequency is the number of times letters of an alphabet appear on average in written language. In the English language the letter frequency of letter a is 8.239%, for b it is 1.505% which means out of 100 letters written in English, the letter a, on an average, will show up 8.239% of times while b shows up 1.505% of times. Letter frequency (in percentage) for other letters is as shown below.
字母頻率是指字母平均以書面語言出現的次數。 在英語中,字母a的字母頻率為8.239% ,而b的字母頻率為1.505% ,這意味著在100個以英語書寫的字母中,字母a的平均出現8.239%為8.239% ,而b 8.239% 1.505%的時間。 其他字母的字母頻率(百分比)如下所示。
This Letter Frequency analysis is a rudimentary way for language identification in which we see if the current letter frequency distribution of a text matches the average letter frequency distribution of the English language. ETAOIN SHRDLU is the approximate order of frequency of the 12 most commonly used letters in the English language.
這種字母頻率分析是一種基本的語言識別方法,其中我們可以查看文本的當前字母頻率分布是否與英語的平均字母頻率分布相匹配。 ETAOIN SHRDLU是英語中最常用的12個字母的頻率順序。
The following chart shows Letter Frequency analysis for decrypted plain texts with encryption keys from 79 to 84.
下表顯示了使用79至84加密密鑰的解密純文本的字母頻率分析。
In the illustration above, we could clearly see how well the Letter Frequency distribution for encryption key 82 fits the distribution of the English language. Now that our hypothesis holds true, we need a way to quantify this measure and we call if the Fitting Quotient.
在上面的插圖中,我們可以清楚地看到加密密鑰82的字母頻率分布與英語分布的匹配程度。 現在我們的假設成立,我們需要一種量化該度量的方法,我們稱其為擬合商。
擬合商 (Fitting Quotient)
Fitting Quotient is the measure that suggests how well the two Letter Frequency Distributions match. Heuristically, we define the Fitting Quotient as the average of the absolute difference between the frequencies (in percentage) of letters in text and the corresponding letter in the English Language. Thus having a smaller value of Fitting Quotient implies the text is closer to the English Language.
擬合商是表明兩個字母頻率分布匹配程度的度量。 試探性地,我們將擬合商定義為text中字母和英語中對應字母的頻率(百分比)之間的絕對差的平均值。 因此,具有較小的“擬合商”值意味著文本更接近英語。
Python-based implementation of the, above defined, Fitting Quotient is as shown below. The function first computes the relative frequency for each letter in text and then takes an average of the absolute difference between the two distributions.
上面定義的擬合商的基于Python的實現如下所示。 該函數首先計算text每個字母的相對頻率,然后取兩個分布之間絕對差的平均值。
解密 (Deciphering)
Now that we have everything we require to directly get the plain text out of the given ciphertext we wrap it in a function that iterates over all possible encryption keys in the range [0, 256), decrypts the ciphertext, computes the fitting quotient for the plain text and returns the one that minimizes the quotient as the original message. Python-based implementation of this deciphering logic is as illustrated below.
現在,我們有我們需要直接的一切得到明文出給定的密文,我們在一個函數,用來在范圍內的所有可能的加密密鑰迭代把它包[0, 256)解密密文,計算了配件商純文本,并返回將商數最小化的原始文本。 解密邏輯的基于Python的實現如下所示。
This approach was also tested against 100 random English sentences with random Encryption keys and it was found that this deciphering technique fared well for all the samples. The approach would fail if the sentence is very short or contains a lot of symbols. The source code for this entire deciphering process is available in a Jupyter notebook at arpitbhayani.me/decipher-single-byte-xor.
還針對帶有隨機加密密鑰的100個隨機英語句子測試了該方法,發現該解密技術對所有樣本都表現良好。 如果句子很短或包含很多符號,則該方法將失敗。 Jupyter筆記本中的arpitbhayani.me/decipher-single-byte-xor提供了整個解密過程的源代碼。
翻譯自: https://medium.com/@often_weird/deciphering-single-byte-xor-ciphertexts-6767d0a57de4
xor在線解密
總結
以上是生活随笔為你收集整理的xor在线解密_解密单字节XOR密文的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言中如何用xor,C语言如何使用异或
- 下一篇: python中xor是什么_python