id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

orion.a.smith@gmail.com apcsa-encryption > apcsaDecryptSumModulusWithHint
prev  |  next  |  chance

Given a ciphertext String, a key length and a hint, reconstruct and return the original plaintext by reversing a sum then modulus algorithm. The problem with that algorithm, however, is that it will almost always lose data because of the remainder operation. This makes reconstructing the original plaintext difficult if not impossible unless some of it is already known as a hint - otherwise, many original plaintexts could have produced the ciphertext. The hint you are given will have some of the original plaintext letters, and the placeholders for the other letters will be occupied by dash ('-') characters. The encryption algorithm to reverse is described as follows.


1) Each letter is assigned a number: A=0, B=1, ... Z=25.  Anything that isn't a letter (hint: Character.isLetter()) is repeated in the ciphertext, in the same position it was in the plaintext.

2) Sum the values of the first n non-space characters together, then calculate the remainder after dividing that sum by 26.  (If there are non-letter characters, remember that the indexes to use for this sum may be more than 0, 1, ..., (n-1).

3) That remainder becomes the basis for the letter of the ciphertext, with the same number-to-letter mapping as step 1.

4) The second character of ciphertext is formed from a similar algorithm based on characters 2, 3, ..., (n+1) (indexes 1, 2, ..., (n), or ending after n if spaces are present) of the plaintext.

5) When you get to the last characters of the plaintext, "wrap" to the beginning characters.  For example: using an plaintext String with 10 characters that are all letters and n=5, the ciphertext at index 8 (where index 9 is the last character) is formed from indexes 8, 9, 0, 1, 2 of the plaintext.  Hint: remainder makes your life easier here, too!

Preconditions: the ciphertext will have at least n characters (all letters will be uppercase), and n>0.

HINT

If, in a run of n ciphertext characters, you already know the plaintext for n-1 of those characters, can you find the last plaintext character?

Be advised, this is a pretty tough problem.

apcsaDecryptSumModulusWithHint("NHYAVTYE", 2, "F-Z-B-ZZ") → "FIZZBUZZ"
apcsaDecryptSumModulusWithHint("NVQW ALP TMU KCKMVJM FN AE RVH RCAQHI", 2, "F--- --- --- ------- -- -- --- ------") → "FIND THE LIE QUICKLY OR WE ARE DOOMED"
apcsaDecryptSumModulusWithHint("ECZ VPOO QTXBDR FO KRYY GVV, UMP KRMC JE!", 3, "T-E R--L D-N--R I- F--M Y--, -OT F--M M-!") → "THE REAL DANGER IS FROM YOU, NOT FROM ME!"

...Save, Compile, Run (ctrl-enter)

public String apcsaDecryptSumModulusWithHint(String ciphertext, int n, String hint) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 350

Copyright Nick Parlante 2017 - privacy