网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

安全算法函数c语言代码,安全的密码c语言

IDEA加密算法的C语言实现

1、数据加密的基本过程就是对原来为明文的文件或数据按某种算法进行处理,使其成为不可读的一段代码,通常称为“密文”,使其只能在输入相应的密钥之后才能显示出本来内容,通过这样的途径来达到保护数据不被非法人窃取、阅读的目的。

创新互联公司成立于2013年,我们提供高端成都网站建设公司网站制作成都网站设计公司、网站定制、成都全网营销小程序制作、微信公众号开发、seo优化服务,提供专业营销思路、内容策划、视觉设计、程序开发来完成项目落地,为成都宴会酒店设计企业提供源源不断的流量和订单咨询。

2、常见加密算法

DES(Data Encryption Standard):数据加密标准,速度较快,适用于加密大量数据的场合;

3DES(Triple DES):是基于DES,对一块数据用三个不同的密钥进行三次加密,强度更高;

RC2和 RC4:用变长密钥对大量数据进行加密,比 DES 快;

IDEA(International Data Encryption Algorithm)国际数据加密算法:使用 128 位密钥提供非常强的安全性;

RSA:由 RSA 公司发明,是一个支持变长密钥的公共密钥算法,需要加密的文件块的长度也是可变的;

DSA(Digital Signature Algorithm):数字签名算法,是一种标准的 DSS(数字签名标准);

AES(Advanced Encryption Standard):高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法;

BLOWFISH,它使用变长的密钥,长度可达448位,运行速度很快;

其它算法,如ElGamal、Deffie-Hellman、新型椭圆曲线算法ECC等。

比如说,MD5,你在一些比较正式而严格的网站下的东西一般都会有MD5值给出,如安全焦点的软件工具,每个都有MD5。

3、例程:

#includestdio.h

#includeprocess.h

#includeconio.h

#includestdlib.h

#define maxim 65537

#define fuyi 65536

#define one 65536

#define round 8

unsigned int inv(unsigned int xin);

unsigned int mul(unsigned int a,unsigned int b);

void cip(unsigned int IN[4],unsigned int OUT[4],unsigned int Z[7][10]);

void key(unsigned int uskey[9],unsigned int Z[7][10]);

void de_key(unsigned int Z[7][10],unsigned int DK[7][10]);

void main()

int i,j,k,x;

unsigned int Z[7][10],DK[7][10],XX[5],TT[5],YY[5];

unsigned int uskey[9];

FILE *fpout,*fpin;

printf("\n Input Key");

for(i=1;i=8;i++)

scanf("%6u",uskey[i]);

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

uskey[i]=100+i*3;

key(uskey,Z);/*产生加密子密钥*/

de_key(Z,DK);/*计算解密子密钥*/

if((fpin=fopen("ekey.txt","w"))==NULL)

{

printf("cannot open file!");

exit(EXIT_FAILURE);

}

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

{

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

fprintf(fpin,"%6u",Z[i][j]);

fprintf(fpin,"\n");

}

fclose(fpin);

/*XX[1..5]中为明文*/

for(i=0;i4;i++) XX[i]=2*i+101;

clrscr();

printf("Ming wen %6u %6u %6u %6u \n",XX[0],XX[1],XX[2],XX[3]);

if((fpin=(fopen("ideaming.txt","w")))==NULL)

{printf("cannot open file!");

exit(EXIT_FAILURE);

}

fprintf(fpin,"%6u,%6u,%6u,%6u \n",XX[0],XX[1],XX[2],XX[3]);

fclose(fpin);

for(i=1;i=30000;i++)

cip(XX,YY,Z);/*用密钥Z加密XX中的明文并存在YY中*/

printf("\n\n Mingwen %6u %6u %6u %6u \n",YY[0],YY[1],YY[2],YY[3]);

if((fpin=fopen("ideamiwn.txt","w"))==NULL)

{

printf("cannot open file!");

exit(EXIT_FAILURE);

}

fprintf(fpout,"%6u %6u %6u %6u\n",YY[0],YY[1],YY[2],YY[3]);

{

printf("cannot open file!");

exit(EXIT_FAILURE);

}

fprintf(fpout,"%6u %6u %6u %6u \n",YY[0],YY[1],YY[2],YY[3]);

fclose(fpout);

for(i=1;i=30000;i++)

cip(YY,TT,DK);/*encipher YY to TT with Key DK*/

printf("\n Jie Mi %6u %6u %6u %6u \n",TT[0],TT[1],TT[2],TT[3]);

if((fpout=fopen("dideaout.txt","w"))==NULL)

{

printf("cannot open file!");

exit(EXIT_FAILURE);

}

fprintf(fpout,"%6u %6u %6u %6u \n",TT[0],TT[1],TT[2],TT[3]);

fclose(fpout);

}

/* 此函数执行IDEA算法中的加密过程*/

void cip(unsigned int IN[4],unsigned int OUT[4],unsigned int Z[7][10])

{

unsigned int r,x1,x2,x3,x4,kk,t1,t2,a;

x1=IN[0];x2=IN[1];x3=IN[2];x4=IN[3];

for(r=1;r=8;r++)

{

/* 对64位的块进行分组运算*/

x1=mul(x1,Z[1][r]);x4=mul(x4,Z[4][r]);

x2=x2+Z[2][r]one;x3=(x3+Z[3][r])one;

/* MA结构的函数 */

kk=mul(Z[5][r],(x1^x3));

t1=mul(Z[6][r],(kk+(x2^x4))one;

/* 随机变换PI*/

x1=x1^t1;x4=x4^t2;a=x2^t2;x2=x3^t1;x3=a;

}

/* 输出转换*/

OUT[0]=mul(x1,Z[1][round+1]);

OUT[3]=mul(x4,Z[1][round+1]);

OUT[1]=(x3+Z[2][round+1])one;

OUT[2]=(x2+Z[3][round+1])one;

}

/* 用高低算法上实现乘法运算*/

unsigned int mul(unsigned int a,unsigned int b)

{

long int p;

long unsigned q;

if(a==0) p=maxim-b;

else if(b==0) p=maxim-a;

else

{

q=(unsigned long)a*(unsigned long)b;

p=(qone)-(q16);

if(p=0) p=p+maxim;

{

return (unsigned) (pone);

}

/*通过Euclidean gcd算法计算xin的倒数*/

unsigned int inv(unsigned int xin)

{

long n1,n2,q,r,b1,b2,t;

if(xin==0)

b2=0;

else

{n1=maxim;n2=xin;b2=1;b1=0;

do{

r=(n1%n2);q=(n1-r)/n2;

if(r==0)

if(b20) b2=maxim+b2;

else

{n1=n2;n2=r;

t=b2;

b2=b1-q*b2;b1=t;

}

}while(r!=0);

}

return (unsigned long int)b2;

}

/*产生加密子密钥Z*/

void key(unsigned int uskey[9],unsigned int Z[7][10])

{

unsigned int S[54];

int i,j,r;

for(i=1;i9;i++)

S[i-1]=uskey[i];

/* shifts */

for(i=8;i54;i++)

{

if(i+2)%8==0)/* 对于S[14],S[22],...进行计算 */

S[i]=((S[i-7]0)^(S[i-14]7)one;

else if((i+1)%8==0)/* 对于S[15],S[23],...进行计算 */

S[i]=((S[i-15]9)^(S[i-14]7)one;

else

S[i]=((S[i-7]9)^(S[i-6]7)one;

}

/*取得子密钥*/

for(r=1;r=round+1;r++)

for(j=1;j7;j++)

Z[j][r]=S[6*(r-1)+j-1];

}

/* 计算解子密钥DK */

void de_key(unsigned int Z[7][10],unsigned int DK[7][10])

{

int j;

for(j=1;j=round+1;j++)

{DK[1][round-j+2]=inv(Z[1][j]);

DK[4][round-j+2]=inv(Z[4][j]);

if(i==1|j==round+1)

{

DK[2][round-j+2]=(fuyi-Z[2][j])one;

DK[3][round-j+2]=(fuyi-Z[3][j])one;

}

else

{

DK[2][round-j+2]=inv(Z[3][j]);

DK[3][round-j+2]=inv(Z[2][j]);

}

}

for(j=1;j=round+1;j++)

{

DK[5][round-j+2]=inv(Z[5][j]);

DK[6][round-j+2]=inv(Z[6][j]);

}

}

C语言微软安全函数问题(strcpy()函数)

例如: 定义一个字符串char a[20],和一个字符串c[]="i am a teacher!"; 把c复制到a中就可以这样用:strcpy(a,c); 这个函数包含在头文件 中. 程序代码: #include #include void main() {char a[20],c[]="i am teacher!"; strcpy(a,c); cout

求n个数的全排列,n不定。用c语言。用于银行家算法中求安全序列

好久没用c了,所以代码可能要用到伪代码

先定义a[maxn]

用子函数递归

void p(int x)

{

if (n == x+1)

{

//foreach a print

//输出数组a

}

for (int i=1 to n)

{

a[x] = i;

p(x+1);

a[x] = 0;

}

}

主函数main调用p(n)

c语言银行家算法安全性判别

把1作为参数传给yanzheng() yanzheng(int m)

然后验证函数里修改:

work=Avaliable;

i=m;

while(im)

if (Finish[i]==falseNeed[i]=work)

{

work=work+Allocation[i];

Finish[i]=true;

anquan[k]=i;

k++;

i = 0;

}

else

i++;

}

C语言中的hash函数

Hash,一般翻译做"散列",也有直接音译为"哈希"的,就是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值。这种转换是一种压缩映射,也就是,散列值的空间通常远小于输入的空间,不同的输入可能会散列成相同的输出,而不可能从散列值来唯一的确定输入值。简单的说就是一种将任意长度的消息压缩到某一固定长度的消息摘要的函数。

HASH主要用于信息安全领域中加密算法,它把一些不同长度的信息转化成杂乱的128位的编码里,叫做HASH值. 也可以说,hash就是找到一种数据内容和数据存放地址之间的映射关系。Hash算法在信息安全方面的应用主要体现在以下的3个方面:文件校验、数字签名、鉴权协议

程程序实现

// 说明:Hash函数(即散列函数)在程序设计中的应用目标 ------ 把一个对象通过某种转换机制对应到一个

//size_t类型(即unsigned long)的整型值。

// 而应用Hash函数的领域主要是 hash表(应用非常广)、密码等领域。

// 实现说明:

// ⑴、这里使用了函数对象以及泛型技术,使得对所有类型的对象(关键字)都适用。

// ⑵、常用类型有对应的偏特化,比如string、char*、各种整形等。

// ⑶、版本可扩展,如果你对某种类型有特殊的需要,可以在后面实现专门化。

// ⑷、以下实现一般放在头文件中,任何包含它的都可使用hash函数对象。

//------------------------------------实现------------------------------------------------

#include string

using std::string;

inlinesize_thash_str(const char* s)

{

unsigned long res = 0;

for (; *s; ++s)

res = 5 * res + *s;

returnsize_t(res);

}

template class Key

struct hash

{

size_toperator () (const Key k) const;

};

// 一般的对象,比如:vector queuestring ;的对象,需要强制转化

template class Key

size_thashKey::operator () (const Key k) const

{

size_tres = 0;

size_tlen = sizeof(Key);

const char* p = reinterpret_castconst char*(k);

while (len--)

{

res = (res1)^*p++;

}

return res;

}

// 偏特化

template

size_thash string ::operator () (const string str) const

{

return hash_str(str.c_str());

}

typedef char* PChar;

template

size_thashPChar::operator () (const PChar s) const

{

return hash_str(s);

}

typedef const char* PCChar;

template

size_thashPCChar::operator () (const PCChar s) const

{

return hash_str(s);

}

template size_t hashchar::operator () (const char x) const { return x; }

template size_t hashunsigned char::operator () (const unsigned char x) const { return x; }

template size_t hashsigned char::operator () (const signed char x) const { return x; }

template size_t hashshort::operator () (const short x) const { return x; }

template size_t hashunsigned short::operator () (const unsigned short x) const { return x; }

template size_t hashint::operator () (const int x) const { return x; }

template size_t hashunsigned int::operator () (const unsigned int x) const { return x; }

template size_t hashlong::operator () (const long x) const { return x; }

template size_t hashunsigned long::operator () (const unsigned long x) const { return x; }

// 使用说明:

//

// ⑴、使用时首先由于是泛型,所以要加上关键字类型。

//

// ⑵、其次要有一个函数对象,可以临时、局部、全局的,只要在作用域就可以。

//

// ⑶、应用函数对象作用于对应类型的对象。

//----------------------- hash函数使用举例 -------------------------

#include iostream

#include vector

#include string

using namespace std;

int main()

{

vectorstring vstr⑵;

vstr[0] = "sjw";

vstr[1] = "suninf";

hashstring strhash; // 局部函数对象

cout " Hash value: " strhash(vstr[0]) endl;

cout " Hash value: " strhash(vstr[1]) endl;

cout " Hash value: " hash vectorstring () (vstr) endl;

cout " Hash value: " hashint() (100) endl; // hashint() 临时函数对象

return 0;

}


网站栏目:安全算法函数c语言代码,安全的密码c语言
转载注明:http://bjjierui.cn/article/dsceoog.html

其他资讯