Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions exercises/simple-cipher/.meta/description.md
Original file line number Diff line number Diff line change
@@ -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](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
[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
35 changes: 35 additions & 0 deletions exercises/simple-cipher/.meta/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## Implementation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like the "## Implementation" section for hints.md. It works very well to bridge from the description content.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Me too. if we find it useful we may want to put it in the template.


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.

4 changes: 4 additions & 0 deletions exercises/simple-cipher/.meta/metadata.yml
Original file line number Diff line number Diff line change
@@ -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"
131 changes: 92 additions & 39 deletions exercises/simple-cipher/README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
# 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
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.
Expand All @@ -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](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 [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

Expand Down
30 changes: 0 additions & 30 deletions exercises/simple-cipher/simple_cipher_test.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down