OpenSSL对称加密算法中如何添加新算法
http://tech.ddvip.com 2007年04月21日 社区交流
本文详细介绍OpenSSL对称加密算法中如何添加新算法
四种加密模式与基本加解密算法之间的关系!
假设加密算法和解密算法的实现函数接口如下
void encrypt(const unsigned char *in, unsigned char *out, const KEY *key, int *length)
void decrypt(const unsigned char *in, unsigned char *out, const KEY *key, int *length)
void ecb_ encrypt(const unsigned char *in, unsigned char *out,
long length, const KEY *key, unsigned char *iv, int enc)
{/* 电子密码本: Electronic Code Book */
register int i;
int len = 8;
register long l = length;
unsigned char buf[8];
if(enc)/*encryption*/
{
for(i=0;i<=l;i+=8)
{
encrypt(&in[i], &out[i], key, &len);/*len == 8 will always be true here*/
}
else
{
for(i=0;i<=l;i+=8)
{
decrypt(&in[i], &out[i], key, &len);/*len == 8 will always be true here*/
}
}
}
void cbc_encrypt(const unsigned char *in, unsigned char *out,
long length, const KEY *key, unsigned char *iv, int enc)
{/* 密钥分组连接模式 */
register int i;
int len = 8;
register long l = length;
unsigned char buf[8];
if(enc)/*encryption*/
{
for(l-=8; l>=0; l-=8, in+=8, out+=8)
{
for(i=0; i<8;>
buf[i] = in[i] ^ iv[i];
encrypt(buf, iv, key, &len);/*len == 8 will always be true here*/
for(i=0; i<8;>
out[i] = iv[i];
}
/*final block*/
if(l != -8)
{
for(i=0; i
buf[i] = in[i] ^ iv[i];
for(; i<8;>
buf[i] = iv[i];
encrypt(buf, iv, key, &len);/*len == 8 here*/
for(i=0; i<8;>
out[i] = iv[i];
}
/* 加密输出为做下一次得iv ,iv与in异或运算的结果作为加密输入*/
}
else/*decryption*/
{
for(l-=8; l>=0; l-=8, in+=8, out +=8)
{
decrypt(in, buf, key, &len);
for(i=0; i<8;>
out[i] = buf[i] ^ iv[i];
for(i=0; i<8;>
iv[i] = in[i];
}
/*final block*/
if(l != -8)
{
decrypt(in, buf, key, &len);
for(i=0; i
out[i] = buf[i] ^ iv[i];
for(i=0; i<8;>
iv[i] = in[i];
}
}
l = 0;
i = 0;
}
void cfb64_encrypt(const unsigned char *in, unsigned char *out,
long length, const KEY *key, unsigned char *iv, int *num, int enc)
{/* 密码反馈模式 */
register long l = length;
unsigned char buf[8];
register int i, save = 0, n = *num;/*start from previously saved processing position*/
int len = 8;
/*restore from previously saved iv*/
for(i=n; i<8;>
buf[i] = iv[i];
if(enc)
{
while(l--)
{
if(n == 0)
{
encrypt(iv, buf, key, &len);
save = 1;
}
*(out++) = iv[n] = *(in++) ^ buf[n];
n = (n+1)&0x07;
}
}
else
{
while(l--)
{
if(n == 0)
{
encrypt(iv, buf, key, &len);
save = 1;
}
*(out++) = (iv[n]=*(in++)) ^ buf[n];
n = (n+1)&0x07;
}
}
if(save)/*store encrypted data into iv for next encryption*/
for(i=n; i<8;>
iv[i] = buf[i];
/* cfb加密输出得结果作为下次得IV, in与加密IV的结果作异或运算的结果作为cfb加密的输出 */
*num = n;/*store current processing position as entry of next encryption*/
save = i = n = 0;
}
void ofb64_encrypt(const unsigned char *in, unsigned char *out,
long length, const KEY *key, unsigned char *iv, int *num)
{/* 输出反馈模式 */
register long l = length;
register int i, n = *num;/*start from previously saved processing position*/
int len = 8;
unsigned char buf[8];
/*restore from previously saved iv*/
if(n != 0)
for(i=n; i<8;>
buf[i] = iv[i];
while(l--)
{
if(n == 0)
{
encrypt(iv, buf, key, &len);
for(i=0; i<8;>
iv[i] = buf[i];
}
*(out++) = *(in++) ^ buf[n];
n = (n+1)&0x07; /* n=(n+1)%0x08*/
/* iv加密输出结果作魏下一次iv, iv与in异或运算的结果作为ofb加密输出 */
}
*num = n;/*store current processing position as entry of next encryption*/
i = n = 0;
}三:如何在SSL协议中添加新的加密算法!
作者:赵治国 责编:豆豆技术应用
正在加载评论...
- Linux/Unix 新闻
- Linux/Unix 入门
- Linux/Unix 命令
- Linux/Unix 安装
- Linux 嵌入式系统
- Linux/Unix 编程
- Linux/Unix 管理
- Linux/Unix 桌面
- Linux/Unix 内核
- Linux/Unix 软件
- SCO Unix
- NetBSD
- OpenBSD
- Redhat/Fedora Linux
- 手机
- Linux/Unix find 搜索命令
- Linux/Unix vi 命令
- Linux/Unix kde 桌面环境
- Linux/Unix GNOME 桌面环境
- Linux/Unix Make 命令
- Linux/Unix crontab 命令
- Linux/Unix ext3 文件系统
- Linux/Unix 文件系统详解
- Linux/Unix ADSL 拨号设置
- Linux/Unix GRUB 配置及应用
- Linux/Unix nfs配置
- Linux/Unix 硬件信息查看及管理
- Linux/Unix 优化
- Linux/Unix 交换分区Swap管理及应用
- Linux/Unix 用户管理
- Linux/Unix Ramdisk
- Linux/Unix 密码恢复管理
- Linux/Unix 文件删除恢复
- Linux/Unix fdisk分区
- Linux/Unix lvs负载均衡管理
- Linux/Unix root用户
- Linux/Unix 集群
- Linux/Unix 日志
- 更多Linux/Unix专题……