java - Using a modified Vigenere cipher algorithm, decryption does not result in original input -
i have made modified vigenere cipher algorithm, i'm running issues. if encrypt string
, decrypt result, don't original string
back. source code:
public class viganereencryptionbase64 { private char[] keys = new char[19]; int sizekey = 0; int cimin = 0; final string mveas = " !\"#$%&'()=+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\\]^_'abcdefghijklmnopqrstuvwxyz{|}~"; public viganereencryptionbase64() { string keys = "bangdollamc08itats"; this.keys = keys.tochararray(); } public string encrypt(string toencrypt) { char charstoencrypt[] = toencrypt.tochararray(); char encryptedchars[] = toencrypt.tochararray(); int = 0; while (i < encryptedchars.length) { int sube = mveas.indexof(charstoencrypt[i]) + mveas.indexof(keys[sizekey]) + cimin; sizekey++; if (sizekey == keys.length) sizekey = 0; encryptedchars[i] = mveas.charat(math.floormod(sube, 95)); cimin = mveas.indexof(encryptedchars[i]); += 1; } return string.valueof(encryptedchars); } public string decrypt(string todecrypt) { char charstodecrypt[] = todecrypt.tochararray(); char decryptedchars[] = todecrypt.tochararray(); int = 0; while (i < charstodecrypt.length) { int sube = mveas.indexof(charstodecrypt[i]) - mveas.indexof(keys[sizekey]) - cimin; sizekey++; if (sizekey == keys.length) sizekey = 0; decryptedchars[i] = mveas.charat(math.floormod(sube, 95)); cimin = mveas.indexof(charstodecrypt[i]); i++; } return string.valueof(decryptedchars); } }
a usage case illustrates issue is
public static void main(string[] args) { viganereencryptionbase64 viganere = new viganereencryptionbase64(); system.out.println("result encryption: " + viganere.encrypt("<!doctype html public \"-//w3c//dtd xhtml 1.0 transitional//en\" \"http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd\">")); viganere = new viganereencryptionbase64(); system.out.println("result description: " + viganere.decrypt("~at,4x~<cp|\"(>rdnds~1x_\\xtmn5t{irx-9dzv+opkhjlbt[x7mk/'j&|p&a0qamr_yh9|h#\\y91/kktqi,su3ik, p$@ih.c:ue'lj25x l#[3f8ql3u]of")); }
where original input "<!doctype html public \"-//w3c//dtd xhtml 1.0 transitional//en\" \"http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd\">"
and encrypted result "~at,4x~<cp|\"(>rdnds~1x_\\xtmn5t{irx-9dzv+opkhjlbt[x7mk/'j&|p&a0qamr_yh9|h#\\y91/kktqi,su3ik, p$@ih.c:ue'lj25x l#[3f8ql3u]of"
and decrypted result "<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">"
which different.
so encryption , decryption working perfectly, have run code , not seen difference between test inputs , outputs.
i think may confused the backslashes \
being used escape characters if use example doesn't require escape sequences:
public static void main(string[] args) { viganere = new viganereencryptionbase64(); system.out.println("result encryption: " + viganere.encrypt("hello!")); // encrypted "+r.b7(" viganere = new viganereencryptionbase64(); system.out.println("result description: " + viganere.decrypt("+r.b7(")); // decrypted "hello!" }
you can see works perfectly.
in example there \
before each "
, java doesn't think you've ended string
, you'll notice lets use "
half way through string
without ending it.
so in reality there still \
in string
, looks there isn't 1 because won't printed system.out.println
. true of escaped characters; when printed user, translated tabs or new lines e.t.c.
Comments
Post a Comment