Note that there are some explanatory texts on larger screens.

plurals
  1. POVigenère cipher implementation
    primarykey
    data
    text
    <p>I have to implement a variant of the <a href="http://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher" rel="nofollow">Vigenère cipher</a>. I got the encryption part without issues, but I have a bug in the decryption code and I don't understand what I'm doing wrong.</p> <p>The requirements are:</p> <ul> <li><p>the key can only contain <code>A</code> - <code>Z</code> (uppercase)</p></li> <li><p>code values for the key characters are 0 for A, 1 for B, ..., and 25 for Z</p></li> <li><p>do not encode a character if the code is &lt; 32 (preserve control characters)</p></li> <li><p>encrypted character code = original character code + key character code</p></li> <li><p>the final encrypted character must be between 32 and 126, exclusively so if the final encrypted character > 126 it must be brought back into the 32 - 126 range by adding 32 to the value and then subtracting 126</p></li> </ul> <p>The encryption code:</p> <pre><code>// it works ok // I have tested it with some provided strings and the results are as expected public String encrypt(String plainText) { StringBuilder sb = new StringBuilder(); for (int i = 0; i &lt; plainText.length(); i++) { char c = plainText.charAt(i); if (c &gt;= 32) { int keyCharValue = theKey.charAt(i % theKey.length()) - 'A'; c += keyCharValue; if (c &gt; 126) { c = (char) (c + 32 - 126); } } sb.append(c); } return sb.toString(); } </code></pre> <p>The decryption code:</p> <pre><code>// there probably is an off-by-one error somewhere // everything is decrypted ok, except '~' which gets decrypted to ' ' (space) public String decrypt(String cipherText) { StringBuilder sb = new StringBuilder(); for (int i = 0; i &lt; cipherText.length(); i++) { char c = cipherText.charAt(i); if (c &gt;= 32) { int keyCharValue = theKey.charAt(i % theKey.length()) - 'A'; c -= keyCharValue; if (c &lt; 32) { c = (char) (c + 126 - 32); } } sb.append(c); } return sb.toString(); } </code></pre> <p>Example (with key <code>ABCDEFGHIJKLMNOPQRSTUVWXYZ</code>):</p> <ul> <li><p>original <code>~~~~~~~~~~~~~~~~~~~~~~~~~~</code></p></li> <li><p>encrypted <code>~!"#$%&amp;'()*+,-./0123456789</code></p></li> <li><p>decrypted <code>~</code> ('~' followed by spaces)</p></li> </ul> <p>EDIT:</p> <p>Here is the code I use for testing (it tests every character from 0 to 126 repeated as a string):</p> <pre><code>public static void main(String[] args) { int passed = 0; int failed = 0; String key = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; for (int c = 0; c &lt;= 126; c++) { StringBuilder sbString = new StringBuilder(); for (int i = 0; i &lt;= 25; i++) { sbString.append((char) c); } String original = sbString.toString(); Cipher cipher = Cipher(key); String encrypted = cipher.encrypt(original); String decrypted = cipher.decrypt(encrypted); if (!original.equals(decrypted)) { failed++; System.out.println("--FAILED--"); System.out.println(original); System.out.println(encrypted); System.out.println(decrypted); } else { passed++; } } int tests = passed + failed; System.out.println(tests + " tests"); System.out.println("passed: " + passed); System.out.println("failed: " + failed); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload