本将开始一个漫长的翻译过程,初次翻译,翻译有误的地方,请指出……看的不明白的看原文。
本人将对RUBY QUIZ的一些测试进行翻译,并且对RUBY语言进行探讨和研究。
那么,现在就开始吧!朋友们!
The Solitaire Cipher
Cryptologist Bruce Schneier designed the hand cipher "Solitaire" for Neal Stephenson's book "Cryptonomicon". Created to be the first truly secure hand cipher, Solitaire requires only a deck of cards for the encryption and decryption of messages.
While it's true that Solitaire is easily completed by hand given ample time, using a computer is much quicker and easier. Because of that, Solitaire conversion routines are available in many languages, though I've not yet run across one in Ruby.
This week's quiz is to write a Ruby script that does the encryption and decryption of messages using the Solitaire cipher.
Let's look at the steps of encrypting a message with Solitaire.
1. Discard any non A to Z characters, and uppercase all remaining letters. Split the message into five character groups, using Xs to pad the last group, if needed. If we begin with the message "Code in Ruby, live longer!", for example, we would now have:
1.丢弃任何非A到Z字,和所有其余的字母大写。分解信息分为五字母为一组,当不够时,用X来填充最后一组。如果我们开始用信息“Code in Ruby, live longer! ” ,例如,我们现在有:
CODEI NRUBY LIVEL ONGER
2. Use Solitaire to generate a keystream letter for each letter in the message. This step is detailed below, but for the sake of example let's just say we get:
2.使用纸牌生成密钥信的每个字母的信息。这在一步下面详细列出,而是为了例子的原因让我们只想说我们得到:
DWJXH YRFDG TMSHP UURXJ
3. Convert the message from step 1 into numbers, A = 1, B = 2, etc:
3.转换第一个步骤的信息为号码, A = 1 ,B= 2 ,等等:
3 15 4 5 9 14 18 21 2 25 12 9 22 5 12 15 14 7 5 18
4. Convert the keystream letters from step 2 using the same method:
4.使用相同的方法转换第2步的密钥信:
4 23 10 24 8 25 18 6 4 7 20 13 19 8 16 21 21 18 24 10
5. 第五步:把第三步生成的数量加上第四步生成的数字,如果其大小大于26,那么就减去26,比如,加起来 6+10,那么就 16,如果 26+1,那么就等于27-26,即为1:
7 12 14 3 17 13 10 1 6 6 6 22 15 13 2 10 9 25 3 2
6. Convert the numbers from step 5 back to letters:
6.把步骤5的算出来的信息转换成字母:
GLNCQ MJAFF FVOMB JIYCB
That took a while to break down, but it's really a very simple process. Decrypting with Solitaire is even easier, so let's look at those steps now. We'll work backwards with our example now, decrypting "GLNCQ MJAFF FVOMB JIYCB".
而且在这方面花了一段时间来打破,但它上是一个非常简单的过程。用纸牌算法解密更加容易,所以让我们看看现在这些步骤。现在我们将继续用我们的实例,解密“ GLNCQ MJAFF FVOMB JIYCB ” 。
1. Use Solitaire to generate a keystream letter for each letter in the message to be decoded. Again, I detail this process below, but sender and receiver use the same key and will get the same letters:
1.使用纸牌算法生成密钥信的每个字母的信息进行解码。同样,我在下面详细说明这一过程,但发送和接收使用相同的钥匙,并会得到相同的信息:
DWJXH YRFDG TMSHP UURXJ
2. Convert the message to be decoded to numbers:
2.把信息转换,译码为数字。
7 12 14 3 17 13 10 1 6 6 6 22 15 13 2 10 9 25 3 2
3. Convert the keystream letters from step 1 to numbers:
3.转换第1步的字幕信息为数字:
4 23 10 24 8 25 18 6 4 7 20 13 19 8 16 21 21 18 24 10
4. Subtract the keystream numbers from step 3 from the message numbers from step 2. If the message number is less than or equal to the keystream number, add 26 to the message number before subtracting. For example, 22 - 1 = 21 as expected, but 1 - 22 = 5 (27 - 22):
4. 从信息中第二步的得出的数字减第三步的的出的数字,如果小于等于0,就用26减去得出的结果:
3 15 4 5 9 14 18 21 2 25 12 9 22 5 12 15 14 7 5 18
5.再次将数字串换为字幕:
CODEI NRUBY LIVEL ONGER
Transforming messages is that simple. Finally, let's look at the missing piece of the puzzle, generating the keystream letters.
转换信息就是这么简单。最后,让我们来看看这个地方的困难所在,生成密钥字母。
1. Key the deck. This is the critical step in the actual operation of the cipher and the heart of it's security. There are many methods to go about this, such as shuffling a deck and then arranging the receiving deck in the same order or tracking a bridge column in the paper and using that to order the cards. Because we want to be able to test our answers though, we'll use an unkeyed deck, cards in order of value. That is, from top to bottom, we'll always start with the deck:
1 2 3 ... 52 A B
To:
1 2 3 ... 52 B A
3. Move the B joker down two cards. If the joker is the bottom card, move it just below the second card. If the joker is the just above the bottom card, move it below the top card. (Again, consider the deck to be circular.) This changes our example deck to:
3.移动B大王向下一卡片。如果大王在一副牌的底部,将它移到为第二张牌。如果大王高于底部牌,将其移动低端牌。 (再次,考虑一副牌是循环。 )这改变我们举例为:
1 B 2 3 4 ... 52 A
4. Perform a triple cut around the two jokers. All cards above the top joker move to below the bottom joker and vice versa. The jokers and the cards between them do not move. This gives us:
5. Perform a count cut using the value of the bottom card. Cut the bottom card's value in cards off the top of the deck and reinsert them just above the bottom card. This changes our deck to:
2 3 4 ... 52 A B 1 (the 1 tells us to move just the B)
6. Find the output letter. Convert the top card to it's value and count down that many cards from the top of the deck, with the top card itself being card number one. Look at the card immediately after your count and convert it to a letter. This is the next letter in the keystream. If the output card is a joker, no letter is generated this sequence. This step does not alter the deck. For our example, the output letter is:
D (the 2 tells us to count down to the 4, which is a D)
7. Return to step 2, if more letters are needed.
For the sake of testing, the first ten output letters for an unkeyed deck are:
7 。返回到步骤2中,如果有更多的信件是必要的。
为了试验,产量前十位的字母unkeyed甲板是:
D (4) W (49) J (10) Skip Joker (53) X (24) H (8)
Y (51) R (44) F (6) D (4) G (33)
That's all there is to Solitaire, finally. It's really longer to explain than it is to code up.
Solutions to this quiz should accept a message as a command line argument and encrypt or decrypt is as needed. It should be easy to tell which is needed by the pattern of the message, but you can use a switch if you prefer.
All the examples for this quiz assume an unkeyed deck, but your script can provide a way to key the deck, if desired. (A real script would require this, of course.)
Here's a couple of messages to test your work with. You'll know when you have them right:
CLEPK HHNIY CFPWH FDFEH
ABVAW LWZSY OORYK DUPVH