java实现维吉尼亚算法

import sun.net.www.content.text.plain;


public class Vigenere
{
//维吉尼亚加密算法的实现,主要问题是如何构建一个2唯的矩阵表
int num = 26*26;
public static int data[][]= new int[26][26];
public static String key = "BEST";
public static String plainText = "HELLO WORLD";
public static String cipherText = "";
public static void init(){
int temp=0;
for(int i =0;i<26;i++){
for(int j=0;j<26;j++){
data[i][j]=(i+j)%26;
System.out.print(data[i][j]);
}
System.out.println();
}
//System.out.println("-------"+data[13][12]);
}

public static String vigenere(String str){
//str为输入的明文
if(str!=null){
plainText = str.trim().toLowerCase();
}
System.out.println("plain length:"+plainText.length());
for(int i=0;i char []temp = new char[plainText.length()];
if(key.length()>=temp.length){
key = temp.toString().toLowerCase();
}
else{
// System.out.println("i="+i);

for(int j=0,t=0;t // System.out.println("j="+j);
if(j==key.length())
{
j=0;
}
temp[t]=key.charAt(j);
}

char []t = temp;
System.out.println();
for(i=0;i key = new String(t).toLowerCase();
}

System.out.println(key);
}
char[]k = key.toLowerCase().toCharArray();
char[]p = plainText.trim().toLowerCase().toCharArray();
char[]c = new char[p.length];
int p1=0;
int p2=0;
for(int i=0;i p1=k[i]-97;
p2=p[i]-97;
System.out.println("p1="+p1+",p2="+p2);
//查表,找出对应的p1,p2位置元素的值
c[i]=(char) (data[p1][p2]+97);
System.out.println(c[i]+"---");
}

cipherText=new String(c).toLowerCase();
System.out.println("明文为:"+plainText);
System.out.println("密钥为:"+key);
System.out.println("密文为:"+cipherText);
return cipherText;
}

public static void main(String args[]){
init();
vigenere("HELLOWORLD");
}

}

你可能感兴趣的:(数据结构算法)