當前位置:首頁 » 編程語言 » 字元密碼的c語言代碼
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

字元密碼的c語言代碼

發布時間: 2023-02-09 06:42:13

c語言 簡單對字母進行加密

1、在我們的編輯頁面輸入以下代碼。

Ⅱ 用C語言設計一個加密 解密 密碼 的程序。

// playFair 加密 你參考下 ...
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#define x 50
char MiYao[x],PassWord[x],AddPass[x],Table[5][5],Map[25];
bool Visit[27]={false};
char English[27]="abcdefghijklmnopqrstuvwxyz";
void Input()
{
printf("請輸入密鑰:\t"); scanf("%s",MiYao);
printf("請輸入待加密密碼:\t"); scanf("%s",PassWord);
}
void Fun_5x5()
{
int count = 0,V =0;
/*標記密鑰內字元為: true*/
for(int i=0;MiYao[i]!='\0';i++)
if(strchr(English,MiYao[i])!=NULL)
Visit[strchr(English,MiYao[i])-English] = true;
/*執行密鑰矩陣操作 並標記已使用字元:true*/
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
{
if(count<strlen(MiYao))
Table[i][j] = MiYao[count++];
else
{
while(Visit[V] != false) V++;
Table[i][j] = English[V];
Visit[V++] = true;
}
}
puts("∞∞∞密鑰矩陣為∞∞∞");
for(int i=0;i<5;i++)
{ for(int j=0;j<5;j++)
printf("%3c",Table[i][j]);
puts("");
}
puts("∞∞∞∞∞∞∞∞∞∞∞");

}
int IsVisited(char ch)
{
return Visit[strchr(English,ch)-English]; //false 未出現過
}
void TabletoMap()
{ int count=0;
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
Map[count++]=Table[i][j];
Map[count]='\0';
}
void Judge()
{
int len = strlen(PassWord),i,j,k;
memset(AddPass,0,sizeof(char));
/*一對對去字母,剩下單個字母,則不變化,直接放入加密串中.*/
if(len%2){
AddPass[len-1] = PassWord[len-1];
len -=1;
}
/*一對中 密鑰矩陣中 存在矩陣 eg.ab 先輸出a同行頂點在輸出b同行頂點*/
int row1,low1,row2,low2,a1,a2;
for(i=0;i<len;i+=2)
{
char c1,c2;
c1 = PassWord[i];
c2 = PassWord[i+1];
/*一對中 兩字母相同 無變化*/
/*一對中 有字母不在密鑰矩陣中 無變化*/
if(c1 == c2 || ( !IsVisited(c1)||!IsVisited(c2)))
{ AddPass[i] = c1;
AddPass[i+1]=c2;
}else{
a1 = strchr(Map,c1)-Map;
row1 = a1/5; low1 = a1%5;
a2 = strchr(Map,c2)-Map;
row2 = a2/5; low2 = a2%5;
/*一對中 字元出現在同行或同列 簡單swap字元*/
if(row1 == row2 || low1 == low2)
{
AddPass[i] = c2;
AddPass[i+1] = c1;
}else{
AddPass[i] = Table[row1][low2];
AddPass[i+1] = Table[row2][low1];
}
}
}AddPass[len+1]='\0';
puts("加密後字元串:");
puts(AddPass);
puts("原串是:");
puts(PassWord);
}
int main()
{
Input();
Fun_5x5();
TabletoMap();
Judge();
return 0;
}

Ⅲ 請問,用C語言如何實現密碼輸入

c語言中可採用getch()函數來實現輸入密碼字元時,不顯示字元到終端上,這時,只需要顯示出一個相應的*就可以達到效果了。參考代碼及運行效果如下圖:

Ⅳ 編寫函數完成字元串的加密與解密(c語言)

C語言代碼和運行結果如下:

輸出符合示例,加解密均正確,望採納~

附源碼鏈接:字元串加解密

Ⅳ 如何對字元串進行MD5加密,用C語言實現,給出源代碼和加密函數

#include <stdio.h>
#define S11 7
#define S12 12
#define S13 17
#define S14 22
#define S21 5
#define S22 9
#define S23 14
#define S24 20
#define S31 4
#define S32 11
#define S33 16
#define S34 23
#define S41 6
#define S42 10
#define S43 15
#define S44 21
#define R_memset(x, y, z) memset(x, y, z)
#define R_memcpy(x, y, z) memcpy(x, y, z)
#define R_memcmp(x, y, z) memcmp(x, y, z)
typedef unsigned long UINT4;
typedef unsigned char *POINTER;
typedef struct {
/*四個32bits數,用於存放最終計算得到的消息摘要.當消息長度>512bits時,也用於存放每個512bits的中間結果*/
UINT4 state[4];
/*存儲原始信息的bits數長度,不包括填充的bits,最長為2^64 bits*/
UINT4 count[2];
/*存放輸入的信息的緩沖區,512bits*/
unsigned char buffer[64];
} MD5_CTX;
static void MD5Transform(UINT4[4], unsigned char[64]);
static void Encode(unsigned char *, UINT4 *, unsigned int);
static void Decode(UINT4 *, unsigned char *, unsigned int);
/*
用於bits填充的緩沖區,當欲加密的信息的bits數被512除其餘數為448時,需要填充的bits的最大值為512=64*8*/
static unsigned char PADDING[64] = {
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
/*接下來的這幾個宏定義是md5演算法規定的,就是對信息進行md5加密都要做的運算*/
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
#define H(x, y, z) ((x) ^ (y) ^ (z))
#define I(x, y, z) ((y) ^ ((x) | (~z)))
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
#define FF(a, b, c, d, x, s, ac) {\
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac);\
(a) = ROTATE_LEFT ((a), (s));\
(a) += (b);\
}
#define GG(a, b, c, d, x, s, ac) {\
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac);\
(a) = ROTATE_LEFT ((a), (s));\
(a) += (b);\
}
#define HH(a, b, c, d, x, s, ac) {\
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac);\
(a) = ROTATE_LEFT ((a), (s));\
(a) += (b);\
}
#define II(a, b, c, d, x, s, ac) {\
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac);\
(a) = ROTATE_LEFT ((a), (s));\
(a) += (b);\
}

Ⅵ c語言,字元串密碼登錄程序

這樣修改,是否符合原來的題意?

Ⅶ 用C語言實現任意字元串的加密,其中,字母用凱撒加密方法加密,非字母不變

我盡量用注釋闡述了思路,希望可以幫到你!!

#include<stdio.h>
#include<string.h>
#define N 80 //可加密字元串最大長度

char plaintext[N]={0}; //明文,輸入時輸入字元,參與運算時強制轉換成整數
int ciphertext[N]={0}; //密文,保存成整數,輸出時強制轉換成字元
int k; //後(右)移位數,相當於密鑰

void getPlainText() //獲得明文字元串
{
printf("請輸入明文:");
scanf("%s",plaintext);
printf("\n");
}

void getLength() //獲取後(右)移位數(密鑰)
{
printf("請輸入後移的位數:");
scanf("%d",&k);
k%=26; //因為字母只有26個,所以超過26相當於重復
}

void Caesar_cipher() //凱撒加密,本程序採用的是字母循環後(右)移
{
unsigned int i;

for(i=0;i<strlen(plaintext);i++)
{
//兩個bool類型的變數是為了判斷字元是否是字母(包括大寫和小寫)
bool flag1=plaintext[i]>='a'&&plaintext[i]<='z';
bool flag2=plaintext[i]>='A'&&plaintext[i]<='Z';

if(flag1||flag2){ //如果是字母,加密
ciphertext[i]=(int)plaintext[i]+k; //字母在字母表中後(右)移K位
if(ciphertext[i]>(int)'z'){ //保證是循環後(右)移
ciphertext[i]-=26;
}
}
else //非字母字元,不做處理,原樣保存
ciphertext[i]=(int)plaintext[i];

}

}

void printCipherText() //輸出加密後的密文
{
unsigned int i;
printf("\n加密後的密文是:");
for(i=0;i<strlen(plaintext);i++) //把參與計算後是整數強制轉換成對應的字元
printf("%c",(char)ciphertext[i]);
printf("\n");

}

void main()
{
getPlainText(); //明文
getLength(); //後(右)移位數
Caesar_cipher(); //凱撒加密
printCipherText(); //密文

}

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

給:
維吉尼亞密碼的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);
}

Ⅸ C語言編程:輸入一行字元串,按給出的規則譯成密碼。

#include<string.h>
#include<stdio.h>
main()
{char a[100];
int n=2,i;
printf("input zifuchuan:\n");
gets(a);
printf("input n:\n");
scanf("%d",&n);
for(i=0;i<strlen(a);i++)
if(a[i]+n>'z') a[i]=a[i]+n-26;
else if (a[i]>='A' && a[i]<='Z' && a[i]+n>'Z') a[i]=a[i]+n-26;
else a[i]=a[i]+n;
for(i=0;i<strlen(a);i++)
printf("%c",a[i]);
}
測試通過

Ⅹ c語言 要求輸入一個有真常的字元串後,將對該字元進行加密處理,字元

給一個很簡單的代碼:
#include
#include
void
main(){
int
i,j=0;
char
s[80],s1[81],ch;
gets(s);
scanf("%c",&ch);
for(i=0;i
評論
0
0
載入更多