[완료]openssl base65 인코딩 디코딩 사용방법

anaud2의 이미지

안녕하세요 어제 질문을 올렸다가 다시 수정해서 올립니다.
아래의 예를 보면 openssl에 홈페이지에 올려져 있는 예제를 화면으로 출력되고 읽어지는것을 파일로
base64인코딩해서 쓰고 다시 파일로 읽어서 base64디코딩해서 파일로 쓰는 구조로 바꿨습니다.
그런데 문제는 디코딩을 해보면 출력되는 파일 base64de.txt를 보면 "Hello World■hello world"로 되어있습니다.
울트라 에디터나 그런걸로 보면 OD OA 로 줄바꿈"\n"이 맞고 또 그걸 복사해서 붙여 넣기 해도
줄바꿈으로 됩니다. 그런데 출력단에서는 까만 네모로 뜨네요

이걸 물어보는 이유는 암호화+원본헤쉬 를 base64인코딩후 base64디코딩 복호화->해쉬후 원본헤쉬와 대조에서 fail이 떨어집니다. 해쉬값이 틀린거져 base64 인코딩과 디코딩 전에는 올바르게 동작했었습니다.

제생각에는 저 까만네모가 변환이 안된게 해쉬값이 틀리게 하는 요인이 아닐까 해서 질문드렸습니다.

int main(void){

BIO *bio, *b64, *bio_out;
char message[] = "Hello World \nhello world";
char message2[30];
char inbuf[512];
int inlen;
FILE *f1,*f2;

if((f1 = fopen("base64en.txt","wb")) ==NULL){
printf("file open error\n");
return 0;
}

b64 = BIO_new(BIO_f_base64());
bio = BIO_new_fp(f1, BIO_NOCLOSE);
bio = BIO_push(b64, bio);
BIO_write(bio, message, strlen(message));
BIO_flush(bio);

BIO_free_all(bio);

fclose(f1);
if((f1 = fopen("base64en.txt","rb")) ==NULL){
printf("file open error\n");
return 0;
}
if((f2 = fopen("base64de.txt","wb")) ==NULL){
printf("file open error\n");
return 0;
}

b64 = BIO_new(BIO_f_base64());
bio = BIO_new_fp(f1, BIO_NOCLOSE);
bio_out = BIO_new_fp(f2, BIO_NOCLOSE);
bio = BIO_push(b64, bio);
while((inlen = BIO_read(bio, inbuf, 512)) > 0){
printf("ibuf[%s]\n",inbuf);
BIO_write(bio_out, inbuf, inlen);
}
BIO_free_all(bio);

return 0;
}
예제상으로 보면 BIO_read를 하면 base64디코딩이 되고 BIO_write를 하면 인코딩이 되는거 같아요
예제상에서는 파일로만 떨어트리게 되는데 "BIO *BIO_new_fp(FILE *stream, int flags);" 이렇게 선언이 되어 있어서 파일로만 떨어트리는 방법밖에 모르겠어요

base64를 인코딩 디코딩을 메모리 버퍼로( char[])로 넣을수 있으면 더 편하게 할거 같은데
아시는분 계시면 답변좀 부탁드립니다.

bushi의 이미지

아무런 이상도 없습니다.

결과는 다음과 같습니다.

[bushi@rose net]$ 
[bushi@rose net]$ gcc -s -o test test.c -lssl -Wall
test.c: In function ‘main’:
test.c:24: warning: value computed is not used
test.c:10: warning: unused variable ‘message2’
[bushi@rose net]$ 
[bushi@rose net]$ ./test
ibuf[Hello World 
hello world]
[bushi@rose net]$ 
[bushi@rose net]$ cat base64en.txt 
SGVsbG8gV29ybGQgCmhlbGxvIHdvcmxk
[bushi@rose net]$ 
[bushi@rose net]$ cat base64de.txt 
Hello World 
hello world[bushi@rose net]$ 
[bushi@rose net]$ 
[bushi@rose net]$ cat base64en.txt 
SGVsbG8gV29ybGQgCmhlbGxvIHdvcmxk
[bushi@rose net]$ 
[bushi@rose net]$ 
[bushi@rose net]$ base64 -d base64en.txt 
Hello World 
hello world[bushi@rose net]$ 
[bushi@rose net]$ 
[bushi@rose net]$ base64 base64de.txt 
SGVsbG8gV29ybGQgCmhlbGxvIHdvcmxk
[bushi@rose net]$

OTL

anaud2의 이미지

많은 도움이 됐습니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.