維吉尼亞加密c語言代碼,維吉利亞密碼加密算法

本文目錄一覽:

維吉尼亞密碼c語言求改。

#include stdlib.h

#include stdio.h

#include string.h

#define N 10000

void function(char message[],char key[],int mode); //加解密函數

int main()

{

int choose;

char m[N],key[N];

printf(“維吉尼亞加密,請輸入1;解密,請輸入2:\n”);

scanf(“%d”,choose);

getchar();

if (choose == 1 || choose == 2)

{

if (choose == 1)

printf(“輸入明文:\n”);

if (choose == 2)

printf(“輸入密文:\n”);

gets(m);

printf(“輸入密鑰:\n”);

gets(key);

function(m,key,choose);

}

else

printf(“輸入錯誤!\n”);

return 0;

}

void function(char message[],char key[],int mode) //加解密函數

{

int i, j = 0; //j控制key的輪迴

int len_k = strlen(key); //密鑰長度

char s[N];

for(i=0; message[i]!=’\0′; i++)

{

if(message[i] == 32) //判斷空格

s[i]=’ ‘;

else

{

if (mode == 1)

s[i]=(int(message[i]-‘a’)+int(key[j%len_k]-‘a’))%26+97;

if (mode == 2)

s[i]=(int(message[i]-‘a’)-int(key[j%len_k]-‘a’)+26)%26+97;

j++;

}

printf(“%c”,s[i]);

}

printf(“\n”);

}

gets(l);//不加這句M就輸入不了為什麼?

是因為沒有這句的話,按的回車鍵就輸成m了。

連用兩個輸入語句時,需要考慮回車鍵,就像我代碼里的getchar()。

維吉尼亞密碼 C++

給,網上的C++的基本都有問題,我給你改好一個,已經編譯運行確認,

#includeiostream

using namespace std;

#define MINCHAR 32

#define CHARSUM 94

char table[CHARSUM][CHARSUM];

bool Init();

bool Encode(char* key, char* source, char* dest);

bool Dncode(char* key, char* source, char* dest);

int main()

{

if(!Init())

{

cout “初始化錯誤!” endl;

return 1;

}

char key[256];

char str1[256];

char str2[256];

int operation;

while(1)

{

do

{

cout “請選擇一個操作:1. 加密; 2. 解密; -1. 退出\n”;

cin operation;

}while(operation != -1 operation != 1 operation != 2);

if(operation == -1)

return 0;

else if(operation == 1)//加密

{

cout “請輸入密鑰:”;

cin key;

cout “請輸入待加密字符串:”;

cin str1;

Encode(key, str1, str2);

cout “加密後的字符串:” str2 endl;

}

else if(operation == 2)//解密

{

cout “請輸入密鑰:”;

cin key;

cout “請輸入待解密字符串:”;

cin str1;

Dncode(key, str1, str2);

cout “解密後的字符串:” str2 endl;

}

cout endl;

}

return 0;

}

// 初始化維吉尼亞方陣

bool Init()

{

int i, j;

for(i = 0; i CHARSUM; i++)

{

for(j = 0; j CHARSUM; j++)

{

table[i][j] = MINCHAR + (i + j) % CHARSUM;

}

}

return true;

}

// 加密

// key:密鑰

// source:待加密的字符串

// dest:經過加密後的字符串

bool Encode(char* key, char* source, char* dest)

{

char* tempSource = source;

char* tempKey = key;

char* tempDest = dest;

do

{

*tempDest = table[(*tempKey) – MINCHAR][(*tempSource) – MINCHAR];

tempDest++;

if(!(*(++tempKey)))

tempKey = key;

}while(*tempSource++);

dest[strlen(source)] = 0;

return true;

}

// 解密

// key:密鑰

// source:待解密的字符串

// dest:經過解密後的字符串

bool Dncode(char* key, char* source, char* dest)

{

char* tempSource = source;

char* tempKey = key;

char* tempDest = dest;

char offset;

do

{

offset = (*tempSource) – (*tempKey);

offset = offset = 0 ? offset : offset + CHARSUM;

*tempDest = MINCHAR + offset;

tempDest++;

if(!(*(++tempKey)))

tempKey = key;

}while(*++tempSource);

dest[strlen(source)] = 0;

return true;

}

寫一個維吉尼亞加密和解碼的C語言程序,具體要求如下。不要用GOTO, 不要有MAGIC NUMBER。

#include stdio.h

#define MINCHAR 32

#define CHARSUM 94

int encode(char* key, char* source, char* dest);

int decode(char* key, char* source, char* dest);

char table[CHARSUM][CHARSUM];

int main()

{

int i, j;

char key[256];

char source[256];

char destination[256];

int operation;

FILE *fp;

/* Initial the Vigenere table */

for(i = 0; i CHARSUM; i++)

for(j = 0; j CHARSUM; j++)

table[i][j] = MINCHAR + (i + j) % CHARSUM;

printf(“please choose one operation code:\n”);

printf(“1. Encode; 2. Decode; Others. Exit.\n”);

scanf(“%d”, operation);

fflush(stdin);

switch (operation)

{

case 1:

printf(“please input the key code:\n”);

gets(key);

fflush(stdin);

printf(“please input the source code you want to encode:\n”);

gets(source);

fflush(stdin);

encode(key, source, destination);

printf(“after encode is: \n”);

printf(“%s\n”, destination);

fp=fopen(“key.txt”, “w”);

fprintf(fp, “%s”, key);

fclose(fp);

fp=fopen(“source.txt”, “w”);

fprintf(fp, “%s”, source);

fclose(fp);

fp=fopen(“destination.txt”, “w”);

fprintf( fp, “%s”,destination);

fclose(fp);

break;

case 2:

printf(“please input the key code:\n”);

gets(key);

fflush(stdin);

printf(“please input the source code you want to decode:\n”);

gets(source);

fflush(stdin);

decode(key, source, destination);

printf(“after decode is: \n”);

printf(“%s\n”, destination);

fp=fopen(“key.txt”, “w”);

fprintf(fp, “%s”, key);

fclose(fp);

fp=fopen(“source.txt”, “w”);

fprintf(fp, “%s”, source);

fclose(fp);

fp=fopen(“destination.txt”, “w”);

fprintf(fp, “%s”, destination);

fclose(fp);

break;

default:

return 0;

}

return 0;

}

int encode(char* key, char* source, char* dest)

{

char* tempSource = source;

char* tempKey = key;

char* tempDest = dest;

do

{

*tempDest = table[(*tempKey) – MINCHAR][(*tempSource) – MINCHAR];

tempDest++;

if (!(*(++tempKey)))

tempKey = key;

} while(*tempSource++);

dest[strlen(source)] = ‘\0’;

return 1;

}

int decode(char* key, char* source, char* dest)

{

char* tempSource = source;

char* tempKey = key;

char* tempDest = dest;

char offset;

do

{

offset = (*tempSource) – (*tempKey);

offset = offset = 0 ? offset : offset + CHARSUM;

*tempDest = MINCHAR + offset;

tempDest++;

if (!(*(++tempKey)))

tempKey = key;

} while(*++tempSource);

dest[strlen(source)] = ‘\0’;

return 1;

}

(c語言)求密碼

360問答

求古典密碼學的c語言代碼

guiyids0033 LV122014-01-16

各位高手小弟急需一個維吉尼亞密碼的程序。幫幫忙

滿意答案

aa7777777777qq

LV12

2014-01-16

給: 維吉尼亞密碼的C語言源代碼 設m表示明文序列,k表示密鑰序列 #include ctype.h #include stdio.h #include stdlib.h #include string.h #include conio.h void crypt(char m[],char k[],char r[]) { int i,j,s=0; j=strlen(k); for(i=0;m[i];i++) m[i]=tolower(m[i]); for(i=0;m[i];i++) if(isalpha(m[i])) { r[i]=(m[i]-‘a’+k[s%j]-‘a’)%26+’a’; s++;/* s用來跳過明文中的空格字符 */ } else r[i]=m[i]; r[i]=0;/* 密文字符串結束符 */ for(i=0;r[i];i++) r[i]=toupper(r[i]); } void decrypt(char c[],char k[],char m[]) { int i,j,s=0; j=strlen(k); for(i=0;c[i];i++) c[i]=tolower(c[i]); for(i=0;c[i];i++) if(isalpha(c[i])) { m[i]=(c[i]-k[s%j]+26)%26+’a’; s++; } else m[i]=c[i]; m[i]=0; } void main() { char m[]=”welcome to my blog.i am bugeyes.”;//我這裡是賦值了一個固定的字符串為明文序列,你也可以做成用戶輸入的 char k[]=”bugeyeswuyan”;//我這裡是賦值了一個固定的字符串為密鑰序列,你也可以做成用戶輸入的 char c[80]; char d[80]; system(“cls”);; crypt(m,k,c); decrypt(c,k,d); puts(m); puts(k); puts(c); puts(d); }

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/250795.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-13 13:30
下一篇 2024-12-13 13:31

相關推薦

  • Python暴力破解wifi密碼

    簡介:本文將從多個方面詳細介紹使用Python暴力破解wifi密碼的方法。代碼實例將被包含在本文中的相關小節中。 一、如何獲取wifi密碼 在使用Python暴力破解wifi密碼之…

    編程 2025-04-27
  • Python 編寫密碼安全檢查工具

    本文將介紹如何使用 Python 編寫一個能夠檢查用戶輸入密碼安全強度的工具。 一、安全強度的定義 在實現安全檢查之前,首先需要明確什麼是密碼的安全強度。密碼的安全強度通常包括以下…

    編程 2025-04-27
  • jiia password – 保護您的密碼安全

    你是否曾經遇到過忘記密碼、密碼泄露等問題?jiia password 正是一款為此而生的解決方案。本文將從加密方案、密碼管理、多平台支持等多個方面,為您詳細闡述 jiia pass…

    編程 2025-04-27
  • Python解鎖Wi-Fi密碼

    想要解鎖Wi-Fi密碼,你需要使用Python編程語言。Python是一種高層次、面向對象、解釋型的動態編程語言。許多人都可以輕鬆學習Python,並用它來編寫各種各樣的程序。在本…

    編程 2025-04-27
  • Python隨機密碼生成代碼

    本文將會從以下幾個方面對Python隨機密碼生成代碼進行詳細闡述: 一、密碼生成原理 密碼生成的原理是利用隨機數生成器生成隨機字符或數字,根據一定的規則組合成所需要的密碼。 在Py…

    編程 2025-04-27
  • Ingress要密碼強制卸載

    當我們需要強制卸載Ingress應用時,我們可能會發現需要驗證Google賬戶的密碼才能夠進行操作,因此本文將教大家如何繞過Google驗證,實現Ingress應用的強制卸載。 一…

    編程 2025-04-25
  • 群暉root密碼詳解

    一、root密碼的概念 root密碼是指用於登錄群暉系統管理員賬戶root的密碼。root是擁有系統最高權限的賬戶,使用root賬戶可以操作系統中的所有資源和數據,因此root密碼…

    編程 2025-04-25
  • 深入理解Linux密碼

    一、密碼的基本原理 密碼是防止惡意訪問的基本手段。在Linux系統中,用戶的密碼存儲在/etc/shadow文件中,只有特權用戶能夠讀取。密碼的存儲採用MD5或SHA256算法進行…

    編程 2025-04-24
  • MySQL8修改root密碼詳解

    MySQL是一款開源的關係型數據庫管理系統,常用於Web應用程序中作為數據庫服務器。作為MySQL的最高權限者,root賬號可以對數據庫進行管理和控制。在MySQL的安裝和使用過程…

    編程 2025-04-23
  • Java加密算法詳解

    一、對稱加密算法 1、基本介紹 對稱加密算法是一種所謂的私密密鑰加密算法,其中使用同一個密鑰執行加密和解密操作。常見的對稱加密算法有DES、AES、RC4等。 2、使用示例代碼 i…

    編程 2025-04-23

發表回復

登錄後才能評論