Caesar (愷撒)密碼,又叫循環移位密碼。它的加密過程可表示為: E(m) = (m+k ) mod n其中,m為明文字母在字母表中的位置數;n為字母表中的字母個數;k為密鑰;E(m)為密文字母在字母表中對應的位置數。解密算法是:m = D(L) =(L-k)mod 26;2、算法設計:使。
網站制作、成都網站設計,成都做網站公司-創新互聯已向1000多家企業提供了,網站設計,網站制作,網絡營銷等服務!設計與技術結合,多年網站推廣經驗,合理的價格為您打造企業品質網站。
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!
在密碼學中,愷撒密碼(英語: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
歷史上以維吉尼亞密表為基礎又演變出很多種加密方法,其基本元素無非是密表與密鑰,并一直沿用到二戰以后的初級電子密碼機上。
/**
* 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://m.kartarina.com/article10/dodeedo.html
成都網站建設公司_創新互聯,為您提供網站設計公司、外貿建站、營銷型網站建設、域名注冊、網頁設計公司、做網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯