与OpenSSL的河豚简单的例子插入乱码援助 [英] Assistance with openssl blowfish simple example inserting garbage characters

查看:269
本文介绍了与OpenSSL的河豚简单的例子插入乱码援助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你有一个简单的加密使用OpenSSL的比这一个,我有问题,我用将非常感激更好的文件一个很好的例子。

更新: Myabe笔者是正确的。的东西我没有分配用memset提醒的strtok窒息非堆栈变量的我。

UPDATE2:得到了核心转储,停止通过使用malloc。更新了code。控制HS依然出现。更新code,以反映这一点。

UPDATE3:的循环结构不会出现在正确的范例。我不确定如何随后的读取存在的比最初读的大小。

UPDATE4:我想我找到了。解密循环有一个奥伦+ = TLEN和缓冲应该丢弃位集。 :(吧。

Update99988:
我在这里放弃了所有的希望。我想我需要扔掉这个例子,并使用从Openssl的书,而不是东西开始。解密后的中间层缓冲区是preappended与^ H的,但由于一个指针是我通过我开始怀疑对齐的问题。

我想我可能是在一个糟糕的范例开始是不是从头开始恶化的小船。我不得不做出一些修正(原来是下面的code注释)。原作者曾与传递地址不正确的一些问题。我的心正在由笔者使用1024和1032不同大小的缓冲区左右踢,但我认为这是关系到8位种子和链式加密电话。

我在(控制HS)获取垃圾字符和一个核心转储像解密破坏堆栈。我是相当新的加密,OpenSSL的,我不是所有熟悉GDB。

我已经尽一切努力简化下来


  1. --version GCC 4.3.2报告

  2. 开启SUSE 11

compile.sh

 的gcc -o -g河豚blowfish.c -lcrypto

run.sh

 的ulimit -c无限
./blowfish example.txt文件encrypted_example decrypted_example
回声差异example.txt文件decrypted_example
example.txt的差异decrypted_example

clean.sh

 室射频河豚encrypted_example decrypted_example核心

example.txt文件

  Lorem存有悲坐阿梅德,consectetur adipisicing ELIT,sed的做eiusmod tempor
incididunt UT labore等dolore麦格纳aliqua。 UT enim广告微量veniam,QUIS
nostrud实习ullamco laboris暂准UT aliquip前EA commodo consequat。
在重新prehenderit在voluptate velit埃塞cillum dolore欧盟DUIS奥特irure悲
fugiat法无pariatur。 Excepteur SINT occaecat cupidatat非proident,必须遵守在
过失魁正式开通了deserunt mollit阿尼姆ID EST laborum。氢ħ
氦^ h
锂大号
铍乙
硼乙
碳C
氮(N)
Ø氧
氟˚F
霓虹灯ñ
钠盐,N
镁中号
一个铝
硅小号
磷P
硫(S)
氯ç

钾K
钙ç
钪小号
钛ŧ
钒V
铬ç
锰的元素M
铁˚F
钴ç
镍ñ
铜ç
锌ž
镓摹
锗摹
一个砷
硒小号
溴乙
氪ķ
铷 - [R
锶小号
钇Y之
锆ž
铌ñ
钼中号
锝ŧ
钌 - [R
铑 - [R
钯P

镉ç
我铟
田小号
锑小号
碲ŧ
我碘
氙气点¯x
ç铯
钡乙
大号镧
ç铈
镨P
钕ñ
钷P
钐小号
Ë铕
钆摹
铽ŧ
ð镝
钬^ h
Ë铒
铥ŧ
镱ÿ
大号镥
铪^ h
钽ŧ
钨W¯¯
铼 - [R
Ø锇
我铱
铂金P
黄金
水星^ h
铊ŧ
导联P
铋乙
钋P
一个砹
氡ř
钫˚F
镭 - [R
一个锕
钍ŧ
镤P
ü铀
镎ñ
钚P
一个镅
锔ç
锫乙
锎ç
锿ê
镄˚F
钔中号
锘ñ
铹大号
Rutherfordiumř
Dubniumð
Seaborgium小号
Bohrium乙

If you have a good example of simply encrypting a file using openssl that is better than this one that I am having issues with I would be very grateful.

Update: Myabe the author was correct. Using memset on something I did not allocate reminds me of strtok choking on non-stack variables.

Update2: Got the core dump to stop by using malloc. Updated the code. The control Hs are still appearing. Updating the code to reflect this.

Update3: The looping structure does not appear correct in the example. I am unsure how subsequent reads are occuring greater than the size of the initial read.

Update4: I think I found it. decrypt loop has a olen += tlen and the buffer should discard that set of bits. :( not it.

Update99988: I have given up all hope here. I think I need to throw away this example and start with something from the Openssl book instead. Upon decrypting the middle layer buffers are preappended with ^H's but since a pointer is passed in I am beginning to suspect alignment issues.

I think I may be in the boat of starting with a bad example is worse than starting from scratch. i had to make some corrections (the original is annotated in the code below). The original author had some issues with passing addresses incorrectly. My mind is being kicked around by the different sized buffers the author is using 1024 and 1032 but I think it is related to the 8 bit seed and chained encryption calls.

I am getting garbage characters in (control Hs) and a core dump like the decryption is corrupting the stack. I am fairly new to encryption, openssl, and I am not all that familiar with gdb.

I have made every attempt to simplify this down

  1. gcc --version reports 4.3.2
  2. Open SUSE 11

compile.sh

gcc -g -o blowfish blowfish.c -lcrypto

run.sh

ulimit -c unlimited
./blowfish example.txt encrypted_example decrypted_example
echo diff example.txt decrypted_example
diff example.txt decrypted_example

clean.sh

rm -rf blowfish encrypted_example decrypted_example core

example.txt

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor 
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis 
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu 
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in 
culpa qui officia deserunt mollit anim id est laborum.

Hydrogen H
Helium H
Lithium L
Beryllium B
Boron B
Carbon C
Nitrogen N
Oxygen O
Fluorine F
Neon N
Sodium N
Magnesium M
Aluminium A
Silicon S
Phosphorus P
Sulfur S
Chlorine C
Argon A
Potassium K
Calcium C
Scandium S
Titanium T
Vanadium V
Chromium C
Manganese M
Iron F
Cobalt C
Nickel N
Copper C
Zinc Z
Gallium G
Germanium G
Arsenic A
Selenium S
Bromine B
Krypton K
Rubidium R
Strontium S
Yttrium Y
Zirconium Z
Niobium N
Molybdenum M
Technetium T
Ruthenium R
Rhodium R
Palladium P
Silver A
Cadmium C
Indium I
Tin S
Antimony S
Tellurium T
Iodine I
Xenon X
Caesium C
Barium B
Lanthanum L
Cerium C
Praseodymium P
Neodymium N
Promethium P
Samarium S
Europium E
Gadolinium G
Terbium T
Dysprosium D
Holmium H
Erbium E
Thulium T
Ytterbium Y
Lutetium L
Hafnium H
Tantalum T
Tungsten W
Rhenium R
Osmium O
Iridium I
Platinum P
Gold A
Mercury H
Thallium T
Lead P
Bismuth B
Polonium P
Astatine A
Radon R
Francium F
Radium R
Actinium A
Thorium T
Protactinium P
Uranium U
Neptunium N
Plutonium P
Americium A
Curium C
Berkelium B
Californium C
Einsteinium E
Fermium F
Mendelevium M
Nobelium N
Lawrencium L
Rutherfordium R
Dubnium D
Seaborgium S
Bohrium B
Hassium H
Meitnerium M
Darmstadtium D
Roentgenium R
Ununbium U
Ununtrium U
Ununquadium U
Ununpentium U
Ununhexium U
Ununseptium U
Ununoctium U

BAD CODE WARNING BAD CODE USE CODE FROM SELECTED ANSWER blowfish.c

/* 
This code is a modificaiton of work cieted below.  It 
was modified by ojblass June 16, 2009.

The original can be found at http://tldp.org/LDP/LG/issue87/vinayak.html

TERMS OF THE Open Publication License (OPL)

Copyright © 2003, Vinayak Hegde. Copying license 
http://www.linuxgazette.com/copying.html
Published in Issue 87 of Linux Gazette, February 2003

1. The modified version must be labeled as such.
2. The person making the modifications must be identified and the modifications 
   dated.
3. Acknowledgement of the original author and publisher if applicable must be 
   retained according to normal academic citation practices.
4. The location of the original unmodified document must be identified.
5. The original author's (or authors') name(s) may not be used to assert or 
   imply endorsement of the resulting document without the original author's (or
   authors') permission. 
*/

#include <openssl/blowfish.h>
#include <openssl/evp.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#define IP_SIZE 1024
#define OP_SIZE 1032
unsigned char key[16];
unsigned char iv[8];

int
generate_key ()
{
  int i, j, fd;
  if ((fd = open ("/dev/random", O_RDONLY)) == -1)
    perror ("open error");

  if ((read (fd, key, 16)) == -1)
    perror ("read key error");

  if ((read (fd, iv, 8)) == -1)
    perror ("read iv error");

  printf ("128 bit key:\n");
  for (i = 0; i < 16; i++)
    printf ("%4d ", key[i]);

  printf ("\nInitialization vector\n");
  for (i = 0; i < 8; i++)
    printf ("%4d ", iv[i]);
  printf ("\n");


  close (fd);
  return 0;
}

int
decrypt (int infd, int outfd)
{
  char *inbuff, *outbuf;
  int olen, tlen, n;
  EVP_CIPHER_CTX ctx;
  EVP_CIPHER_CTX_init (&ctx);
  EVP_DecryptInit (&ctx, EVP_bf_cbc (), key, iv);

  outbuf = (unsigned char *) malloc ( sizeof(unsigned char) * IP_SIZE );
  inbuff = (unsigned char *) malloc ( sizeof(unsigned char) * OP_SIZE );

  /* keep reading until a break */
  for (;;)
    {
      memset(inbuff,'\0', OP_SIZE);
      if ((n = read (infd, inbuff, OP_SIZE)) == -1)
    {
      perror ("read error");
      break;
    }
      else if (n == 0)
    break;

      memset(outbuf,'\0', IP_SIZE);

      if (EVP_DecryptUpdate (&ctx, outbuf, &olen, inbuff, n) != 1)
    {
      printf ("error in decrypt update\n");
      return 0;
    }

      if (EVP_DecryptFinal (&ctx, outbuf + olen, &tlen) != 1)
    {
      printf ("error in decrypt final\n");
      return 0;
    }
      olen += tlen;
      if ((n = write (outfd, outbuf, olen)) == -1)
    perror ("write error");
    }

  EVP_CIPHER_CTX_cleanup (&ctx);
  return 1;
}

int
encrypt (int infd, int outfd)
{
  char *inbuff, *outbuf;

  int olen, tlen, n;
  EVP_CIPHER_CTX ctx;
  EVP_CIPHER_CTX_init (&ctx);
  EVP_EncryptInit (&ctx, EVP_bf_cbc (), key, iv);

  outbuf = (unsigned char *) malloc ( sizeof(unsigned char) * OP_SIZE );
  inbuff = (unsigned char *) malloc ( sizeof(unsigned char) * IP_SIZE );

  for (;;)
    {
      memset(inbuff,'\0', IP_SIZE);

      if ((n = read (infd, inbuff, IP_SIZE)) == -1)
    {
      perror ("read error");
      break;
    }
      else if (n == 0)
    break;

      if (EVP_EncryptUpdate (&ctx, outbuf, &olen, inbuff, n) != 1)
    {
      printf ("error in encrypt update\n");
      return 0;
    }

      if (EVP_EncryptFinal (&ctx, outbuf + olen, &tlen) != 1)
    {
      printf ("error in encrypt final\n");
      return 0;
    }
      olen += tlen;
      if ((n = write (outfd, outbuf, olen)) == -1)
    perror ("write error");
    }
  EVP_CIPHER_CTX_cleanup (&ctx);
  return 1;
}

int
main (int argc, char *argv[])
{
  int flags1 = 0, flags2 = 0, outfd, infd, decfd;
  mode_t mode;
  char choice, temp;
  int done = 0, n, olen;

  memset(key,'\0', 16);
  memset(iv,'\0', 8);
  memset(&mode, '\0', sizeof(mode));

  flags1 = flags1 | O_RDONLY;
  flags2 = flags2 | O_RDONLY;
  flags2 = flags2 | O_WRONLY;
  flags2 = flags2 | O_CREAT;

  mode = mode | S_IRUSR;
  mode = mode | S_IWUSR;

  generate_key ();

  if ((infd = open (argv[1], flags1, mode)) == -1)
    perror ("open input file error");

  if ((outfd = open (argv[2], flags2, mode)) == -1)
    perror ("open output file error");

  encrypt (infd, outfd);

  close (infd);
  close (outfd);

  if ((outfd = open (argv[2], flags1, mode)) == -1)
    perror ("open output file error");

  if ((decfd = open (argv[3], flags2, mode)) == -1)
    perror ("open output file error");

  /* After much head scratching reusing the out as an in is correct here */
  decrypt (outfd, decfd);

  close (outfd);
  fsync (decfd);
  close (decfd);

  return 0;
}

解决方案

The error is in the way the EVP_DecryptFinal and EVP_EncryptFinal are called. These functions should be called in the end of the for cycle, also the final part where to olen is added tlen and written again was duplicating output. Bellow is the final working version:

/*
 * This code is a modificaiton of work cieted below.  It was modified by
 * ojblass June 16, 2009.
 * 
 * The original can be found at http://tldp.org/LDP/LG/issue87/vinayak.html
 * 
 * TERMS OF THE Open Publication License (OPL)
 * 
 * Copyright © 2003, Vinayak Hegde. Copying license
 * http://www.linuxgazette.com/copying.html Published in Issue 87 of Linux
 * Gazette, February 2003
 * 
 * 1. The modified version must be labeled as such. 2. The person making the
 * modifications must be identified and the modifications dated. 3.
 * Acknowledgement of the original author and publisher if applicable must be
 * retained according to normal academic citation practices. 4. The location
 * of the original unmodified document must be identified. 5. The original
 * author's (or authors') name(s) may not be used to assert or imply
 * endorsement of the resulting document without the original author's (or
 * authors') permission.
 */

#include <openssl/blowfish.h>
#include <openssl/evp.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#define IP_SIZE 1024
#define OP_SIZE 1024 + EVP_MAX_BLOCK_LENGTH
unsigned char   key[16];
unsigned char   iv[8];

int
generate_key()
{
	int             i, fd;
	if ((fd = open("/dev/random", O_RDONLY)) == -1)
		perror("open error");

	if ((read(fd, key, 16)) == -1)
		perror("read key error");

	if ((read(fd, iv, 8)) == -1)
		perror("read iv error");

	printf("128 bit key:\n");
	for (i = 0; i < 16; i++)
		printf("%4d ", key[i]);

	printf("\nInitialization vector\n");
	for (i = 0; i < 8; i++)
		printf("%4d ", iv[i]);
	printf("\n");


	close(fd);
	return 0;
}

int
do_decrypt(int infd, int outfd)
{
	unsigned char           *inbuff, *outbuf;
	int             olen=0, tlen=0, n=0;
	EVP_CIPHER_CTX  ctx;
	EVP_CIPHER_CTX_init(&ctx);
	EVP_DecryptInit(&ctx, EVP_bf_cbc(), key, iv);

	outbuf = (unsigned char *) malloc(sizeof(unsigned char) * OP_SIZE);
	inbuff = (unsigned char *) malloc(sizeof(unsigned char) * IP_SIZE);

	/* keep reading until a break */
	for (;;) {
		memset(inbuff, 0, IP_SIZE);
		if ((n = read(infd, inbuff, IP_SIZE)) == -1) {
			perror("read error");
			break;
		} else if (n == 0)
			break;

		memset(outbuf, 0, OP_SIZE);

		if (EVP_DecryptUpdate(&ctx, outbuf, &olen, inbuff, n) != 1) {
			printf("error in decrypt update\n");
			return 0;
		}
		if ((n = write(outfd, outbuf, olen)) == -1)
			perror("write error");
	}

	tlen=0;
	if (EVP_DecryptFinal(&ctx, outbuf + olen, &tlen) != 1) {
		perror("error in decrypt final");
		return 0;
	}

	if ((n = write(outfd, outbuf+olen, tlen)) == -1)
		perror("write error");

	EVP_CIPHER_CTX_cleanup(&ctx);
	return 1;
}

int
do_encrypt(int infd, int outfd)
{
	unsigned char           *inbuff, *outbuf;

	int             olen=0, tlen=0, n=0;
	EVP_CIPHER_CTX  ctx;
	EVP_CIPHER_CTX_init(&ctx);
	EVP_EncryptInit(&ctx, EVP_bf_cbc(), key, iv);

	outbuf = (unsigned char *) malloc(sizeof(unsigned char) * OP_SIZE);
	inbuff = (unsigned char *) malloc(sizeof(unsigned char) * IP_SIZE);

	for (;;) {
		memset(inbuff, 0, IP_SIZE);

		if ((n = read(infd, inbuff, IP_SIZE)) == -1) {
			perror("read error");
			break;
		} else if (n == 0)
			break;

		if (EVP_EncryptUpdate(&ctx, outbuf, &olen, inbuff, n) != 1) {
			printf("error in encrypt update\n");
			return 0;
		}

		if ((n = write(outfd, outbuf, olen)) == -1)
			perror("write error");
	}
	tlen=0;
	if (EVP_EncryptFinal(&ctx, outbuf + olen, &tlen) != 1) {
		printf("error in encrypt final\n");
		return 0;
	}

	if ((n = write(outfd, outbuf+olen, tlen)) == -1)
		perror("write error");

	EVP_CIPHER_CTX_cleanup(&ctx);
	return 1;
}

int
main(int argc, char *argv[])
{
	int             flags1 = 0, flags2 = 0, outfd, infd;
	mode_t          mode;

	memset(key, 0, 16);
	memset(iv, 0, 8);
	memset(&mode, 0, sizeof(mode));

	flags1 = flags1 | O_RDONLY;
	flags2 = flags2 | O_RDONLY;
	flags2 = flags2 | O_WRONLY;
	flags2 = flags2 | O_CREAT;

	mode = mode | S_IRUSR;
	mode = mode | S_IWUSR;


	generate_key();


	if ((infd = open(argv[1], flags1, mode)) == -1)
		perror("open input file error");

	if ((outfd = open(argv[2], flags2, mode)) == -1)
		perror("open output file error");

	do_encrypt(infd, outfd);

	close(infd);
	fsync(outfd);
	close(outfd);

	if ((infd = open(argv[2], flags1, mode)) == -1)
		perror("open output file error");

	if ((outfd = open(argv[3], flags2, mode)) == -1)
		perror("open output file error");

	do_decrypt(infd, outfd);

	close(infd);
	fsync(infd);
	close(outfd);

	return 0;
}

这篇关于与OpenSSL的河豚简单的例子插入乱码援助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆