#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "taiji.h"
#include "swsds.h"
#define RSA_PKCS1_PADDING_SIZE 11
unsigned int RSA_padding_add_PKCS1_type_1_enc(unsigned char *to, int tlen,
const unsigned char *from, int flen)
{
int j;
unsigned char *p;
if (flen > (tlen-RSA_PKCS1_PADDING_SIZE))
{
//RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
return 1;
}
p=(unsigned char *)to;
*p ++ = 0;
*p ++ = 2; /* Private Key BT (Block Type) */
/* pad out with 0xff data */
j=tlen-3-flen;
/*memset(p,0xff,j);*/
unsigned char * random = (unsigned char *)malloc(j+1);
srand( (unsigned)time( NULL ) );
for(int i = 0; i < j;i++ )
random[i] = rand()%100 + 1;
memcpy(p,random,j);
free(random);
p+=j;
* p++ = '\0';
memcpy(p,from,(unsigned int)flen);
return 0;
}
unsigned int RSA_padding_del_PKCS1_type_1_enc(unsigned char *to, unsigned int *tlen,
const unsigned char *from, int flen)
{
int i,j;
const unsigned char *p;
p=from;
if ((* p++ != 00) || (* p++ != 02))
{
//RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_BLOCK_TYPE_IS_NOT_01);
return 1;
}
/* scan over padding data */
j=flen-1; /* one for type. */
for (i=0; i<j; i++)
{
if (*p != 0x00) /* should decrypt to 0xff */
{
p ++;
//if (*p == 0)
//{ p++; break; }
//else {
// //RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_BAD_FIXED_HEADER_DECRYPT);
// return(-1);
//}
}
else
{
p++;
break;
}
}
//if (i == j)
//{
// //RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_NULL_BEFORE_BLOCK_MISSING);
// return(-1);
//}
if (i < 8)
{
//RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_BAD_PAD_BYTE_COUNT);
return 1;
}
i++; /* Skip over the '\0' */
j-=i;
memcpy(to,p,(unsigned int)j);
*tlen = j;
return 0;
}
unsigned int RSA_padding_add_PKCS1_type_1_sign(unsigned char *to, int tlen,const unsigned char *from, int flen)
{
/*if((flen != 16) && (flen != 20))
{
return HASH_LENGTH_ERROR;
}*/
if (flen != 16)
{
if (flen != 20)
{
return 1;
}
}
unsigned int i ,j;
unsigned char * p = to;
*p = 0x00;
p ++;
*p = 0x01;
p ++;
unsigned char T_MD5_pre[] = {0x30,0x20,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x04,0x10};
unsigned char T_SHA1_pre[]= {0x30,0x21,0x30,0x09,0x06,0x05,0x2b,0x0e,0x03,0x02,0x1a,0x05,0x00,0x04,0x14};
//Hash 用MD5的情况
if (flen == 16)
{
j = tlen - 3 - 18 - 16;
memset(p,0xff,j);
p += j;
*p = 0x00;
p ++;
memcpy(p,T_MD5_pre,18);
p += 18;
memcpy(p,from,flen);
}
if (flen == 20)
{
j = tlen - 3 - 15 - 20;
memset(p,0xff,j);
p += j;
*p = 0x00;
p ++;
memcpy(p,T_SHA1_pre,15);
p += 15;
memcpy(p,from,flen);
}
return 0;
}
unsigned int RSA_padding_del_PKCS1_type_1_sign(unsigned char *to, unsigned int tlen, const unsigned char *from, int flen)
{
int i , j;
unsigned char * p = (unsigned char *)from;
if ((*p++ != 0x00) || (*p++ != 01))
{
return SDR_PKOPERR;
}
for (i = 0;i < flen ;i ++)
{
if (*p != 0xff)
{
if (*p != 0x00)
{
return SDR_PKOPERR;
}
else
{
p ++;
break;
}
}
else
{
p ++;
}
}
if (tlen == 16)
{
p += 18;
memcpy(to,p,16);
}
if (tlen == 20)
{
p += 15;
memcpy(to,p,20);
}
return 0;
}
unsigned int check_zero(unsigned char * array,unsigned int count)
{
int i ;
for (i = 0; i < count; i ++)
{
if (array[i] == 0)
{
break;
}
}
if (i == count)
return 0;
else
return 1;
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
util.rar (1个子文件)
util.cpp 4KB
共 1 条
- 1
j6j6j6x6x6x6
- 粉丝: 1
- 资源: 16
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页