⽤java实现4种加密解密算法(包括:换位加密解密、替换加密解密、位加密解
密、⼀次⼀密)
案例1:换位加密解密算法
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class demo {
static char[]jiami(char[] str,int n)//加密算法
{
int i, j, k, d;
int len;
char[] temp, miwen, mtemp ={};
len = str.length;//字符串长度
if((d = len % n)!=0){
len = len + n - d;
}
if((temp =new char[len +1])== null)//申请内存
{
System.out.print("申请内存失败!\n");
}
if((miwen =new char[len +1])== null)//申请内存
{
System.out.print("申请内存失败!\n");
}else{
mtemp = miwen;
}
System.arraycopy(str,0, temp,0, str.length);
for(i = str.length; i < len; i++){
temp[i]=32;
}
temp[len]='\0';
i =0;
for(k =0; k < n; k++)//转换,加密
{
for(j =0; j < len / n; j++){
mtemp[i]= temp[k + j * n];
i++;
}
}
mtemp[i]='\0';
temp = null;
return miwen;//返回密⽂
}
static char[]jiemi(char[] str,int n)//解密算法
{
int i, j, k =0, d;
int len;
char[] temp, mingwen, mtemp ={};
len = str.length;
if((d = len % n)!=0){
if((d = len % n)!=0){
len = len - d;
}
n = len / n;
if((temp =new char[len +1])== null)//申请内存
{
System.out.print("申请内存失败!\n");
}
if((mingwen =new char[len +1])== null)//申请内存
{
System.out.print("申请内存失败!\n");
}else{
mtemp = mingwen;
}
System.arraycopy(str,0, temp,0, str.length);
for(i = str.length; i < len; i++){
temp[i]=32;
}
temp[len]='\0';
i =0;
for(k =0; k < n; k++)//恢复出明⽂
{
for(j =0; j < len / n; j++){
mtemp[i]= temp[k + j * n];
i++;
}
}
while(mtemp[--i]==32);
i++;
mtemp[i]='\0';
return mingwen;//返回明⽂
}
public static void main(String[] args)throws IOException {
int n, i;
char[] srcstr =new char[100];
char[] miwen;
String go;
System.out.print("换位加密解密算法演⽰!\n");
do{
System.out.print("\n请先输⼊换位加密矩阵每⾏的字符数:");
Scanner input =new Scanner(System.in);
n = Int();//输⼊换位加密矩阵每⾏的字符数
System.out.println("请输⼊明⽂:");
//输⼊明⽂字符串
BufferedReader bufferedReader =new BufferedReader(new InputStreamReader(System.in)); String str = adLine();
srcstr = CharArray();
System.out.print("输⼊的明⽂为:");
for(i =0; i < srcstr.length; i++){
System.out.print(srcstr[i]);
}
miwen =jiami(srcstr, n);//加密
System.out.print("\n加密后的密⽂为:");
for(i =0; i < srcstr.length; i++){
for(i =0; i < srcstr.length; i++){
System.out.print(miwen[i]);
}
miwen =jiemi(miwen, n);//解密
System.out.print("\n解密出的明⽂为:");
for(i =0; i < srcstr.length; i++){
System.out.print(miwen[i]);
}
System.out.print("\n继续执⾏(y/n)?");
go = ();
}while(go.equalsIgnoreCase("y"));
System.out.println("演⽰结束!");
}
}
案例2:替换加密解密算法
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class demo {
static char[]jiami(char[] str,int n)//加密算法
{
int i, len;
char[] miwen;
len = str.length;
if((miwen =new char[len +1])== null)//申请内存
{
System.out.print("申请内存失败!\n");
}
for(i =0; i < len; i++)//移位替换
{
miwen[i]=(char)(str[i]+ n);
}
miwen[len]='\0';
return miwen;
}
static char[]jiemi(char[] str,int n)//解密算法
{
int i, len;
char[] mingwen;
len = str.length;
if((mingwen =new char[len +1])== null)//申请内存
{
System.out.print("申请内存失败!\n");
}
for(i =0; i < len; i++)//移位替换
{
mingwen[i]=(char)(str[i]- n);
}
mingwen[len]='\0';
return mingwen;
}
public static void main(String[] args)throws IOException {
public static void main(String[] args)throws IOException {
int i, n;
char[] srcstr =new char[100];
char[] miwen;
String go;
System.out.print("替换加密解密算法演⽰!\n");
do{
System.out.print("\n请先输⼊替换加密解密算法中的密钥:");
Scanner input =new Scanner(System.in);
n = Int();//密钥
System.out.print("请输⼊明⽂字符串:");
BufferedReader bufferedReader =new BufferedReader(new InputStreamReader(System.in)); String str = adLine();
srcstr = CharArray();//明⽂字符串
System.out.print("输⼊的明⽂为:");
for(i =0; i < srcstr.length; i++){
System.out.print(srcstr[i]);
}
miwen =jiami(srcstr, n);//加密
System.out.print("\n加密后的密⽂为:");
for(i =0; i < srcstr.length; i++){
System.out.print(miwen[i]);
}
miwen =jiemi(miwen, n);//解密
System.out.print("\n解密出的明⽂为:");
for(i =0; i < srcstr.length; i++){
System.out.print(miwen[i]);
}
System.out.print("\n继续执⾏(y/n)?");
go = ();
}while(go.equalsIgnoreCase("y"));
System.out.println("演⽰结束!");
}
}
执⾏结果如下:
案例3:位加密解密算法
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class demo {
static char[]bitcode(char[] str,char n)//位加密解密算法
{
int i, len;
char[] wen;
len = str.length;
if((wen =new char[len +1])== null){
System.out.print("申请内存失败!\n");
}
for(i =0; i < len; i++){
wen[i]=(char)(str[i]^ n);//异或运算
}
wen[len]='\0';
return wen;
}
public static void main(String[] args)throws IOException {
java加密方式有哪些int i;
char ch;
char[] srcstr =new char[100];
char[] miwen;
String go;
System.out.print("位加密解密算法演⽰!\n");
do{
System.out.print("\n请先输⼊替换加密解密算法中的密钥:");
Scanner input =new Scanner(System.in);
ch = ().charAt(0);//密钥
System.out.print("请输⼊明⽂字符串:");
BufferedReader bufferedReader =new BufferedReader(new InputStreamReader(System.in)); String str = adLine();
srcstr = CharArray();//明⽂字符串
System.out.print("输⼊的明⽂为:");
for(i =0; i < srcstr.length; i++){
System.out.print(srcstr[i]);
}
miwen =bitcode(srcstr, ch);//加密
System.out.print("\n加密后的密⽂为:");
for(i =0; i < srcstr.length; i++){
System.out.print(miwen[i]);
}
miwen =bitcode(miwen, ch);//解密
System.out.print("\n解密出的明⽂为:");
for(i =0; i < srcstr.length; i++){
System.out.print(miwen[i]);
}
System.out.print("\n继续执⾏(y/n)?");
go = ();
}while(go.equalsIgnoreCase("y"));
System.out.println("演⽰结束!");
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论