当前位置:首页 » 编程语言 » 字符密码的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
加载更多