标签:new playfair 对称加密 加密算法 playfair密码
这次,使用java来模拟现实playfair密码加密解密过程
import java.util.HashSet;
import java.util.Scanner;
public class Test1 {
public static char a[][] = new char[5][5];
public static String Encryptkey = "";
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("Please input the key");
String key = input.nextLine();
System.out.println("Please input the clear key");
String clearkey = input.nextLine();
System.out.println("Please input the signture key");
String signture = input.nextLine();
System.out.println("Output the key matrix");
Keymatrix(key);
OutputMatrix();
System.out.println("Encrypt");
Encrypt(clearkey, signture.toUpperCase());
Dncrypt(Encryptkey);
}
private static void Dncrypt(String encryptkey2) {
// TODO Auto-generated method stub
String Dncryptkey = "";
int m=0, n=0, o=0, p=0;
for(int i=0; i<encryptkey2.length(); i+=2) {
for(int j=0; j<5; j++) {
for(int k=0; k<5; k++) {
if(a[j][k] == encryptkey2.charAt(i)) {
m=j;
n=k;
}
if(a[j][k] == encryptkey2.charAt(i+1)) {
o=j;
p=k;
}
}
}
if(m==o) {
Dncryptkey =Dncryptkey + a[m][(n+4)%5] + a[o][(p+4)%5];
}
else if(n==p) {
Dncryptkey =Dncryptkey + a[(m+4)%5][n] + a[(o+4)%5][p];
}
else {
Dncryptkey =Dncryptkey + a[o][n] + a[m][p];
}
}
System.out.println(Dncryptkey);
System.out.println();
}
private static void Encrypt(String clearkey, String signture) {
// TODO Auto-generated method stub
String Upperclearkey = clearkey.toUpperCase();
String formatkey = "";
for(int i=0; i<Upperclearkey.length(); i++) {
if(Upperclearkey.charAt(i)>=‘A‘ && Upperclearkey.charAt(i) <‘Z‘) {
formatkey+=Upperclearkey.charAt(i);
}
}
while(isOverFlow(formatkey)) {
formatkey = addSignture(formatkey, signture);
}
if(formatkey.length()%2 !=0) {
formatkey += signture;
}
getEncrypt(formatkey);
}
private static void getEncrypt(String formatkey) {
// TODO Auto-generated method stub
int m=0, n=0, o=0, p=0;
for(int i=0; i<formatkey.length(); i+=2) {
for(int j=0; j<5; j++) {
for(int k=0; k<5; k++) {
if(a[j][k] == formatkey.charAt(i)) {
m=j;
n=k;
}
if(a[j][k] == formatkey.charAt(i+1)) {
o=j;
p=k;
}
}
}
if(m==o) {
Encryptkey =Encryptkey + a[m][(n+1)%5] + a[o][(p+1)%5];
}
else if(n==p) {
Encryptkey =Encryptkey + a[(m+1)%5][n] + a[(o+1)%5][p];
}
else {
Encryptkey =Encryptkey + a[o][n] + a[m][p];
}
}
System.out.println(Encryptkey);
System.out.println();
}
private static boolean isOverFlow(String formatkey) {
// TODO Auto-generated method stub
for(int i=0; i<formatkey.length() - formatkey.length()%2; i+=2) {
if(formatkey.charAt(i) == formatkey.charAt(i+1)) {
return true;
}
}
return false;
}
private static String addSignture(String formatkey, String signture) {
// TODO Auto-generated method stub
String temp = "";
for(int i=0; i<formatkey.length() - formatkey.length()%2; i+=2) {
if(formatkey.charAt(i) == formatkey.charAt(i+1)) {
temp = temp + formatkey.charAt(i) + signture.charAt(0) + formatkey.charAt(i+1);
}
else {
temp = temp + formatkey.charAt(i) + formatkey.charAt(i+1);
}
}
return temp;
}
private static void OutputMatrix() {
for(int i=0; i<5; i++) {
for(int j=0; j<5; j++) {
System.out.print(a[i][j]+" ");
}
System.out.println();
}
System.out.println();
}
private static void Keymatrix(String key) {
// TODO Auto-generated method stub
HashSet<Character> hs = new HashSet<Character>();
String formatkey ="";
String Upperkey = key.toUpperCase();
for(int i=0; i<Upperkey.length(); i++) {
if(Upperkey.charAt(i) >=‘A‘ && Upperkey.charAt(i) <‘Z‘) {
if(!hs.contains(Upperkey.charAt(i))) {
hs.add(Upperkey.charAt(i));
formatkey+=Upperkey.charAt(i);
}
}
}
for(char i=‘A‘; i<‘Z‘; i++) {
if(!hs.contains(i)) {
formatkey+=i;
}
}
int m = 0;
for(int i=0; i<5; i++)
for(int j=0; j<5; j++) {
a[i][j] = formatkey.charAt(m);
m ++;
}
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:new playfair 对称加密 加密算法 playfair密码
原文地址:http://blog.csdn.net/qiyu93422/article/details/46884825