From 2076c18cf4e6e4f213225df08cb4381567d6977d Mon Sep 17 00:00:00 2001 From: Tom Leen Date: Tue, 5 Sep 2017 20:18:15 -0400 Subject: [PATCH 1/5] simple-cipher: update README to reflect exercise implementation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Clear up the expected implementation details for the simple-cipher exercise in the README. Provide more explanation of the Vigenère cipher. Moved README updates into .meta/ directory and regenerated the README. Resolves #786 --- exercises/simple-cipher/.meta/description.md | 100 ++++++++++++++ exercises/simple-cipher/.meta/hints.md | 35 +++++ exercises/simple-cipher/.meta/metadata.yml | 4 + exercises/simple-cipher/README.md | 129 +++++++++++++----- exercises/simple-cipher/simple_cipher_test.go | 30 ---- 5 files changed, 231 insertions(+), 67 deletions(-) create mode 100644 exercises/simple-cipher/.meta/description.md create mode 100644 exercises/simple-cipher/.meta/hints.md create mode 100644 exercises/simple-cipher/.meta/metadata.yml diff --git a/exercises/simple-cipher/.meta/description.md b/exercises/simple-cipher/.meta/description.md new file mode 100644 index 000000000..44a9b06d2 --- /dev/null +++ b/exercises/simple-cipher/.meta/description.md @@ -0,0 +1,100 @@ +Implement a simple shift cipher like Caesar and a more secure +substitution cipher. + +## Step 1 + +"If he had anything confidential to say, he wrote it in cipher, that +is, by so changing the order of the letters of the alphabet, that not +a word could be made out. If anyone wishes to decipher these, and get +at their meaning, he must substitute the fourth letter of the +alphabet, namely D, for A, and so with the others." —Suetonius, Life +of Julius Caesar + +Ciphers are very straight-forward algorithms that allow us to render +text less readable while still allowing easy deciphering. They are +vulnerable to many forms of cryptoanalysis, but we are lucky that +generally our little sisters are not cryptoanalysts. + +The Caesar Cipher was used for some messages from Julius Caesar that +were sent afield. Now Caesar knew that the cipher wasn't very good, +but he had one ally in that respect: almost nobody could read well. So +even being a couple letters off was sufficient so that people couldn't +recognize the few words that they did know. + +Your task is to create a simple shift cipher like the Caesar Cipher. +This image is a great example of the Caesar Cipher: + +![Caesar Cipher][1] + +For example: + +Giving "iamapandabear" as input to the encode function returns the +cipher "ldpdsdqgdehdu". Obscure enough to keep our message secret in +transit. + +When "ldpdsdqgdehdu" is put into the decode function it would return +the original "iamapandabear" letting your friend read your original +message. + +Initially you will implement a [Caesar Cipher](cc) with a fixed shift +distance of 3 (*namely D, for A*). + +## Step 2 + +Fixed distance Shift Ciphers are no fun though when your kid sister +figures it out. Try amending the code to allow us to specify a shift +distance. + +You will implement a more generic Shift Cipher with a flexible shift +distance. + +# Step 3 + +With only 26 true possible shift values, your kid sister will figure +this out too. Next lets define a more complex cipher using a string as +key value: a [Vigenère cipher](vc). + +Here's an example: + +Given the key "aaaaaaaaaaaaaaaaaa", encoding the string +"iamapandabear" would return the original "iamapandabear". + +Given the key "ddddddddddddddddd", encoding our string "iamapandabear" +would return the obscured "ldpdsdqgdehdu" + +In the example above, we've set a = 0 for all the key values, with a +shift distance of 0. So when the plaintext is added to the key, we end +up with the same message coming out. So "aaaa" is not an ideal +key. But if we set the key to "dddd", we would get the same thing as +the Shift Cipher with all shift distances set to 4. + +This is not much of an improvement over the fixed distance Shift +Cipher. However, we can put many different lengths into the key if we +use strings with different characters: + +Given the key "adadadadad", encoding the string "iamapandabear" would +return the obscured "idmdpdngaeedr". + +Each character in the key is used to shift the corresponding character +by index. If the key is shorter than the text, repeat the key as +needed. + +For the final step you will implement a Vigenère cipher. + +## Extensions + +Shift ciphers work by making the text slightly odd, but are vulnerable +to frequency analysis. Substitution ciphers help that, but are still +very vulnerable when the key is short or if spaces are +preserved. You'll see one solution to this problem in the exercise +"crypto-square". + +If you want to go farther in this field, the questions begin to be +about how we can exchange keys in a secure way. Take a look at +[Diffie-Hellman on Wikipedia][dh] for one of the first implementations +of this scheme. + +[1]: https://upload.wikimedia.org/wikipedia/commons/thumb/4/4a/Caesar_cipher_left_shift_of_3.svg/320px-Caesar_cipher_left_shift_of_3.svg.png +[cc]: https://en.wikipedia.org/wiki/Caesar_cipher +[vc]: https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher +[dh]: https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange diff --git a/exercises/simple-cipher/.meta/hints.md b/exercises/simple-cipher/.meta/hints.md new file mode 100644 index 000000000..f0a6198a6 --- /dev/null +++ b/exercises/simple-cipher/.meta/hints.md @@ -0,0 +1,35 @@ +## Implementation + +The definition of the Cipher interface is located in +[cipher.go](./cipher.go). + +Your implementations should conform to the Cipher interface. + +```go +type Cipher interface { + Encode(string) string + Decode(string) string +} +``` + +It is expected that `Encode` will ignore all values in the string that +are not A-Za-z, they will not be represented in the output. The output +will be also normalized to lowercase. + +The functions used to obtain the ciphers are: + +```go +func NewCaesar() Cipher + +func NewShift(distance int) Cipher + +func NewVigenere(key string) Cipher +``` + +Argument for `NewShift` must be in the range 1 to 25 or -1 to -25. +Zero is disallowed. For invalid arguments `NewShift` returns nil. + +Argument for `NewVigenere` must consist of lower case letters a-z +only. Values consisting entirely of the letter 'a' are disallowed. +For invalid arguments `NewVigenere` returns nil. + diff --git a/exercises/simple-cipher/.meta/metadata.yml b/exercises/simple-cipher/.meta/metadata.yml new file mode 100644 index 000000000..7ca04f8f8 --- /dev/null +++ b/exercises/simple-cipher/.meta/metadata.yml @@ -0,0 +1,4 @@ +--- +blurb: "Implement a simple shift cipher like Caesar and a more secure substitution cipher" +source: "Substitution Cipher at Wikipedia" +source_url: "http://en.wikipedia.org/wiki/Substitution_cipher" diff --git a/exercises/simple-cipher/README.md b/exercises/simple-cipher/README.md index 5394c733d..1688c2459 100644 --- a/exercises/simple-cipher/README.md +++ b/exercises/simple-cipher/README.md @@ -1,15 +1,16 @@ # Simple Cipher -Implement a simple shift cipher like Caesar and a more secure substitution cipher. +Implement a simple shift cipher like Caesar and a more secure +substitution cipher. ## Step 1 -"If he had anything confidential to say, he wrote it in cipher, that is, -by so changing the order of the letters of the alphabet, that not a word -could be made out. If anyone wishes to decipher these, and get at their -meaning, he must substitute the fourth letter of the alphabet, namely D, -for A, and so with the others." -—Suetonius, Life of Julius Caesar +"If he had anything confidential to say, he wrote it in cipher, that +is, by so changing the order of the letters of the alphabet, that not +a word could be made out. If anyone wishes to decipher these, and get +at their meaning, he must substitute the fourth letter of the +alphabet, namely D, for A, and so with the others." —Suetonius, Life +of Julius Caesar Ciphers are very straight-forward algorithms that allow us to render text less readable while still allowing easy deciphering. They are @@ -17,9 +18,9 @@ vulnerable to many forms of cryptoanalysis, but we are lucky that generally our little sisters are not cryptoanalysts. The Caesar Cipher was used for some messages from Julius Caesar that -were sent afield. Now Caesar knew that the cipher wasn't very good, but -he had one ally in that respect: almost nobody could read well. So even -being a couple letters off was sufficient so that people couldn't +were sent afield. Now Caesar knew that the cipher wasn't very good, +but he had one ally in that respect: almost nobody could read well. So +even being a couple letters off was sufficient so that people couldn't recognize the few words that they did know. Your task is to create a simple shift cipher like the Caesar Cipher. @@ -29,59 +30,113 @@ This image is a great example of the Caesar Cipher: For example: -Giving "iamapandabear" as input to the encode function returns the cipher "ldpdsdqgdehdu". Obscure enough to keep our message secret in transit. +Giving "iamapandabear" as input to the encode function returns the +cipher "ldpdsdqgdehdu". Obscure enough to keep our message secret in +transit. When "ldpdsdqgdehdu" is put into the decode function it would return the original "iamapandabear" letting your friend read your original message. +Initially you will implement a [Caesar Cipher](cc) with a fixed shift +distance of 3 (*namely D, for A*). + ## Step 2 -Shift ciphers are no fun though when your kid sister figures it out. Try -amending the code to allow us to specify a key and use that for the -shift distance. This is called a substitution cipher. +Fixed distance Shift Ciphers are no fun though when your kid sister +figures it out. Try amending the code to allow us to specify a shift +distance. + +You will implement a more generic Shift Cipher with a flexible shift +distance. + +# Step 3 + +With only 26 true possible shift values, your kid sister will figure +this out too. Next lets define a more complex cipher using a string as +key value: a [Vigenère cipher](vc). Here's an example: -Given the key "aaaaaaaaaaaaaaaaaa", encoding the string "iamapandabear" -would return the original "iamapandabear". +Given the key "aaaaaaaaaaaaaaaaaa", encoding the string +"iamapandabear" would return the original "iamapandabear". Given the key "ddddddddddddddddd", encoding our string "iamapandabear" -would return the obscured "lpdsdqgdehdu" +would return the obscured "ldpdsdqgdehdu" -In the example above, we've set a = 0 for the key value. So when the -plaintext is added to the key, we end up with the same message coming -out. So "aaaa" is not an ideal key. But if we set the key to "dddd", we -would get the same thing as the Caesar Cipher. +In the example above, we've set a = 0 for all the key values, with a +shift distance of 0. So when the plaintext is added to the key, we end +up with the same message coming out. So "aaaa" is not an ideal +key. But if we set the key to "dddd", we would get the same thing as +the Shift Cipher with all shift distances set to 4. -## Step 3 +This is not much of an improvement over the fixed distance Shift +Cipher. However, we can put many different lengths into the key if we +use strings with different characters: -The weakest link in any cipher is the human being. Let's make your -substitution cipher a little more fault tolerant by providing a source -of randomness and ensuring that the key is not composed of numbers or -capital letters. +Given the key "adadadadad", encoding the string "iamapandabear" would +return the obscured "idmdpdngaeedr". -If someone doesn't submit a key at all, generate a truly random key of -at least 100 characters in length, accessible via Cipher#key (the # -syntax means instance variable) +Each character in the key is used to shift the corresponding character +by index. If the key is shorter than the text, repeat the key as +needed. -If the key submitted has capital letters or numbers, throw an -ArgumentError with a message to that effect. +For the final step you will implement a Vigenère cipher. ## Extensions Shift ciphers work by making the text slightly odd, but are vulnerable to frequency analysis. Substitution ciphers help that, but are still -very vulnerable when the key is short or if spaces are preserved. Later -on you'll see one solution to this problem in the exercise +very vulnerable when the key is short or if spaces are +preserved. You'll see one solution to this problem in the exercise "crypto-square". -If you want to go farther in this field, the questions begin to be about -how we can exchange keys in a secure way. Take a look at [Diffie-Hellman -on Wikipedia][dh] for one of the first implementations of this scheme. +If you want to go farther in this field, the questions begin to be +about how we can exchange keys in a secure way. Take a look at +[Diffie-Hellman on Wikipedia][dh] for one of the first implementations +of this scheme. [1]: https://upload.wikimedia.org/wikipedia/commons/thumb/4/4a/Caesar_cipher_left_shift_of_3.svg/320px-Caesar_cipher_left_shift_of_3.svg.png -[dh]: http://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange +[cc]: https://en.wikipedia.org/wiki/Caesar_cipher +[vc]: https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher +[dh]: https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange + +## Implementation + +The definition of the Cipher interface is located in +[cipher.go](./cipher.go). + +Your implementations should conform to the Cipher interface. + +```go +type Cipher interface { + Encode(string) string + Decode(string) string +} +``` + +It is expected that `Encode` will ignore all values in the string that +are not A-Za-z, they will not be represented in the output. The output +will be also normalized to lowercase. + +The functions used to obtain the ciphers are: + +```go +func NewCaesar() Cipher + +func NewShift(distance int) Cipher + +func NewVigenere(key string) Cipher +``` + +Argument for `NewShift` must be in the range 1 to 25 or -1 to -25. +Zero is disallowed. For invalid arguments `NewShift` returns nil. + +Argument for `NewVigenere` must consist of lower case letters a-z +only. Values consisting entirely of the letter 'a' are disallowed. +For invalid arguments `NewVigenere` returns nil. + + ## Running the tests diff --git a/exercises/simple-cipher/simple_cipher_test.go b/exercises/simple-cipher/simple_cipher_test.go index 489db9dce..cba6b4df9 100644 --- a/exercises/simple-cipher/simple_cipher_test.go +++ b/exercises/simple-cipher/simple_cipher_test.go @@ -1,33 +1,3 @@ -// For Step 1, implement the Caesar cipher, which seems clear enough except -// maybe for figuring out whether to add or subtract. -// -// For Step 2, implement a shift cipher, like Caesar except the shift amount -// is specified as an int. (Each letter of "ddddddddddddddddd", is 'd'; -// 'd'-'a' == 3, so the corresponding shift amount is 3.) -// -// Steps 2 and 3 seem to be describing the Vigenère cipher (Google it) -// so let's do that too. The random thing, don't worry about. There is -// no test for that. -// -// API: -// -// type Cipher interface { -// Encode(string) string -// Decode(string) string -// } -// NewCaesar() Cipher -// NewShift(int) Cipher -// NewVigenere(string) Cipher -// -// Interface Cipher is in file cipher.go. -// -// Argument for NewShift must be in the range 1 to 25 or -1 to -25. -// Zero is disallowed. For invalid arguments NewShift returns nil. -// -// Argument for NewVigenere must consist of lower case letters a-z only. -// Values consisting entirely of the letter 'a' are disallowed. -// For invalid arguments NewVigenere returns nil. - package cipher import ( From e6ceaf16508375512600524c0cf9c171b03d487f Mon Sep 17 00:00:00 2001 From: Tom Leen Date: Tue, 5 Sep 2017 20:23:47 -0400 Subject: [PATCH 2/5] simple-cipher: Fix bad links in README description --- exercises/simple-cipher/.meta/description.md | 4 ++-- exercises/simple-cipher/README.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/exercises/simple-cipher/.meta/description.md b/exercises/simple-cipher/.meta/description.md index 44a9b06d2..da8b32e4e 100644 --- a/exercises/simple-cipher/.meta/description.md +++ b/exercises/simple-cipher/.meta/description.md @@ -36,7 +36,7 @@ When "ldpdsdqgdehdu" is put into the decode function it would return the original "iamapandabear" letting your friend read your original message. -Initially you will implement a [Caesar Cipher](cc) with a fixed shift +Initially you will implement a [Caesar Cipher][cc] with a fixed shift distance of 3 (*namely D, for A*). ## Step 2 @@ -52,7 +52,7 @@ distance. With only 26 true possible shift values, your kid sister will figure this out too. Next lets define a more complex cipher using a string as -key value: a [Vigenère cipher](vc). +key value: a [Vigenère cipher][vc]. Here's an example: diff --git a/exercises/simple-cipher/README.md b/exercises/simple-cipher/README.md index 1688c2459..1b08cf7b9 100644 --- a/exercises/simple-cipher/README.md +++ b/exercises/simple-cipher/README.md @@ -38,7 +38,7 @@ When "ldpdsdqgdehdu" is put into the decode function it would return the original "iamapandabear" letting your friend read your original message. -Initially you will implement a [Caesar Cipher](cc) with a fixed shift +Initially you will implement a [Caesar Cipher][cc] with a fixed shift distance of 3 (*namely D, for A*). ## Step 2 @@ -54,7 +54,7 @@ distance. With only 26 true possible shift values, your kid sister will figure this out too. Next lets define a more complex cipher using a string as -key value: a [Vigenère cipher](vc). +key value: a [Vigenère cipher][vc]. Here's an example: From a520c3e597da94deba52e8d7b2502e80b8500da0 Mon Sep 17 00:00:00 2001 From: Tom Leen Date: Wed, 6 Sep 2017 11:06:13 -0400 Subject: [PATCH 3/5] Update README text --- exercises/simple-cipher/.meta/description.md | 6 ++---- exercises/simple-cipher/.meta/hints.md | 6 +++--- exercises/simple-cipher/README.md | 12 +++++------- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/exercises/simple-cipher/.meta/description.md b/exercises/simple-cipher/.meta/description.md index da8b32e4e..eb6d5d7a0 100644 --- a/exercises/simple-cipher/.meta/description.md +++ b/exercises/simple-cipher/.meta/description.md @@ -68,7 +68,7 @@ up with the same message coming out. So "aaaa" is not an ideal key. But if we set the key to "dddd", we would get the same thing as the Shift Cipher with all shift distances set to 4. -This is not much of an improvement over the fixed distance Shift +These keys are not much of an improvement over the fixed distance Shift Cipher. However, we can put many different lengths into the key if we use strings with different characters: @@ -79,15 +79,13 @@ Each character in the key is used to shift the corresponding character by index. If the key is shorter than the text, repeat the key as needed. -For the final step you will implement a Vigenère cipher. - ## Extensions Shift ciphers work by making the text slightly odd, but are vulnerable to frequency analysis. Substitution ciphers help that, but are still very vulnerable when the key is short or if spaces are preserved. You'll see one solution to this problem in the exercise -"crypto-square". +"[crypto-square](exercises/crypto-square/)". If you want to go farther in this field, the questions begin to be about how we can exchange keys in a secure way. Take a look at diff --git a/exercises/simple-cipher/.meta/hints.md b/exercises/simple-cipher/.meta/hints.md index f0a6198a6..66bc1d16c 100644 --- a/exercises/simple-cipher/.meta/hints.md +++ b/exercises/simple-cipher/.meta/hints.md @@ -19,11 +19,11 @@ will be also normalized to lowercase. The functions used to obtain the ciphers are: ```go -func NewCaesar() Cipher +func NewCaesar() Cipher { } -func NewShift(distance int) Cipher +func NewShift(distance int) Cipher { } -func NewVigenere(key string) Cipher +func NewVigenere(key string) Cipher { } ``` Argument for `NewShift` must be in the range 1 to 25 or -1 to -25. diff --git a/exercises/simple-cipher/README.md b/exercises/simple-cipher/README.md index 1b08cf7b9..fcd6d85db 100644 --- a/exercises/simple-cipher/README.md +++ b/exercises/simple-cipher/README.md @@ -70,7 +70,7 @@ up with the same message coming out. So "aaaa" is not an ideal key. But if we set the key to "dddd", we would get the same thing as the Shift Cipher with all shift distances set to 4. -This is not much of an improvement over the fixed distance Shift +These keys are not much of an improvement over the fixed distance Shift Cipher. However, we can put many different lengths into the key if we use strings with different characters: @@ -81,15 +81,13 @@ Each character in the key is used to shift the corresponding character by index. If the key is shorter than the text, repeat the key as needed. -For the final step you will implement a Vigenère cipher. - ## Extensions Shift ciphers work by making the text slightly odd, but are vulnerable to frequency analysis. Substitution ciphers help that, but are still very vulnerable when the key is short or if spaces are preserved. You'll see one solution to this problem in the exercise -"crypto-square". +"[crypto-square](exercises/crypto-square/)". If you want to go farther in this field, the questions begin to be about how we can exchange keys in a secure way. Take a look at @@ -122,11 +120,11 @@ will be also normalized to lowercase. The functions used to obtain the ciphers are: ```go -func NewCaesar() Cipher +func NewCaesar() Cipher { } -func NewShift(distance int) Cipher +func NewShift(distance int) Cipher { } -func NewVigenere(key string) Cipher +func NewVigenere(key string) Cipher { } ``` Argument for `NewShift` must be in the range 1 to 25 or -1 to -25. From 4da095e660babb5a48aff0eaa0750cd1b6f90a40 Mon Sep 17 00:00:00 2001 From: Tom Leen Date: Tue, 5 Sep 2017 20:18:15 -0400 Subject: [PATCH 4/5] simple-cipher: update README to reflect exercise implementation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Clear up the expected implementation details for the simple-cipher exercise in the README. Provide more explanation of the Vigenère cipher. Moved README updates into .meta/ directory and regenerated the README. Resolves #786 --- exercises/simple-cipher/.meta/description.md | 98 +++++++++++++ exercises/simple-cipher/.meta/hints.md | 35 +++++ exercises/simple-cipher/.meta/metadata.yml | 4 + exercises/simple-cipher/README.md | 131 ++++++++++++------ exercises/simple-cipher/simple_cipher_test.go | 30 ---- 5 files changed, 229 insertions(+), 69 deletions(-) create mode 100644 exercises/simple-cipher/.meta/description.md create mode 100644 exercises/simple-cipher/.meta/hints.md create mode 100644 exercises/simple-cipher/.meta/metadata.yml diff --git a/exercises/simple-cipher/.meta/description.md b/exercises/simple-cipher/.meta/description.md new file mode 100644 index 000000000..eb6d5d7a0 --- /dev/null +++ b/exercises/simple-cipher/.meta/description.md @@ -0,0 +1,98 @@ +Implement a simple shift cipher like Caesar and a more secure +substitution cipher. + +## Step 1 + +"If he had anything confidential to say, he wrote it in cipher, that +is, by so changing the order of the letters of the alphabet, that not +a word could be made out. If anyone wishes to decipher these, and get +at their meaning, he must substitute the fourth letter of the +alphabet, namely D, for A, and so with the others." —Suetonius, Life +of Julius Caesar + +Ciphers are very straight-forward algorithms that allow us to render +text less readable while still allowing easy deciphering. They are +vulnerable to many forms of cryptoanalysis, but we are lucky that +generally our little sisters are not cryptoanalysts. + +The Caesar Cipher was used for some messages from Julius Caesar that +were sent afield. Now Caesar knew that the cipher wasn't very good, +but he had one ally in that respect: almost nobody could read well. So +even being a couple letters off was sufficient so that people couldn't +recognize the few words that they did know. + +Your task is to create a simple shift cipher like the Caesar Cipher. +This image is a great example of the Caesar Cipher: + +![Caesar Cipher][1] + +For example: + +Giving "iamapandabear" as input to the encode function returns the +cipher "ldpdsdqgdehdu". Obscure enough to keep our message secret in +transit. + +When "ldpdsdqgdehdu" is put into the decode function it would return +the original "iamapandabear" letting your friend read your original +message. + +Initially you will implement a [Caesar Cipher][cc] with a fixed shift +distance of 3 (*namely D, for A*). + +## Step 2 + +Fixed distance Shift Ciphers are no fun though when your kid sister +figures it out. Try amending the code to allow us to specify a shift +distance. + +You will implement a more generic Shift Cipher with a flexible shift +distance. + +# Step 3 + +With only 26 true possible shift values, your kid sister will figure +this out too. Next lets define a more complex cipher using a string as +key value: a [Vigenère cipher][vc]. + +Here's an example: + +Given the key "aaaaaaaaaaaaaaaaaa", encoding the string +"iamapandabear" would return the original "iamapandabear". + +Given the key "ddddddddddddddddd", encoding our string "iamapandabear" +would return the obscured "ldpdsdqgdehdu" + +In the example above, we've set a = 0 for all the key values, with a +shift distance of 0. So when the plaintext is added to the key, we end +up with the same message coming out. So "aaaa" is not an ideal +key. But if we set the key to "dddd", we would get the same thing as +the Shift Cipher with all shift distances set to 4. + +These keys are not much of an improvement over the fixed distance Shift +Cipher. However, we can put many different lengths into the key if we +use strings with different characters: + +Given the key "adadadadad", encoding the string "iamapandabear" would +return the obscured "idmdpdngaeedr". + +Each character in the key is used to shift the corresponding character +by index. If the key is shorter than the text, repeat the key as +needed. + +## Extensions + +Shift ciphers work by making the text slightly odd, but are vulnerable +to frequency analysis. Substitution ciphers help that, but are still +very vulnerable when the key is short or if spaces are +preserved. You'll see one solution to this problem in the exercise +"[crypto-square](exercises/crypto-square/)". + +If you want to go farther in this field, the questions begin to be +about how we can exchange keys in a secure way. Take a look at +[Diffie-Hellman on Wikipedia][dh] for one of the first implementations +of this scheme. + +[1]: https://upload.wikimedia.org/wikipedia/commons/thumb/4/4a/Caesar_cipher_left_shift_of_3.svg/320px-Caesar_cipher_left_shift_of_3.svg.png +[cc]: https://en.wikipedia.org/wiki/Caesar_cipher +[vc]: https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher +[dh]: https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange diff --git a/exercises/simple-cipher/.meta/hints.md b/exercises/simple-cipher/.meta/hints.md new file mode 100644 index 000000000..66bc1d16c --- /dev/null +++ b/exercises/simple-cipher/.meta/hints.md @@ -0,0 +1,35 @@ +## Implementation + +The definition of the Cipher interface is located in +[cipher.go](./cipher.go). + +Your implementations should conform to the Cipher interface. + +```go +type Cipher interface { + Encode(string) string + Decode(string) string +} +``` + +It is expected that `Encode` will ignore all values in the string that +are not A-Za-z, they will not be represented in the output. The output +will be also normalized to lowercase. + +The functions used to obtain the ciphers are: + +```go +func NewCaesar() Cipher { } + +func NewShift(distance int) Cipher { } + +func NewVigenere(key string) Cipher { } +``` + +Argument for `NewShift` must be in the range 1 to 25 or -1 to -25. +Zero is disallowed. For invalid arguments `NewShift` returns nil. + +Argument for `NewVigenere` must consist of lower case letters a-z +only. Values consisting entirely of the letter 'a' are disallowed. +For invalid arguments `NewVigenere` returns nil. + diff --git a/exercises/simple-cipher/.meta/metadata.yml b/exercises/simple-cipher/.meta/metadata.yml new file mode 100644 index 000000000..7ca04f8f8 --- /dev/null +++ b/exercises/simple-cipher/.meta/metadata.yml @@ -0,0 +1,4 @@ +--- +blurb: "Implement a simple shift cipher like Caesar and a more secure substitution cipher" +source: "Substitution Cipher at Wikipedia" +source_url: "http://en.wikipedia.org/wiki/Substitution_cipher" diff --git a/exercises/simple-cipher/README.md b/exercises/simple-cipher/README.md index 5394c733d..fcd6d85db 100644 --- a/exercises/simple-cipher/README.md +++ b/exercises/simple-cipher/README.md @@ -1,15 +1,16 @@ # Simple Cipher -Implement a simple shift cipher like Caesar and a more secure substitution cipher. +Implement a simple shift cipher like Caesar and a more secure +substitution cipher. ## Step 1 -"If he had anything confidential to say, he wrote it in cipher, that is, -by so changing the order of the letters of the alphabet, that not a word -could be made out. If anyone wishes to decipher these, and get at their -meaning, he must substitute the fourth letter of the alphabet, namely D, -for A, and so with the others." -—Suetonius, Life of Julius Caesar +"If he had anything confidential to say, he wrote it in cipher, that +is, by so changing the order of the letters of the alphabet, that not +a word could be made out. If anyone wishes to decipher these, and get +at their meaning, he must substitute the fourth letter of the +alphabet, namely D, for A, and so with the others." —Suetonius, Life +of Julius Caesar Ciphers are very straight-forward algorithms that allow us to render text less readable while still allowing easy deciphering. They are @@ -17,9 +18,9 @@ vulnerable to many forms of cryptoanalysis, but we are lucky that generally our little sisters are not cryptoanalysts. The Caesar Cipher was used for some messages from Julius Caesar that -were sent afield. Now Caesar knew that the cipher wasn't very good, but -he had one ally in that respect: almost nobody could read well. So even -being a couple letters off was sufficient so that people couldn't +were sent afield. Now Caesar knew that the cipher wasn't very good, +but he had one ally in that respect: almost nobody could read well. So +even being a couple letters off was sufficient so that people couldn't recognize the few words that they did know. Your task is to create a simple shift cipher like the Caesar Cipher. @@ -29,59 +30,111 @@ This image is a great example of the Caesar Cipher: For example: -Giving "iamapandabear" as input to the encode function returns the cipher "ldpdsdqgdehdu". Obscure enough to keep our message secret in transit. +Giving "iamapandabear" as input to the encode function returns the +cipher "ldpdsdqgdehdu". Obscure enough to keep our message secret in +transit. When "ldpdsdqgdehdu" is put into the decode function it would return the original "iamapandabear" letting your friend read your original message. +Initially you will implement a [Caesar Cipher][cc] with a fixed shift +distance of 3 (*namely D, for A*). + ## Step 2 -Shift ciphers are no fun though when your kid sister figures it out. Try -amending the code to allow us to specify a key and use that for the -shift distance. This is called a substitution cipher. +Fixed distance Shift Ciphers are no fun though when your kid sister +figures it out. Try amending the code to allow us to specify a shift +distance. + +You will implement a more generic Shift Cipher with a flexible shift +distance. + +# Step 3 + +With only 26 true possible shift values, your kid sister will figure +this out too. Next lets define a more complex cipher using a string as +key value: a [Vigenère cipher][vc]. Here's an example: -Given the key "aaaaaaaaaaaaaaaaaa", encoding the string "iamapandabear" -would return the original "iamapandabear". +Given the key "aaaaaaaaaaaaaaaaaa", encoding the string +"iamapandabear" would return the original "iamapandabear". Given the key "ddddddddddddddddd", encoding our string "iamapandabear" -would return the obscured "lpdsdqgdehdu" - -In the example above, we've set a = 0 for the key value. So when the -plaintext is added to the key, we end up with the same message coming -out. So "aaaa" is not an ideal key. But if we set the key to "dddd", we -would get the same thing as the Caesar Cipher. +would return the obscured "ldpdsdqgdehdu" -## Step 3 +In the example above, we've set a = 0 for all the key values, with a +shift distance of 0. So when the plaintext is added to the key, we end +up with the same message coming out. So "aaaa" is not an ideal +key. But if we set the key to "dddd", we would get the same thing as +the Shift Cipher with all shift distances set to 4. -The weakest link in any cipher is the human being. Let's make your -substitution cipher a little more fault tolerant by providing a source -of randomness and ensuring that the key is not composed of numbers or -capital letters. +These keys are not much of an improvement over the fixed distance Shift +Cipher. However, we can put many different lengths into the key if we +use strings with different characters: -If someone doesn't submit a key at all, generate a truly random key of -at least 100 characters in length, accessible via Cipher#key (the # -syntax means instance variable) +Given the key "adadadadad", encoding the string "iamapandabear" would +return the obscured "idmdpdngaeedr". -If the key submitted has capital letters or numbers, throw an -ArgumentError with a message to that effect. +Each character in the key is used to shift the corresponding character +by index. If the key is shorter than the text, repeat the key as +needed. ## Extensions Shift ciphers work by making the text slightly odd, but are vulnerable to frequency analysis. Substitution ciphers help that, but are still -very vulnerable when the key is short or if spaces are preserved. Later -on you'll see one solution to this problem in the exercise -"crypto-square". +very vulnerable when the key is short or if spaces are +preserved. You'll see one solution to this problem in the exercise +"[crypto-square](exercises/crypto-square/)". -If you want to go farther in this field, the questions begin to be about -how we can exchange keys in a secure way. Take a look at [Diffie-Hellman -on Wikipedia][dh] for one of the first implementations of this scheme. +If you want to go farther in this field, the questions begin to be +about how we can exchange keys in a secure way. Take a look at +[Diffie-Hellman on Wikipedia][dh] for one of the first implementations +of this scheme. [1]: https://upload.wikimedia.org/wikipedia/commons/thumb/4/4a/Caesar_cipher_left_shift_of_3.svg/320px-Caesar_cipher_left_shift_of_3.svg.png -[dh]: http://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange +[cc]: https://en.wikipedia.org/wiki/Caesar_cipher +[vc]: https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher +[dh]: https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange + +## Implementation + +The definition of the Cipher interface is located in +[cipher.go](./cipher.go). + +Your implementations should conform to the Cipher interface. + +```go +type Cipher interface { + Encode(string) string + Decode(string) string +} +``` + +It is expected that `Encode` will ignore all values in the string that +are not A-Za-z, they will not be represented in the output. The output +will be also normalized to lowercase. + +The functions used to obtain the ciphers are: + +```go +func NewCaesar() Cipher { } + +func NewShift(distance int) Cipher { } + +func NewVigenere(key string) Cipher { } +``` + +Argument for `NewShift` must be in the range 1 to 25 or -1 to -25. +Zero is disallowed. For invalid arguments `NewShift` returns nil. + +Argument for `NewVigenere` must consist of lower case letters a-z +only. Values consisting entirely of the letter 'a' are disallowed. +For invalid arguments `NewVigenere` returns nil. + + ## Running the tests diff --git a/exercises/simple-cipher/simple_cipher_test.go b/exercises/simple-cipher/simple_cipher_test.go index 489db9dce..cba6b4df9 100644 --- a/exercises/simple-cipher/simple_cipher_test.go +++ b/exercises/simple-cipher/simple_cipher_test.go @@ -1,33 +1,3 @@ -// For Step 1, implement the Caesar cipher, which seems clear enough except -// maybe for figuring out whether to add or subtract. -// -// For Step 2, implement a shift cipher, like Caesar except the shift amount -// is specified as an int. (Each letter of "ddddddddddddddddd", is 'd'; -// 'd'-'a' == 3, so the corresponding shift amount is 3.) -// -// Steps 2 and 3 seem to be describing the Vigenère cipher (Google it) -// so let's do that too. The random thing, don't worry about. There is -// no test for that. -// -// API: -// -// type Cipher interface { -// Encode(string) string -// Decode(string) string -// } -// NewCaesar() Cipher -// NewShift(int) Cipher -// NewVigenere(string) Cipher -// -// Interface Cipher is in file cipher.go. -// -// Argument for NewShift must be in the range 1 to 25 or -1 to -25. -// Zero is disallowed. For invalid arguments NewShift returns nil. -// -// Argument for NewVigenere must consist of lower case letters a-z only. -// Values consisting entirely of the letter 'a' are disallowed. -// For invalid arguments NewVigenere returns nil. - package cipher import ( From 9a4d73e3bf75c09b8a19f07fdba6a45a6689e4f0 Mon Sep 17 00:00:00 2001 From: Tom Leen Date: Wed, 6 Sep 2017 11:23:36 -0400 Subject: [PATCH 5/5] Better link to crypto-square in README --- exercises/simple-cipher/.meta/description.md | 2 +- exercises/simple-cipher/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/exercises/simple-cipher/.meta/description.md b/exercises/simple-cipher/.meta/description.md index eb6d5d7a0..0e054f3ce 100644 --- a/exercises/simple-cipher/.meta/description.md +++ b/exercises/simple-cipher/.meta/description.md @@ -85,7 +85,7 @@ Shift ciphers work by making the text slightly odd, but are vulnerable to frequency analysis. Substitution ciphers help that, but are still very vulnerable when the key is short or if spaces are preserved. You'll see one solution to this problem in the exercise -"[crypto-square](exercises/crypto-square/)". +"[crypto-square](https://github.com/exercism/go/tree/master/exercises/crypto-square)". If you want to go farther in this field, the questions begin to be about how we can exchange keys in a secure way. Take a look at diff --git a/exercises/simple-cipher/README.md b/exercises/simple-cipher/README.md index fcd6d85db..b9214b7cc 100644 --- a/exercises/simple-cipher/README.md +++ b/exercises/simple-cipher/README.md @@ -87,7 +87,7 @@ Shift ciphers work by making the text slightly odd, but are vulnerable to frequency analysis. Substitution ciphers help that, but are still very vulnerable when the key is short or if spaces are preserved. You'll see one solution to this problem in the exercise -"[crypto-square](exercises/crypto-square/)". +"[crypto-square](https://github.com/exercism/go/tree/master/exercises/crypto-square)". If you want to go farther in this field, the questions begin to be about how we can exchange keys in a secure way. Take a look at