#decryption in affine cypher

18 messages · Page 1 of 1 (latest)

pastel drift
#

https://exercism.org/tracks/go/exercises/affine-cipher
Hi, I'd really appreciate any help, no idea what's wrong with the calculation in decValue. This is the Decrypt method. Based on the Decryption formula in the link It tries to compute the plaintext based on multiple modular inverse of a, corresponding value of the ciphertext letter, b and m (26)

for _, char := range text {
        // D(y) = (a^-1)(y - b) mod m
        encryptedLetterValue := letterMap[char] // the number that maps to the encrypted letter 
        fmt.Printf("The value of the encrypted letter %c is %d\n", char, encryptedLetterValue)

        // a^-1 = FindMMIOfA(a)
        // TOFIX: what is wrong with this formula? it returns negative values which then maps to garbled output. See the test outputs. I'm sure it's not an order of operations problem.
        decValue := (FindMMIOfA(a) * (encryptedLetterValue - b)) % 26 
        
// we then retrieve the corresponding letter from the codeMap based on its value
        plainText.WriteString(string(codeMap[decValue]))
        fmt.Printf("plaintext is %s\n", plainText.String())
Exercism

Can you solve Affine Cipher in Go? Improve your Go skills with support from our world-class team of mentors.

gaunt jackal
#

Could you share your entire code/file?

#

It's hard to run code snippets

pastel drift
#

looking at how the mod 26 is done (I'm not good at math) for decryption in https://en.wikipedia.org/wiki/Affine_cipher it looks like if the result is negative, 26 is added repeatedly until a positive value is hit. Though I'm not sure if that's how the modulo's supposed to be calculated

The affine cipher is a type of monoalphabetic substitution cipher, where each letter in an alphabet is mapped to its numeric equivalent, encrypted using a simple mathematical function, and converted back to a letter. The formula used means that each letter encrypts to one other letter, and back again, meaning the cipher is essentially a standard...

gaunt jackal
#

Yeah, your mod results should all be positive

pastel drift
#

I've tried that (adding 26 repeatedly until I get a positive value) but it still doesn't decrypt

gaunt jackal
#

You got a hard coded 15 in your code that looks ... suspicious

#

Where did that come from?

pastel drift
#

my bad, it's supposed to be "a"

#

Thank you, part of it works now!!

#

for exercism: exe\0c\0s etc

gaunt jackal
#

🎉

#

Did you ensure your mod-26 results in a positive value?

pastel drift
#

Yes it works now !!

gaunt jackal
#

Got all the tests to pass?

pastel drift
#

Yes !!

gaunt jackal
#

Congrats 🙂