java - Trying to encrypt and decrypt a vigenere cipher -


i have text file encrypted using shifting, need encrypt encrypted text again time using vigenere cipher. need decrypt encrypted text (vigenere first shifting) upper , lower case letters need stay same black spaces, quotes, commas, , full stops. completed shift encryption , decryption left vigenere. below shown class encrypting vigenere, have not written decryption yet because stuck in encryption step. thank you.

public static string vigenereencryption(string str,string keyword) { char [] c_arr = null; int g =0; int keylength = keyword.length(); string encrypted = ""; string update =""; string []list = null; for(int k = 0; k<keyword.length();k++){ char key = keyword.charat(k);  c_arr = keyword.tochararray(); update = update + key; } for(int i=0;i<str.length();i++) {        //stores ascii value of character in string @ index 'i'     int c=str.charat(i);     //encryption logic uppercase letters      if(character.isuppercase(c))     {         for(int k = 0; k<keyword.length();k++){             g = c_arr[k] ;          }         c=(c+g)%26;         //if c value exceeds ascii value of 'z' reduce subtracting 26(no.of alphabets) keep in boundaries of ascii values of 'a' , 'z'         if(c>'z')             c=c-26;     }     //encryption logic lowercase letters     else if(character.islowercase(c))     {         c=(c+g)%26;         //if c value exceeds ascii value of 'z' reduce subtracting 26(no.of alphabets) keep in boundaries of ascii values of 'a' , 'z'         if(c>'z')             c=c-26;     }        //concatinate encrypted characters/strings     encrypted=encrypted+(char) c; } return encrypted;}}//end of public class` 

it looks looping on keyword inside loop text. not necessary.

you can find implementations of vigenere cipher at rosettacode. modify following code java per needs (like checking upper/lower case , processing them accordingly):

    static string encrypt(string text, final string key) {         string res = "";         text = text.touppercase();         (int = 0, j = 0; < text.length(); i++) {             char c = text.charat(i);             if (c < 'a' || c > 'z') continue;             res += (char)((c + key.charat(j) - 2 * 'a') % 26 + 'a');             j = ++j % key.length();         }         return res;     }      static string decrypt(string text, final string key) {         string res = "";         text = text.touppercase();         (int = 0, j = 0; < text.length(); i++) {             char c = text.charat(i);             if (c < 'a' || c > 'z') continue;             res += (char)((c - key.charat(j) + 26) % 26 + 'a');             j = ++j % key.length();         }         return res;     } 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -