资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java恺撒密码代码 java实现凯撒密码

可以代替所有密码的代码是什么

Caesar (恺撒)密码,又叫循环移位密码。它的加密过程可表示为: E(m) = (m+k ) mod n其中,m为明文字母在字母表中的位置数;n为字母表中的字母个数;k为密钥;E(m)为密文字母在字母表中对应的位置数。解密算法是:m = D(L) =(L-k)mod 26;2、算法设计:使。

网站制作、成都网站设计,成都做网站公司-创新互联已向1000多家企业提供了,网站设计,网站制作,网络营销等服务!设计与技术结合,多年网站推广经验,合理的价格为您打造企业品质网站。

用java语言编写

public class Test {

public static void main(String[] args){

System.out.println("请输入5个字母");

Scanner input =new Scanner(System.in);

String zm = input.next();//获取输入的5个字母也就是字符串

char[] arrays= new char[5];

for(int i=0;izm.length();i++){//用for循环将字符串分解为5个字符

arrays[i]=zm.charAt(i);

}

System.out.println("请输入一个整数t");

int a = input.nextInt();//获取输入的整数

for(int i=0;iarrays.length;i++){

System.out.print((char)(arrays[i]+a));//输出结果并转化为char类型

}

}

}

凯撒密码实现英文短句的加解密

1. 将“We are students.”这个英文词句用k=4的凯萨密码翻译成密码

1. 恺撒密码,

作为一种最为古老的对称加密体制,他的基本思想是:

通过把字母移动一定的位数来实现加密和解密。

例如,如果密匙是把明文字母的位数向后移动三位,那么明文字母B就变成了密文的E,依次类推,X将变成A,Y变成B,Z变成C,由此可见,位数就是凯撒密码加密和解密的密钥。

如:ZHDUHVWXGHQWV(后移三位)

2. 凯撒密码,

是计算机C语言编程实现加密和解密。挺复杂的。你可以研究一下哦。

2. 将凯撒密码(K=7)的加密、解密过程用C语言编程实现

/*

声明:MSVC++6.0环境测试通过

*/

#includestdio.h

#includectype.h

#define maxlen 100

#define K 7

char *KaisaEncode(char *str)//加密

{

char *d0;

d0=str;

for(;*str!='\0';str++)

{

if(isupper(*str))

*str=(*str-'A'+K)%26+'A';

else if(islower(*str))

*str=(*str-'a'+K)%26+'a';

else

continue;

}

return d0;

}

char *KaisaDecode(char *str)//解密

{

char *d0;

d0=str;

for(;*str!='\0';str++)

{

if(isupper(*str))

*str=(*str-'A'-K+26)%26+'A';

else if(islower(*str))

*str=(*str-'a'-K+26)%26+'a';

else

continue;

}

return d0;

}

int main(void)

{

char s[maxlen];

gets(s);

puts(KaisaEncode(s));

puts(KaisaDecode(s));

return 0;

}

3. 将凯撒密码X的加密、解密过程用C语言编程实现

(2)kaiser加密算法 具体程序:#include #include char encrypt(char ch,int n)/*加密函数,把字符向右循环移位n*/ { while(ch='A'ch='a'ch='z') { return ('a'+(ch-'a'+n)%26); } return ch; } void menu()/*菜单,1.加密,2.解密,3.暴力破解,密码只能是数字*/ { clrscr(); printf("\n========================================================="); printf("\n1.Encrypt the file"); printf("\n2.Decrypt the file"); printf("\n3.Force decrypt file"); printf("\n4.Quit\n"); printf("=========================================================\n"); printf("Please select a item:"); return; } main() { int i,n; char ch0,ch1; FILE *in,*out; char infile[20],outfile[20]; textbackground(BLACK); textcolor(LIGHTGREEN); clrscr(); sleep(3);/*等待3秒*/ menu(); ch0=getch(); while(ch0!='4') { if(ch0=='1') { clrscr(); printf("\nPlease input the infile:"); scanf("%s",infile);/*输入需要加密的文件名*/ if((in=fopen(infile,"r"))==NULL) { printf("Can not open the infile!\n"); printf("Press any key to exit!\n"); getch(); exit(0); } printf("Please input the key:"); scanf("%d",n);/*输入加密密码*/ printf("Please input the outfile:"); scanf("%s",outfile);/*输入加密后文件的文件名*/ if((out=fopen(outfile,"w"))==NULL) { printf("Can not open the outfile!\n"); printf("Press any key to exit!\n"); fclose(in); getch(); exit(0); } while(!feof(in))/*加密*/ { fputc(encrypt(fgetc(in),n),out); } printf("\nEncrypt is over!\n"); fclose(in); fclose(out); sleep(1); } if(ch0=='2') { clrscr(); printf("\nPlease input the infile:"); scanf("%s",infile);/*输入需要解密的文件名*/ if((in=fopen(infile,"r"))==NULL) { printf("Can not open the infile!\n"); printf("Press any key to exit!\n"); getch(); exit(0); } printf("Please input the key:"); scanf("%d",n);/*输入解密密码(可以为加密时候的密码)*/ n=26-n; printf("Please input the outfile:"); scanf("%s",outfile);/*输入解密后文件的文件名*/ if((out=fopen(outfile,"w"))==NULL) { printf("Can not open the outfile!\n"); printf("Press any key to exit!\n"); fclose(in); getch(); exit(0); } while(!feof(in)) { fputc(encrypt(fgetc(in),n),out); } printf("\nDecrypt is over!\n"); fclose(in); fclose(out); sleep(1); } if(ch0=='3') { clrscr(); printf("\nPlease input the infile:"); scanf("%s",infile);/*输入需要解密的文件名*/ if((in=fopen(infile,"r"))==NULL) { printf("Can not open the infile!\n"); printf("Press any key to exit!\n"); getch(); exit(0); } printf("Please input the outfile:"); scanf("%s",outfile);/*输入解密后文件的文件名*/ if((out=fopen(outfile,"w"))==NULL) { printf("Can not open the outfile!\n"); printf("Press any key to exit!\n"); fclose(in); getch(); exit(0); } for(i=1;i=25;i++)/*暴力破解过程,在察看信息正确后,可以按'Q'或者'q'退出*/ { rewind(in); rewind(out); clrscr(); printf("==========================================================\n"); printf("The outfile is:\n"); printf("==========================================================\n"); while(!feof(in)) { ch1=encrypt(fgetc(in),26-i); putch(ch1); fputc(ch1,out); } printf("\n========================================================\n"); printf("The current key is: %d \n",i);/*显示当前破解所用密码*/ printf("Press 'Q' to quit and other key to continue。

\n"); printf("==========================================================\n"); ch1=getch(); if(ch1=='q'||ch1=='Q')/*按'Q'或者'q'时退出*/ { clrscr(); printf("\nGood Bye!\n"); fclose(in); fclose(out); sleep(3); exit(0); } } printf("\nForce decrypt is over!\n"); fclose(in); fclose(out); sleep(1); } menu(); ch0=getch(); } clrscr(); printf("\nGood Bye!\n"); sleep(3); }。

4. 怎样编写程序:实现恺撒密码加密单词"julus"

用下面程序:新建个txt,放进去任意单词,设置#define N 5中的值,实现字母移位,达到加密目的。

本程序提供解密功能/************************************************************************//* 版权所有:信息工程学院 王明 使用时请注明出处!! *//* 算法:凯撒密码体制 e799bee5baa6e4b893e5b19e31333264643062 *//************************************************************************/#include #define N 5void jiami(char namea[256]) { FILE *fp_jiami,*fp_file2; char c; fp_jiami=fopen(namea,"rb"); fp_file2=fopen("file2.txt","wb"); while(EOF!=(fscanf(fp_jiami,"%c",c))) { if((c='A'c='a'c='A'c='a'c='a'c='A'c='a'c='A'c='a'c='A'c='Z')c=c+32; } fprintf(fp_file3,"%c",c); } fclose(fp_file3); fclose(fp_jiemi); }int main(){ char name[256]; int n; printf("输入你要操作的TXT文本:"); gets(name); printf("\n请选择需要进行的操作:\n"); printf(" 1:加密 2:解密 \n"); printf("输入你的选择:"); scanf("%d",n); switch(n) { case 1:{jiami(name);printf("\t加密成功!!\n\n"); break;} case 2:{jiemi(name);printf("\t解密成功!!\n\n"); break;} default:{printf("输入操作不存在!");} } return 0;}。

5. 谁有PYTHON编写的凯撒密码的加密和解密代码

给你写了一个.

def convert(c, key, start = 'a', n = 26):

a = ord(start)

offset = ((ord(c) - a + key)%n)

return chr(a + offset)

def caesarEncode(s, key):

o = ""

for c in s:

if c.islower():

o+= convert(c, key, 'a')

elif c.isupper():

o+= convert(c, key, 'A')

else:

o+= c

return o

def caesarDecode(s, key):

return caesarEncode(s, -key)

if __name__ == '__main__':

key = 3

s = 'Hello world!'

e = caesarEncode(s, key)

d = caesarDecode(e, key)

print e

print d

运行结果:

Khoor zruog!

Hello world!

4 16 15 8 19 2 21 22 13 2 21 10 16 15-1 凯撒密码是什么?

在密码学中,恺撒密码(英语:Caesar cipher),或称恺撒加密、恺撒变换、变换加密,是一种最简单且最广为人知的加密技术。它是一种替换加密的技术,明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文。例如,当偏移量是3的时候,所有的字母A将被替换成D,B变成E,以此类推。这个加密方法是以罗马共和时期恺撒的名字命名的,当年恺撒曾用此方法与其将军们进行联系。

(以上摘自百度百科,更多详情请自行学习了解)

然后这些数字,分别指代英文26个字母,比如4指代d,16指代p等等。以此类推,则除了“-1”以外的其他数字转换成字母依次是:dpohsbuvmbujpo

-1指的是偏移量为1,即明文中的所有字母分别向右偏移一位继而得到上述密文。因此若想得到明文,须将dpoh...的所有字母分别向左偏移一位,即d变成c,p变成o等等。以此类推,明文即是:

congratulation

祝贺

至于那个“-1”,个人猜想还有一种理解,就是指4 16……那些数字分别减去1。这样理解也能得出同一个答案,只是我不确定那个“-”究竟是减号还是普通的短破折号。

恺撒密码

“恺撒密码”据传是古罗马恺撒大帝用来保护重要军情的加密系统。(既是今天我们所说的:替代密码)

它是一种置换密码,通过将字母按顺序推后起3位起到加密作用,如将字母A换作字母D,将字母B换作字母E。据说恺撒是率先使用加密函的古代将领之一,因此这种加密方法被称为恺撒密码。

假如有这样一条指令:

明文(小写):ji xiao jing

用恺撒密码加密后就成为:

密文(大写):ML ALDR MLQJ

如果这份指令被敌方截获,也将不会泄密,因为字面上看不出任何意义。

这种加密方法还可以依据移位的不同产生新的变化,如将每个字母左19位,就产生这样一个明密对照表:

明文:a b c d e f g h i j k l m n o pq r s t u v w x y z

密文:T U V W X Y Z A B C D E F G H I J K L M N O P Q R S

在这个加密表下,明文与密文的对照关系就变成:

明文:b a i d u

密文:UTB WN

很明显,这种密码的密度是很低的,只需简单地统计字频就可以破译。于是人们在单一恺撒密码的基础上扩展出多表密码,称为“维吉尼亚”密码。它是由16世纪法国亨利三世王朝的布莱瑟·维吉尼亚发明的,其特点是将26个恺撒密表合成一个,见下表:

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

A 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

B 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 A

CC D E F G H I J K L M N O P Q R S T U V W X Y Z A B

D D E F G H I J K L M N O P Q R S T U V W X Y Z A B C

E E F G H I J K L M N O P Q R S T U V W X Y Z A B C D

F F G H I J K L M N O P Q R S T U V W X Y Z A B C D E

G G H I J K L M N O P Q R S T U V W X Y Z A B C D E F

H H I J K L M N O P Q R S T U V W X Y Z A B C D E F G

I I J K L M N O P Q R S T U V W X Y Z A B C D E F G H

J J K L M N O P Q R S T U V W X Y Z A B C D E F G H I

K K L M N O P Q R S T U V W X Y Z A B C D E F G H I J

L L M N O P Q R S T U V W X Y Z A B C D E F G H I J K

M M N O P Q R S T U V W X Y Z A B C D E F G H I J K L

N N O P Q R S T U V W X Y Z A B C D E F G H I J K L M

O O P Q R S T U V W X Y Z A B C D E F G H I J K L M N

P P Q R S T U V W X Y Z A B C D E F G H I J K L M N O

Q Q R S T U V W X Y Z A B C D E F G H I J K L M N O P

R R S T U V W X Y Z A B C D E F G H I J K L M N O P Q

S S T U V W X Y Z A B C D E F G H I J K L M N O P Q R

T T U V W X Y Z A B C D E F G H I J K L M N O P Q R S

U U V W X Y Z A B C D E F G H I J K L M N O P Q R S T

V V W X Y Z A B C D E F G H I J K L M N O P Q R S T U

W W X Y Z A B C D E F G H I J K L M N O P Q R S T U V

X X Y Z A B C D E F G H I J K L M N O P Q R S T U V W

Y Y Z A B C D E F G H I J K L M N O P Q R S T U V W X

Z Z 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

维吉尼亚密码(类似于今天我们所说的置换密码)引入了“密钥”的概念,即根据密钥来决定用哪一行的密表来进行替换,以此来对抗字频统计。假如以上面第一行代表明文字母,左面第一列代表密钥字母,对如下明文加密:

TO BE OR NOT TO BE THAT IS THE QUESTION

当选定RELATIONS作为密钥时,加密过程是:明文一个字母为T,第一个密钥字母为R,因此可以找到在R行中代替T的为K,依此类推,得出对应关系如下:

密钥:RELAT IONSR ELATI ONSRE LATIO NSREL

明文:TOBEO RNOTT OBETH ATIST HEQUE STION

密文:KSMEH ZBBLK SMEMP OGAJX SEJCS FLZSY

历史上以维吉尼亚密表为基础又演变出很多种加密方法,其基本元素无非是密表与密钥,并一直沿用到二战以后的初级电子密码机上。

写一个java加密程序

/**

* SHA-1加密函数

*

*

*/

public class SsytemSha1 {

private final int[] abcde = {

0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0

};

// 摘要数据存储数组

private int[] digestInt = new int[5];

// 计算过程中的临时数据存储数组

private int[] tmpData = new int[80];

// 计算sha-1摘要

private int process_input_bytes(byte[] bytedata) {

// 初试化常量

System.arraycopy(abcde, 0, digestInt, 0, abcde.length);

// 格式化输入字节数组,补10及长度数据

byte[] newbyte = byteArrayFormatData(bytedata);

// 获取数据摘要计算的数据单元个数

int MCount = newbyte.length / 64;

// 循环对每个数据单元进行摘要计算

for (int pos = 0; pos MCount; pos++) {

// 将每个单元的数据转换成16个整型数据,并保存到tmpData的前16个数组元素中

for (int j = 0; j 16; j++) {

tmpData[j] = byteArrayToInt(newbyte, (pos * 64) + (j * 4));

}

// 摘要计算函数

encrypt();

}

return 20;

}

// 格式化输入字节数组格式

private byte[] byteArrayFormatData(byte[] bytedata) {

// 补0数量

int zeros = 0;

// 补位后总位数

int size = 0;

// 原始数据长度

int n = bytedata.length;

// 模64后的剩余位数

int m = n % 64;

// 计算添加0的个数以及添加10后的总长度

if (m 56) {

zeros = 55 - m;

size = n - m + 64;

} else if (m == 56) {

zeros = 63;

size = n + 8 + 64;

} else {

zeros = 63 - m + 56;

size = (n + 64) - m + 64;

}

// 补位后生成的新数组内容

byte[] newbyte = new byte[size];

// 复制数组的前面部分

System.arraycopy(bytedata, 0, newbyte, 0, n);

// 获得数组Append数据元素的位置

int l = n;

// 补1操作

newbyte[l++] = (byte) 0x80;

// 补0操作

for (int i = 0; i zeros; i++) {

newbyte[l++] = (byte) 0x00;

}

// 计算数据长度,补数据长度位共8字节,长整型

long N = (long) n * 8;

byte h8 = (byte) (N 0xFF);

byte h7 = (byte) ((N 8) 0xFF);

byte h6 = (byte) ((N 16) 0xFF);

byte h5 = (byte) ((N 24) 0xFF);

byte h4 = (byte) ((N 32) 0xFF);

byte h3 = (byte) ((N 40) 0xFF);

byte h2 = (byte) ((N 48) 0xFF);

byte h1 = (byte) (N 56);

newbyte[l++] = h1;

newbyte[l++] = h2;

newbyte[l++] = h3;

newbyte[l++] = h4;

newbyte[l++] = h5;

newbyte[l++] = h6;

newbyte[l++] = h7;

newbyte[l++] = h8;

return newbyte;

}

private int f1(int x, int y, int z) {

return (x y) | (~x z);

}

private int f2(int x, int y, int z) {

return x ^ y ^ z;

}

private int f3(int x, int y, int z) {

return (x y) | (x z) | (y z);

}

private int f4(int x, int y) {

return (x y) | x (32 - y);

}

// 单元摘要计算函数

private void encrypt() {

for (int i = 16; i = 79; i++) {

tmpData[i] = f4(tmpData[i - 3] ^ tmpData[i - 8] ^ tmpData[i - 14] ^

tmpData[i - 16], 1);

}

int[] tmpabcde = new int[5];

for (int i1 = 0; i1 tmpabcde.length; i1++) {

tmpabcde[i1] = digestInt[i1];

}

for (int j = 0; j = 19; j++) {

int tmp = f4(tmpabcde[0], 5) +

f1(tmpabcde[1], tmpabcde[2], tmpabcde[3]) + tmpabcde[4] +

tmpData[j] + 0x5a827999;

tmpabcde[4] = tmpabcde[3];

tmpabcde[3] = tmpabcde[2];

tmpabcde[2] = f4(tmpabcde[1], 30);

tmpabcde[1] = tmpabcde[0];

tmpabcde[0] = tmp;

}

for (int k = 20; k = 39; k++) {

int tmp = f4(tmpabcde[0], 5) +

f2(tmpabcde[1], tmpabcde[2], tmpabcde[3]) + tmpabcde[4] +

tmpData[k] + 0x6ed9eba1;

tmpabcde[4] = tmpabcde[3];

tmpabcde[3] = tmpabcde[2];

tmpabcde[2] = f4(tmpabcde[1], 30);

tmpabcde[1] = tmpabcde[0];

tmpabcde[0] = tmp;

}

for (int l = 40; l = 59; l++) {

int tmp = f4(tmpabcde[0], 5) +

f3(tmpabcde[1], tmpabcde[2], tmpabcde[3]) + tmpabcde[4] +

tmpData[l] + 0x8f1bbcdc;

tmpabcde[4] = tmpabcde[3];

tmpabcde[3] = tmpabcde[2];

tmpabcde[2] = f4(tmpabcde[1], 30);

tmpabcde[1] = tmpabcde[0];

tmpabcde[0] = tmp;

}

for (int m = 60; m = 79; m++) {

int tmp = f4(tmpabcde[0], 5) +

f2(tmpabcde[1], tmpabcde[2], tmpabcde[3]) + tmpabcde[4] +

tmpData[m] + 0xca62c1d6;

tmpabcde[4] = tmpabcde[3];

tmpabcde[3] = tmpabcde[2];

tmpabcde[2] = f4(tmpabcde[1], 30);

tmpabcde[1] = tmpabcde[0];

tmpabcde[0] = tmp;

}

for (int i2 = 0; i2 tmpabcde.length; i2++) {

digestInt[i2] = digestInt[i2] + tmpabcde[i2];

}

for (int n = 0; n tmpData.length; n++) {

tmpData[n] = 0;

}

}

// 4字节数组转换为整数

private int byteArrayToInt(byte[] bytedata, int i) {

return ((bytedata[i] 0xff) 24) | ((bytedata[i + 1] 0xff) 16) |

((bytedata[i + 2] 0xff) 8) | (bytedata[i + 3] 0xff);

}

// 整数转换为4字节数组

private void intToByteArray(int intValue, byte[] byteData, int i) {

byteData[i] = (byte) (intValue 24);

byteData[i + 1] = (byte) (intValue 16);

byteData[i + 2] = (byte) (intValue 8);

byteData[i + 3] = (byte) intValue;

}

// 将字节转换为十六进制字符串

private static String byteToHexString(byte ib) {

char[] Digit = {

'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C',

'D', 'E', 'F'

};

char[] ob = new char[2];

ob[0] = Digit[(ib 4) 0X0F];

ob[1] = Digit[ib 0X0F];

String s = new String(ob);

return s;

}

// 将字节数组转换为十六进制字符串

private static String byteArrayToHexString(byte[] bytearray) {

String strDigest = "";

for (int i = 0; i bytearray.length; i++) {

strDigest += byteToHexString(bytearray[i]);

}

return strDigest;

}

// 计算sha-1摘要,返回相应的字节数组

public byte[] getDigestOfBytes(byte[] byteData) {

process_input_bytes(byteData);

byte[] digest = new byte[20];

for (int i = 0; i digestInt.length; i++) {

intToByteArray(digestInt[i], digest, i * 4);

}

return digest;

}

// 计算sha-1摘要,返回相应的十六进制字符串

public String getDigestOfString(byte[] byteData) {

return byteArrayToHexString(getDigestOfBytes(byteData));

}

public static void main(String[] args) { //测试通过

String data = "123";

String digest = new SsytemSha1().getDigestOfString(data.getBytes());

}

}


文章标题:java恺撒密码代码 java实现凯撒密码
文章来源:http://cdkjz.cn/article/dodeedo.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220