# Vigenere Encryption/Decryption

Page 1 of 1

## 4 Replies - 27881 Views - Last Post: 13 September 2011 - 07:57 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=247043&amp;s=1c0647f0bb9db7907839aec37a6d8998&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 orange75

• New D.I.C Head

Reputation: 1
• Posts: 30
• Joined: 11-November 10

# Vigenere Encryption/Decryption

Posted 13 September 2011 - 02:47 PM

Hey guys, I'm back again. This time I'm having some trouble with a Vigenere Decryption class... Basically the algorithm in which it decrypts. I've been trying to write it down on paper and figure it out that way, but I've hit a road block. Here's what I have, just the method that decrypts it... Could you guys check it out for me? Thanks!

If you need my encrypt method, I can post it, too. A few things though before you look.

kA is the array of the keyword/code word to decrypt/encrypt.

```   public byte dC(byte B)/>
{
int a = (int) 'a';
if(((char) b == ' ') || ((char) b == '\n')){
return (byte) b;
}
b = (byte) Character.toLowerCase((char) B)/>;
int kC = (int) kA[curPos%keyArr.length];
int dis = (int) b - a;
int kD = (int) kC + a;
int nK = ((int) dis - kD + 26) % 26;
int nB = a + nK;
curPos++;
return (byte) nB;
}

```

Is This A Good Question/Topic? 0

## Replies To: Vigenere Encryption/Decryption

### #2 blackcompe

• D.I.C Lover

Reputation: 1159
• Posts: 2,547
• Joined: 05-May 05

## Re: Vigenere Encryption/Decryption

Posted 13 September 2011 - 07:06 PM

I'm going to show you an example of how I'd do this. I really can't understand the intent of your code. This way is faster. Here's my interface:

```public class VigenereCipher {

private char shiftTable[][] = new char[26][26];

static {
//populate table
}

public static String encrypt(String plainText, String key) {
return null;
}

public static String decrypt(String cipherText, String key) {
return null;
}

public static void main( String[] args ) {

}

}

```

I used the Vigenere Cipher Wikipedia entry to guide my logic. This class only supports an alphabet of lower case ascii characters.

```public static String decrypt(String cipherText, String key) {
StringBuilder plainText = new StringBuilder();
char[] cipherTextArr = cipherText.toCharArray();
char[] keyArr = key.toCharArray();
//decipher this cipher text letter
for(int i = 0; i < cipherTextArr.length; i++) {
//go to the row in the table corresponding to this key letter
char row[] = shiftTable[(byte)keyArr[i]-97];
//find the position of this ciphertext letter in this row
for(int j = 0; j < row.length; j++) {
//use the column's label as the plaintext
if(row[j] == cipherTextArr[i]) {
plainText.append( shiftTable[0][j] );
}
}
}
return plainText.toString();
}

```

Console output:

```Plaintext: attackatdawn
Keyword: lemon
Key: lemonlemonle
Encrypted: lxfopvefrnhr
Decrypted: attackatdawn

```

This post has been edited by blackcompe: 13 September 2011 - 07:08 PM

### #3 pbl

• There is nothing you can't do with a JTable

Reputation: 8381
• Posts: 31,956
• Joined: 06-March 08

## Re: Vigenere Encryption/Decryption

Posted 13 September 2011 - 07:13 PM

blackcompe seems right to me, this is overcomplicated for nothing especially that your variables names are not very descriptive

### #4 orange75

• New D.I.C Head

Reputation: 1
• Posts: 30
• Joined: 11-November 10

## Re: Vigenere Encryption/Decryption

Posted 13 September 2011 - 07:34 PM

I appreciate the help guys. The idea was to do it in a formula, I think. Also, I promise my variables aren't really that nondescript. I figured it out though! It only took a couple hours with extended breaks and a couple pieces of scrap paper working through the equation. Thanks again.

### #5 pbl

• There is nothing you can't do with a JTable

Reputation: 8381
• Posts: 31,956
• Joined: 06-March 08

## Re: Vigenere Encryption/Decryption

Posted 13 September 2011 - 07:57 PM

It is preaching for my parish, but jave a look here

http://www.dreaminco...igenere-square/