使用vigenere表进行多字母加密(java)

问题描述 投票:0回答:2

我在这个程序中使用vigenere表加密了文本,但是在编译时它给出了这个例外:线程“main”中的异常java.lang.RuntimeException:无法编译的源代码 - 不是在advancednetworks.PolyalphabetCipher.main上的语句(PolyalphabetCipher.java: 25)Java结果:1

任何人都可以帮助我纠正它,尽快

import java.util.*;

public class PolyalphabetCipher {
public static void main(String... s)
{
    //createVigenereTable();
    Scanner sc=new Scanner(System.in);
    String key, text;

    System.out.println("Enter the keyword");
    key=sc.nextLine();
    key=key.toUpperCase();
    System.out.println("Enter text to be encrypted");
    text=sc.nextLine();
    text=text.toLowerCase();
    text=text.replaceAll("\\s+","");
    System.out.println(text);
    encrypt(key,text);      

}
public static int[][] createVigenereTable()
{
    int table[][]=new int[26][26];
    int rem=0, n=26;
    int value;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            value=j+rem;
            if(value>25)
            {
                value=value-26;
            }
            table[i][j]=value+65;
        }
    rem++;
    }

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)

            {
           char b=(char)(table[i][j]);
            System.out.print(b);
            System.out.print(" ");
            }
            System.out.println();
    }
    return table;

  }


//keyword::row and t=plainText::column
public static void encrypt(String k, String t)
{
    int len=k.length();
    char keyword[]=new char[t.length()];
    for(int i=0, j=0;i<t.length();i++)
    {
        keyword[i]=k.charAt(j);
        j++;
        if(j==len)
            j=0;

    }
    char cipherText[]=new char[t.length()];

    int vigenere[][]=new int[26][26];
    vigenere=createVigenereTable();
    System.out.println(vigenere);
    for(int x=0;x<t.length();x++)
    {
        int i=0;
        int j=0;
     for(int y=0;y<26;y++)
     {
         if(keyword[x]==vigenere[0][y])  
                 i=y;
         if(t.charAt(x)==vigenere[y][0])
                 j=y;
     }
      cipherText[x]=(char)(vigenere[j][i]);   
     }
    System.out.println(cipherText);
    }

}
java vigenere
2个回答
0
投票

如果您使用不基于ASCII表的清晰映射字母 - >整数,则该算法将更容易实现。

这可以通过创建两个翻译地图来完成,

toInteger= {a  0, b  1, ...}
toLetter=  {0 a, 1 b, ...}

然后简单地将消息和键转换为整数列表,并进行非常简单的转换并使用java modulo operator,最后将结果序列转换回字母。


0
投票

使用vigenere密码加密和解密字符的一种更简单的方法是使用它们的char值并计算加密/解密的char值:

public class VignereTable {

    private static final int ASCII_UPPERCASE_LETTERS_START = 65;
    private static final int ALPHABET_SIZE = 26;

    private VignereTable() {
         //prevent initialization
    }

    public static char cipherChar(char _key, char _letter) {
        return (char) ((_key + _letter) % ALPHABET_SIZE + ASCII_UPPERCASE_LETTERS_START);
    }

    public static char decipherChar(char _key, char _letter) {
        int index = _key - ASCII_UPPERCASE_LETTERS_START;
        if (_letter >= ASCII_UPPERCASE_LETTERS_START + index && _letter <= ASCII_UPPERCASE_LETTERS_START + (index * 2))
            return (char) (_letter - index);
        else if (_letter <= ASCII_UPPERCASE_LETTERS_START + index)
            return (char) (_letter + ALPHABET_SIZE - index);
        else
            return (char) (_letter - (_key - ASCII_UPPERCASE_LETTERS_START));
    }

    public static char[][] asArray() {
        char table[][] = new char[ALPHABET_SIZE][ALPHABET_SIZE];
        for (int i = 0; i < ALPHABET_SIZE; i++) {
            for (int j = 0; j < ALPHABET_SIZE; j++) {
                table[i][j] = (char) ((ASCII_UPPERCASE_LETTERS_START + ((j + i)) % ALPHABET_SIZE));
            }
        }
        return table;
    }

    public static String asString() {
        char[][] vTable = asArray();
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < vTable.length; i++) {
            for (int j = 0; j < vTable.length; j++) {
                builder.append(vTable[i][j]).append(" ");
            }
            builder.append("\n");
        }
        return builder.toString();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.