网络安全常见的四种加密解密算法

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
ch = (char) ((b[i] - 'A' + k) % 26 + 'A');
}
if(b[i]>='0'&&b[i]<='9')
{
if(b[i]+k%10<='9')
{
ch=(char)(b[i]+k%10);//当提取的字符在9之前的n位时
}
else
{ch=(char)('0'+((b[i]-('9'-k)-1)%10));//孤立出来的n个字符
Scanner input = new Scanner(System.in);
System.out.println("输入明文,要求用小写字母,不要打空格");
String s = input.nextLine();//输入明文,要求用小写字母,不要打空格
System.out.println("输入秘钥,要求用小写字母,不要打空格");
}
System.out.println("密文为:"+MStr+"密匙为:" + k);
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
/**
*解密公式
*/
void D(int k) {
try {
char b[];
b = MStr.toString().toCharArray();
for (int i = 0; i < ch2.length; i++) {
in2[i] = ch2[i] - 97;//利用ascii变成0-25数字
}
int j = 0;
for (int i = 0; i < length; i++) {
if (j == length2)
j = 0;
in1[i] = (in1[i] + in2[j]) % 26;//加密算法,mod26
try {
char b[];
b = MStr.toString().toCharArray();
char ch =' ';
for (int i = 0; i < MStr.length(); i++) {
if (b[i] >= 'a' && b[i] <= 'z') {
ch = (char) ((b[i] - 'a' + 26 - k) % 26 + 'a');
}
移位:
package mima;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Mainer {
ch = (char) ((b[i] - 'A' + 26 - k) % 26 + 'A');
}
if(b[i]>='0'&&b[i]<='9'){
if(b[i]-k%10>='0')//当提取的字符在0之前的n个时
{ ch=(char)(b[i]-k%10);}
else{
ch=(char)('9'-((b[i]-(k-'0')-1)%10));//操作孤立出来的n个字符
if (b[i] >= 'a' && b[i] <= 'z') {
ch = (char) ((b[i] - 'a' + k) % 26 + 'a');
}
if(b[i] >= 'A' && b[i] <= 'Z'){
ch = (char) ((b[i] - 'A' + k) % 26 + 'A');
}
}
ch=b[i];}
CStr.append(ch);//拼接字符串
}
System.out.println("明文:"+CStr);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
public static void main(String[] args) {
int key = s.nextInt() % 26; // %26的意义是获取密钥的偏移值
yiwei ks = new yiwei();
if(r==1){ks.E(key);}//加密
else if(r==2)
{ks.D(key);}//解密
else System.out.print("请选择正确选项(1或2)");
for (int i = 0; i < ch1.length; i++) {
in1[i] = ch1[i] - 97;//利用ascii变成0-25数字
}
char[] ch2 = s2.toCharArray();
int length2 = ch2.length;//明文长度
int[] in2 = new int[length2];
StringBuffer MStr = new StringBuffer("");//加密字符串
StringBuffer CStr = new StringBuffer("");//解密字符串
public static void main(String[] args) {
System.out.print("请输入密钥:");
int key = s.nextInt() % 26; // %26的意义是获取密钥的偏移值
Mainer ks = new Mainer();
ks.E(key);//加密
ks百度文库D(key);//解密
}
/**
*加密公式
*/
void E(int k) {
try {
System.out.println("请输入一段明文:");
else{
ch=(char)('9'-((b[i]-(k-'0')-1)%10));//操作孤立出来的n个字符
}}
CStr.append(ch);//拼接字符串
}
System.out.println("明文:"+CStr);
} catch (Exception e) {
System.out.println(e.getMessage());
Scanner s = new Scanner(System.in);
int key = s.nextInt() % 26; // %26的意义是获取密钥的偏移值
Mainer ks = new Mainer();
ks.E(key);//加密
ks.D(key);//解密
}
/**
*加密公式
*/
void E(int k) {
j++;
}
for (int i = 0; i < length; i++) {
ch1[i] = (char) (in1[i] + 97);//将数字变成字母
}
return String.valueOf(ch1);//将字符串数字变成String类型的字符串,返回
}
public static String deciphering(String s1, String s2) {
package mima;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Mainer {
StringBuffer MStr = new StringBuffer("");//加密字符串
System.out.print("1.移位加密;2移位解密");
Scanner result = new Scanner(System.in);
int r=result.nextInt();
System.out.print("请输入密钥:");
Scanner s = new Scanner(System.in);
if(b[i]>='0'&&b[i]<='9')
{
if(b[i]+k%10<='9')
{
ch=(char)(b[i]+k%10);//当提取的字符在9之前的n位时
}
else
{ch=(char)('0'+((b[i]-('9'-k)-1)%10));//孤立出来的n个字符
}ch=b[i];}
MStr.append(ch);//拼接字符串
String s2 = input.nextLine();//输入秘钥,要求用小写字母,不要打空格
String s3 = encrypt(s, s2);//使用加密算法,得到密文
System.out.println(s3);
String s4=deciphering(s3,s2);//使用解密算法,实现解密
}
if(b[i] >= 'A' && b[i] <= 'Z'){
ch = (char) ((b[i] - 'A' + 26 - k) % 26 + 'A');
}
if(b[i]>='0'&&b[i]<='9'){
if(b[i]-k%10>='0')//当提取的字符在0之前的n个时
{ ch=(char)(b[i]-k%10);}
char[] ch1 = s1.toCharArray();
int length = ch1.length;//密文长度
int[] in1 = new int[length];
for (int i = 0; i < ch1.length; i++) {
}}
MStr.append(ch);//拼接字符串
}
System.out.println("密文为:"+MStr+"密匙为:" + k);
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
/**
*解密公式
*/
void D(int k) {
try {
System.out.println("请输入一段明文:");
char b[];
BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
String str2 = br2.readLine();
b = str2.toCharArray();
System.out.println(s4);
}
public static String encrypt(String s1, String s2) {
char[] ch1 = s1.toCharArray();
int length = ch1.length;//明文长度
int[] in1 = new int[length];
char ch =' ';
for (int i = 0; i < MStr.length(); i++) {
if (b[i] >= 'a' && b[i] <= 'z') {
ch = (char) ((b[i] - 'a' + 26 - k) % 26 + 'a');
}
if(b[i] >= 'A' && b[i] <= 'Z'){
StringBuffer CStr = new StringBuffer("");//解密字符串
public static void main(String[] args) {
System.out.print("请输入密钥:");
Scanner s = new Scanner(System.in);
char b[];
BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
String str2 = br2.readLine();
b = str2.toCharArray();
char ch = ' ';
for (int i = 0; i < str2.length(); i++) {
char ch = ' ';
for (int i = 0; i < str2.length(); i++) {
if (b[i] >= 'a' && b[i] <= 'z') {
ch = (char) ((b[i] - 'a' + k) % 26 + 'a');
}
if(b[i] >= 'A' && b[i] <= 'Z'){
}
}
}
vigenere
package mima;
import java.util.Scanner;
public class vigenere {
public static void main(String[] args) {
// TODO Auto-generated method stub
@SuppressWarnings("resource")
相关文档
最新文档