关于使用openssl库的问题……
对openssl算法库初接触,在VC2010下编译静态LIB成功,可不会使用,特来向众高手请教两个问题:
一:如何使用SHA512算法加密信息?
见sha.h中有如下定义,但不知各参数意义,
int SHA512_Init(SHA512_CTX *c);
int SHA512_Update(SHA512_CTX *c, const void *data, size_t len);
int SHA512_Final(unsigned char *md, SHA512_CTX *c);
unsigned char *SHA512(const unsigned char *d, size_t n,unsigned char *md);
void SHA512_Transform(SHA512_CTX *c, const unsigned char *data);
二:对于使用AES或DES算法生成的密文,如何用openssl内带的方法格式化输出为十六进制编码,类似于以下Crypto中的函数,
bool HexEncodeFile(const char *inFilename, const char *outFilename);
bool HexDecodeFile(const char *inFilename, const char *outFilename);
bool HexEncodeString(const char *plain, char** outEncoded);
bool HexDecodeString(const char *encoded, char** outPlain);
[解决办法]
看源代码
[解决办法]
void enBase64(char* &pReturn,const unsigned char *inputBuffer, int inputLen,int* outLen)
{
EVP_ENCODE_CTX code_ctx;
int base64Len = (((inputLen+2)/3)*4) + 1; // Base64 text length
int pemLen = base64Len + base64Len/64; // PEM adds a newline every 64 bytes
pReturn = new char[pemLen];
int result,tmpLen;
EVP_EncodeInit(&code_ctx);
EVP_EncodeUpdate(&code_ctx, (unsigned char *)pReturn, &result, (unsigned char *)inputBuffer, inputLen);
EVP_EncodeFinal(&code_ctx, (unsigned char *)&pReturn[result], &tmpLen);
result += tmpLen;
*outLen = result;
}
void unBase64(char* &pReturn,const char *inputBuffer, int length,int* outLen)
{
EVP_ENCODE_CTX code_ctx;
int orgLen = (((length+2)/4)*3) + 1;
pReturn = new char[orgLen];
int result, tmpLen;
EVP_DecodeInit(&code_ctx);
EVP_DecodeUpdate(&code_ctx, (unsigned char *)pReturn, &result, (unsigned char *)inputBuffer, length);
EVP_DecodeFinal(&code_ctx, (unsigned char *)&pReturn[result], &tmpLen);
result += tmpLen;
*outLen = result;
}