Flask接口签名sign原理与实例代码浅析
323
2023-01-11
java实现仿射密码加密解密
本文实例为大家分享了java实现仿射密码加密解密的具体代码,供大家参考,具体内容如下
加密:将明文转化为对应的数字,如 ‘a'-> 0, ‘b'->1,…,'1'->26,'2'->27,…然后将数字进行仿射运算,求取出来的数字再转化为字符。即 密文=(K1*明文+K2)mod36
解密:密文转化为对应数字,然后进行仿射的逆运算,得到对应数字,然后将其转化为字符明文。解密 K3是K1的乘法逆元
import java.util.Scanner;
public class Affine{
public static void main(String[] args) {
char[] form = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0'
, '1', '2', '3', '4', '5', '6', '7', '8', '9'};
Scanner sc = new Scanner(System.in);
System.out.println("请输入http://待加密的明文:");
String MingWen = sc.nextLine();
MingWen=MingWen.toUpperCase();//将输入的明文全部大写
final int K1 = 103;
final int K2 = 103;
final int K3 = 31;
int [] cipherNum=new int[MingWen.length()];//用来存储数字化的密文
encryption(MingWen,form,K1,K2,cipherNum);
decryption(form,MingWen,K2,K3,cipherNum);
}
public static void encryption(String MingWen,char[] form,int K1,int K2,int[] cipherNum){
//第一步:将明文存入数组
char[] pla=new char[MingWen.length()];
for (int i = 0; i pla[i]=MingWen.charAt(i); }//已将明文存入字符数组pla中 //第二步:通过脚标找到明文中每个字符对应的数,参与加密运算 int[] MingWenNumber=new int[pla.length]; for (int i = 0; i for (int j = 0; j if (form[j]==pla[i]) { MingWenNumber[i] = j; } } } //通过脚标将明文全部转化为数字 char[] cipher = new char[MingWen.length()];//cipher密码数组 用来存储密文 for (int i = 0; i int a= Math.floorMod((K1*MingWenNumber[i]+K2),36); cipherNum[i]=a; cipher[i]=form[a]; //计算密文并存入数组中 } System.out.println("加密结果是:"); System.out.println(cipher); } public static void decryption(char[] form,String Mingwen,int K2,int K3,int[] cipherNum){ char[] JieMI=new char[Mingwen.length()]; for (int i =0;i JieMI[i]=form[Math.floorMod(K3*(cipherNum[i]-K2),36)]; } System.out.println("解密结果是:"); System.out.println(JieMI); } }
pla[i]=MingWen.charAt(i);
}//已将明文存入字符数组pla中
//第二步:通过脚标找到明文中每个字符对应的数,参与加密运算
int[] MingWenNumber=new int[pla.length];
for (int i = 0; i for (int j = 0; j if (form[j]==pla[i]) { MingWenNumber[i] = j; } } } //通过脚标将明文全部转化为数字 char[] cipher = new char[MingWen.length()];//cipher密码数组 用来存储密文 for (int i = 0; i int a= Math.floorMod((K1*MingWenNumber[i]+K2),36); cipherNum[i]=a; cipher[i]=form[a]; //计算密文并存入数组中 } System.out.println("加密结果是:"); System.out.println(cipher); } public static void decryption(char[] form,String Mingwen,int K2,int K3,int[] cipherNum){ char[] JieMI=new char[Mingwen.length()]; for (int i =0;i JieMI[i]=form[Math.floorMod(K3*(cipherNum[i]-K2),36)]; } System.out.println("解密结果是:"); System.out.println(JieMI); } }
for (int j = 0; j
if (form[j]==pla[i]) {
MingWenNumber[i] = j;
}
}
}
//通过脚标将明文全部转化为数字
char[] cipher = new char[MingWen.length()];//cipher密码数组 用来存储密文
for (int i = 0; i int a= Math.floorMod((K1*MingWenNumber[i]+K2),36); cipherNum[i]=a; cipher[i]=form[a]; //计算密文并存入数组中 } System.out.println("加密结果是:"); System.out.println(cipher); } public static void decryption(char[] form,String Mingwen,int K2,int K3,int[] cipherNum){ char[] JieMI=new char[Mingwen.length()]; for (int i =0;i JieMI[i]=form[Math.floorMod(K3*(cipherNum[i]-K2),36)]; } System.out.println("解密结果是:"); System.out.println(JieMI); } }
if (form[j]==pla[i]) {
MingWenNumber[i] = j;
}
}
}
//通过脚标将明文全部转化为数字
char[] cipher = new char[MingWen.length()];//cipher密码数组 用来存储密文
for (int i = 0; i int a= Math.floorMod((K1*MingWenNumber[i]+K2),36); cipherNum[i]=a; cipher[i]=form[a]; //计算密文并存入数组中 } System.out.println("加密结果是:"); System.out.println(cipher); } public static void decryption(char[] form,String Mingwen,int K2,int K3,int[] cipherNum){ char[] JieMI=new char[Mingwen.length()]; for (int i =0;i JieMI[i]=form[Math.floorMod(K3*(cipherNum[i]-K2),36)]; } System.out.println("解密结果是:"); System.out.println(JieMI); } }
int a= Math.floorMod((K1*MingWenNumber[i]+K2),36);
cipherNum[i]=a;
cipher[i]=form[a];
//计算密文并存入数组中
}
System.out.println("加密结果是:");
System.out.println(cipher);
}
public static void decryption(char[] form,String Mingwen,int K2,int K3,int[] cipherNum){
char[] JieMI=new char[Mingwen.length()];
for (int i =0;i JieMI[i]=form[Math.floorMod(K3*(cipherNum[i]-K2),36)]; } System.out.println("解密结果是:"); System.out.println(JieMI); } }
JieMI[i]=form[Math.floorMod(K3*(cipherNum[i]-K2),36)];
}
System.out.println("解密结果是:");
System.out.println(JieMI);
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~