看书……
密码学……
如果你要实现某种既定标准的算法,比如SHA-1,MD5啥的。请去看相关的标准。都是免费的。
百度找找
有RSA算法,MD5算法,维尼格算法
这些算法都很好找啊,我空间里面就有一篇RSA的
百度搜索
这些代码本来就难懂,涉及到数学的东西也很多
据说IBM公司DES加密的源代码公布了十几年后才有人弄懂
#include iostream.h
#include math.h
#include stdio.h
typedef int Elemtype;
Elemtype p,q,e;
Elemtype fn;
Elemtype m,c;
int flag = 0;
typedef void (*Msghandler) (void);
struct MsgMap {
char ch;
Msghandler handler;
};
/* 公钥 */
struct PU {
Elemtype e;
Elemtype n;
} pu;
/* 私钥 */
struct PR {
Elemtype d;
Elemtype n;
} pr;
/* 判定一个数是否为素数 */
bool test_prime(Elemtype m) {
if (m = 1) {
return false;
}
else if (m == 2) {
return true;
}
else {
for(int i=2; i=sqrt(m); i++) {
if((m % i) == 0) {
return false;
break;
}
}
return true;
}
}
/* 将十进制数据转化为二进制数组 */
void switch_to_bit(Elemtype b, Elemtype bin[32]) {
int n = 0;
while( b 0) {
bin[n] = b % 2;
n++;
b /= 2;
}
}
/* 候选菜单,主界面 */
void Init() {
cout"*********************************************"endl;
cout"*** Welcome to use RSA encoder ***"endl;
cout"*** a.about ***"endl;
cout"*** e.encrypt ***"endl;
cout"*** d.decrypt ***"endl;
cout"*** s.setkey ***"endl;
cout"*** q.quit ***"endl;
cout"**********************************by*Terry***"endl;
cout"press a key:"endl;
}
/* 将两个数排序,大的在前面*/
void order(Elemtype in1, Elemtype in2) {
Elemtype a = ( in1 in2 ? in1 : in2);
Elemtype b = ( in1 in2 ? in1 : in2);
in1 = a;
in2 = b;
}
/* 求最大公约数 */
Elemtype gcd(Elemtype a, Elemtype b) {
order(a,b);
int r;
if(b == 0) {
return a;
}
else {
while(true) {
r = a % b;
a = b;
b = r;
if (b == 0) {
return a;
break;
}
}
}
}
/* 用扩展的欧几里得算法求乘法逆元 */
Elemtype extend_euclid(Elemtype m, Elemtype bin) {
order(m,bin);
Elemtype a[3],b[3],t[3];
a[0] = 1, a[1] = 0, a[2] = m;
b[0] = 0, b[1] = 1, b[2] = bin;
if (b[2] == 0) {
return a[2] = gcd(m, bin);
}
if (b[2] ==1) {
return b[2] = gcd(m, bin);
}
while(true) {
if (b[2] ==1) {
return b[1];
break;
}
int q = a[2] / b[2];
for(int i=0; i3; i++) {
t[i] = a[i] - q * b[i];
a[i] = b[i];
b[i] = t[i];
}
}
}
/* 快速模幂算法 */
Elemtype modular_multiplication(Elemtype a, Elemtype b, Elemtype n) {
Elemtype f = 1;
Elemtype bin[32];
switch_to_bit(b,bin);
for(int i=31; i=0; i--) {
f = (f * f) % n;
if(bin[i] == 1) {
f = (f * a) % n;
}
}
return f;
}
/* 产生密钥 */
void produce_key() {
cout"input two primes p and q:";
cinpq;
while (!(test_prime(p)test_prime(q))){
cout"wrong input,please make sure two number are both primes!"endl;
cout"input two primes p and q:";
cinpq;
};
pr.n = p * q;
pu.n = p * q;
fn = (p - 1) * (q - 1);
cout"fn = "fnendl;
cout"input e :";
cine;
while((gcd(fn,e)!=1)) {
cout"e is error,try again!";
cout"input e :";
cine;
}
pr.d = (extend_euclid(fn,e) + fn) % fn;
pu.e = e;
flag = 1;
cout"PR.d: "pr.d" PR.n: "pr.nendl;
cout"PU.e: "pu.e" PU.n: "pu.nendl;
}
/* 加密 */
void encrypt() {
if(flag == 0) {
cout"setkey first:"endl;
produce_key();
}
cout"input m:";
cinm;
c = modular_multiplication(m,pu.e,pu.n);
cout"c is:"cendl;
}
/* 解密 */
void decrypt() {
if(flag == 0) {
cout"setkey first:"endl;
produce_key();
}
cout"input c:";
cinc;
m = modular_multiplication(c,pr.d,pr.n);
cout"m is:"mendl;
}
/* 版权信息 */
void about() {
cout"*********************************************"endl;
cout"*** by Terry ***"endl;
cout"*** copyright 2010,All rights reserved by ***"endl;
cout"*** Terry,technology supported by weizuo !***"endl;
cout"*** If you have any question, please mail ***"endl;
cout"*** to 18679376@qq.com ! ***"endl;
cout"*** Computer of science and engineering ***"endl;
cout"*** XiDian University 2010-4-29 ***"endl;
cout"*********************************************"endl;
coutendlendl;
Init();
}
/* 消息映射 */
MsgMap Messagemap[] = {
,
,
,
,
};
/* 主函数,提供循环 */
void main() {
Init();
char d;
while((d = getchar())!='q') {
int i = 0;
while(Messagemap[i].ch) {
if(Messagemap[i].ch == d) {
Messagemap[i].handler();
break;
}
i++;
}
}
}
本程序由520huiqin编写,详情见参考资料
智能化时代的到来涉及了各种核心算法,保护算法就能保障开发者权益,杜绝市面上各种山寨品,加密芯片恰好能起到很好的保护作用,如何选择加密芯片呢?KEROS加密芯片专注于加密领域十余年,行业首选。
1.安全性:采用国际通用aes256算法加密并同时通过KAS传送,除基本认证之外,利用2K安全EEPROM,用户可以自己管理密钥和数据,实现双重保护。
2.唯一性:以定制的方式为每一位用户单独定制“专属型号CID”,多用户之间算法不兼容,并且采用固化的方法直接将算法固化到晶圆上而无需烧入。
3.序列号:每颗芯片制造生产时具有5字节全球唯一SN序列号,每颗芯片SN都不会重复。
4.防抄特性:每颗芯片都有自己独特的密钥系统,破解单颗芯片只对这颗芯片对应的产品有效,对整个同类型的产品是无效的,依旧无法通过验证。而且KEROS采用ASIC方法设计,芯片内为纯逻辑电路,封装内有40多层逻辑电路整合了10万多个逻辑门,爆力刨片破解难度可想而知。
5.安全存储:用户可以将保密数据加密之后安全的存放到EEPROM中。非对称加密算法c语言的介绍就聊到这里吧,感谢你花时间阅读本站内容。
本文标签:非对称加密算法c语言