OPENSSL Base64编码和解码
2009-12-17 14:53
357 查看
BOOL Base64Encode(unsigned char *pData, int nLeng, int linebreaks, char * pOutBufffer, int *pBufferLenth)
{
int res = FALSE;
BIO *bmem, *b64;
BUF_MEM *bptr;
b64 = BIO_new(BIO_f_base64());
if (!b64) return res;
if (!linebreaks)
{
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
}
bmem = BIO_new(BIO_s_mem());
if (bmem) {
b64 = BIO_push(b64, bmem);
if (BIO_write(b64, pData, nLeng)==nLeng)
{
(void)BIO_flush(b64);
BIO_get_mem_ptr(b64, &bptr);
if (*pBufferLenth > bptr->length)
{
memcpy(pOutBufffer, bptr->data, bptr->length);
pOutBufffer[bptr->length] = 0;
res = TRUE;
}
*pBufferLenth = bptr->length + 1;
}
}
BIO_free_all(b64);
return res;
}
BOOL Base64Decode(char *pData, int nLeng, int linebreaks, unsigned char * pOutBufffer, int *pBufferLenth)
{
int res = FALSE;
BIO *bmem;
BIO *b64;
if (nLeng == 0)
nLeng = strlen(pData);
int nMaxLen=(nLeng*6+7)/8;
int nMiniLen;
unsigned char *buf = new unsigned char[nMaxLen];
if (buf)
{
b64 = BIO_new(BIO_f_base64());
if (b64)
{
if (!linebreaks)
{
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
}
bmem = BIO_new_mem_buf((char*)pData, nLeng);
b64 = BIO_push(b64, bmem);
nMiniLen = BIO_read(b64, buf, nMaxLen);
if(*pBufferLenth >= nMiniLen)
{
memcpy(pOutBufffer, buf, nMiniLen);
res = TRUE;
}
*pBufferLenth = nMiniLen;
BIO_free_all(b64);
}
delete []buf;
}
return res;
}
{
int res = FALSE;
BIO *bmem, *b64;
BUF_MEM *bptr;
b64 = BIO_new(BIO_f_base64());
if (!b64) return res;
if (!linebreaks)
{
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
}
bmem = BIO_new(BIO_s_mem());
if (bmem) {
b64 = BIO_push(b64, bmem);
if (BIO_write(b64, pData, nLeng)==nLeng)
{
(void)BIO_flush(b64);
BIO_get_mem_ptr(b64, &bptr);
if (*pBufferLenth > bptr->length)
{
memcpy(pOutBufffer, bptr->data, bptr->length);
pOutBufffer[bptr->length] = 0;
res = TRUE;
}
*pBufferLenth = bptr->length + 1;
}
}
BIO_free_all(b64);
return res;
}
BOOL Base64Decode(char *pData, int nLeng, int linebreaks, unsigned char * pOutBufffer, int *pBufferLenth)
{
int res = FALSE;
BIO *bmem;
BIO *b64;
if (nLeng == 0)
nLeng = strlen(pData);
int nMaxLen=(nLeng*6+7)/8;
int nMiniLen;
unsigned char *buf = new unsigned char[nMaxLen];
if (buf)
{
b64 = BIO_new(BIO_f_base64());
if (b64)
{
if (!linebreaks)
{
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
}
bmem = BIO_new_mem_buf((char*)pData, nLeng);
b64 = BIO_push(b64, bmem);
nMiniLen = BIO_read(b64, buf, nMaxLen);
if(*pBufferLenth >= nMiniLen)
{
memcpy(pOutBufffer, buf, nMiniLen);
res = TRUE;
}
*pBufferLenth = nMiniLen;
BIO_free_all(b64);
}
delete []buf;
}
return res;
}
相关文章推荐
- OpenSSL 常用函数——Base64编码及解码
- 嵌入式 OpenSSL 常用函数——Base64编码及解码和证书操作
- 利用openssl进行BASE64编码解码、md5/sha1摘要、AES/DES3加密解密
- 利用openssl进行BASE64编码解码、md5/sha1摘要、AES/DES3加密解密
- 利用openssl进行BASE64编码解码、md5/sha1摘要、AES/DES3加密解密
- 嵌入式 OpenSSL 常用函数——Base64编码及解码和证书操作
- 利用openssl进行base64的编码与解码
- 实用命令:利用openssl进行BASE64编码解码、md5/sha1摘要、AES/DES3加密解密
- OpenSSL Base64编码与解码
- 使用openssl进行base64编码 解码
- 实用命令:利用openssl进行BASE64编码解码、md5/sha1摘要、AES/DES3加密解密
- 利用OpenSSL进行BASE64的编码与解码
- 利用openssl进行base64的编码与解码
- openssl学习篇之base64编码、解码
- Openssl的Base64编码及解码
- linux之用openssl命令Base64编码解码、md5/sha1摘要、AES/DES3加密解密
- 实用命令:利用openssl进行BASE64编码解码、md5/sha1摘要、AES/DES3加密解密
- openssl学习篇之base64编码、解码;md5 摘要;sha1摘要;3des加密,解密;rsa算法
- 实用命令:利用openssl进行BASE64编码解码、md5/sha1摘要、AES/DES3加密解密
- 实用命令:利用openssl进行BASE64编码解码、md5/sha1摘要、AES/DES3加密解密