Linux下用管道执行base64命令加密解密字符串
2016-12-29 16:42
190 查看
Linux下用管道执行base64命令加密解密字符串
直接上实例代码,用base64加密解密字符串。
test.c 文件:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#define MAXL_BASE64CODE 1024
int base64_encode(const char* str, char* out)
{
int n=0;
FILE* fp=NULL;
char buf[256];
sprintf(buf, "echo %s | base64", str);
if(NULL == (fp=popen(buf,"r")))
{
fprintf(stderr, "execute command failed: %s", strerror(errno));
return -1;
}
if(NULL == fgets(out, MAXL_BASE64CODE, fp))
{
pclose(fp);
return -1;
}
n = strlen(out);
if('\n' == out[n-1])
out[n-1] = 0; // 去掉base64命令输出的换行符
pclose(fp);
return 0;
}
int base64_decode(const char* str, char* out)
{
int n=0;
FILE* fp=NULL;
char buf[256];
sprintf(buf, "echo %s | base64 -d", str);
if(NULL == (fp=popen(buf, "r")))
{
fprintf(stderr, "execute command failed: %s", strerror(errno));
return -1;
}
if(NULL == fgets(out, MAXL_BASE64CODE, fp))
{
pclose(fp);
return -1;
}
n = strlen(out);
if('\n' == out[n-1])
out[n-1] = 0; // 去掉base64命令输出的换行符
pclose(fp);
return 0;
}
int main()
{
char* str="Hello World!";
char buf1[MAXL_BASE64CODE];
char buf2[MAXL_BASE64CODE];
printf("%s\n", str);
base64_encode(str, buf1); // 加密
printf("%s\n", buf1);
base64_decode(buf1, buf2); // 解密
printf("%s\n", buf2);
return 0;
}
执行结果:
$ ./test
Hello World!
SGVsbG8gV29ybGQhCg==
Hello World!
直接上实例代码,用base64加密解密字符串。
test.c 文件:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#define MAXL_BASE64CODE 1024
int base64_encode(const char* str, char* out)
{
int n=0;
FILE* fp=NULL;
char buf[256];
sprintf(buf, "echo %s | base64", str);
if(NULL == (fp=popen(buf,"r")))
{
fprintf(stderr, "execute command failed: %s", strerror(errno));
return -1;
}
if(NULL == fgets(out, MAXL_BASE64CODE, fp))
{
pclose(fp);
return -1;
}
n = strlen(out);
if('\n' == out[n-1])
out[n-1] = 0; // 去掉base64命令输出的换行符
pclose(fp);
return 0;
}
int base64_decode(const char* str, char* out)
{
int n=0;
FILE* fp=NULL;
char buf[256];
sprintf(buf, "echo %s | base64 -d", str);
if(NULL == (fp=popen(buf, "r")))
{
fprintf(stderr, "execute command failed: %s", strerror(errno));
return -1;
}
if(NULL == fgets(out, MAXL_BASE64CODE, fp))
{
pclose(fp);
return -1;
}
n = strlen(out);
if('\n' == out[n-1])
out[n-1] = 0; // 去掉base64命令输出的换行符
pclose(fp);
return 0;
}
int main()
{
char* str="Hello World!";
char buf1[MAXL_BASE64CODE];
char buf2[MAXL_BASE64CODE];
printf("%s\n", str);
base64_encode(str, buf1); // 加密
printf("%s\n", buf1);
base64_decode(buf1, buf2); // 解密
printf("%s\n", buf2);
return 0;
}
执行结果:
$ ./test
Hello World!
SGVsbG8gV29ybGQhCg==
Hello World!
相关文章推荐
- linux使用管道命令执行ps获取cpu与内存占用率
- linux多命令顺序执行与管道符-54
- linux之用openssl命令Base64编码解码、md5/sha1摘要、AES/DES3加密解密
- linux常用命令(六)命令执行顺序控制与管道
- linux 命令系列之 多命令顺序执行和管道符(44)
- Linux命令执行顺序与管道命令
- Linux基础 -- 命令执行顺序控制与管道
- 7、linux命令执行顺序控制与管道
- Linux:命令执行顺序控制与管道
- 实验楼Linux学习笔记(九)之 命令执行顺序控制与管道
- Linux的多命令执行与管道符
- java利用Apache commons codec进行MD5加密,BASE64加密解密,执行系统命令
- java使用Runtime.exec执行linux命令重定向或管道问题
- Linux命令执行顺序控制与管道
- Linux学习笔记--命令的顺序执行、管道符、通配符、其他特殊符号
- 【Linux】命令执行顺序控制与管道
- Linux学习日记 —— 10.3.4 Shell基础-Bash基本功能-多命令顺序执行与管道符
- Linux学习10:命令执行顺序控制与管道
- Linux命令执行顺序控制与管道
- linux c之通过popen和pclose函数创建管道执行shell 运行命令使用总结