Hello,
If you are trying to encrypt a message using your program below and decrypt
it
using the OpenSSL command line tool, you will need to use the -nosalt option
since
you have NULLed the salt in:
EVP_BytesToKey(ciph,EVP_md5(),NULL,(unsigned
char*)passwd,strlen(passwd),1,(unsigned char *)key,(unsigned char *)iv);
If you are wanting to keep the salt, you will need to generate one in your
program
and save it to your encrypted file. Otherwise, you will need the -nosalt
option.
Take a look at the enc.c code under the apps subdirectory in your OpenSSL
directory.
The code generates a salt using the RAND_pseudo_bytes function and saves it
to the
beginning of the file as Salted__12345678 where 12345678 is a generated salt
from RAND_pseudo_bytes.
Your code would look something like this:
OpenSSL_add_all_algorithms();
char key[EVP_MAX_KEY_LENGTH];
char iv[EVP_MAX_IV_LENGTH];
EVP_CIPHER_CTX ctx;
unsigned char out[512+8];
int outl;
char passwd[] = "password";
EVP_CIPHER *ciph;
int i,len;
unsigned char buff[1024];
unsigned char c;
unsigned char salt[PKCS5_SALT_LEN];
CFile fileIn;
fileIn.Open("A:\\test.txt", CFile::typeBinary | CFile::modeRead, NULL);
CFile fileOut;
fileOut.Open("A:\\test.enc", CFile::typeBinary | CFile::modeCreate |
CFile::modeWrite, NULL);
RAND_pseudo_bytes(salt,PKCS5_SALT_LEN);
fileOut.write("Salted__",strlen("Salted__"));
fileOut.write(salt,PKCS5_SALT_LEN);
ciph = EVP_bf_cbc();
EVP_BytesToKey(ciph,EVP_md5(),salt,(unsigned char
*)passwd,strlen(passwd),1,(unsigned char *)key,(unsigned char *)iv);
cout << "begin init:" << endl;
EVP_CipherInit(&ctx, ciph, (unsigned char *)key, (unsigned char *)iv,1);
cout << "begin update:" << endl;
i=1;
while (i != 0){
len=0;
while(i!=0){
i=fileIn.Read(&c,1);
buff[len]=c;
len++;
if(len>(512+8)) break;
}
EVP_CipherUpdate(&ctx, out, &outl, (unsigned char *)buff, len);
fileOut.Write(&out, outl);
}
EVP_CipherFinal(&ctx, out, &outl);
fileOut.Write(&out, outl);
fileIn.Close();
fileOut.Close();
Hope this helps!
Edward Seabolt
-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]]On Behalf Of Lee Melville
Sent: Friday, September 29, 2000 2:52 PM
To: [EMAIL PROTECTED]
Subject: more enc probs
Hi,
Here's my problem, the following code encrypts a file (i think it does
anyhow), the test file that i use starts off as 22 bytes, the encrypted
version is 24 ( i am not sure this is relevent). Anyhow the problem is when
i come to decrypt the file using the openssl command line tool it complains
about a bad magic number. What is this? My code is the problem
(obviously), but i dont know where this magic number fits in and hence i
havn't got a clue as to what to do. Plz Help.
Thanks.
Lee Melville
heres the code;-
//--------------------------------------------------------------------------
-----
OpenSSL_add_all_algorithms();
char key[EVP_MAX_KEY_LENGTH];
char iv[EVP_MAX_IV_LENGTH];
EVP_CIPHER_CTX ctx;
unsigned char out[512+8];
int outl;
char passwd[] = "password";
EVP_CIPHER *ciph;
int i,len;
unsigned char buff[1024];
unsigned char c;
CFile fileIn;
fileIn.Open("A:\\test.txt", CFile::typeBinary | CFile::modeRead, NULL);
CFile fileOut;
fileOut.Open("A:\\test.enc", CFile::typeBinary | CFile::modeCreate |
CFile::modeWrite, NULL);
ciph = EVP_bf_cbc();
EVP_BytesToKey(ciph,EVP_md5(),NULL,(unsigned char
*)passwd,strlen(passwd),1,(unsigned char *)key,(unsigned char *)iv);
cout << "begin init:" << endl;
EVP_CipherInit(&ctx, ciph, (unsigned char *)key, (unsigned char *)iv,1);
cout << "begin update:" << endl;
i=1;
while (i != 0){
len=0;
while(i!=0){
i=fileIn.Read(&c,1);
buff[len]=c;
len++;
if(len>(512+8)) break;
}
EVP_CipherUpdate(&ctx, out, &outl, (unsigned char *)buff, len);
fileOut.Write(&out, outl);
}
EVP_CipherFinal(&ctx, out, &outl);
fileOut.Write(&out, outl);
fileIn.Close();
fileOut.Close();
______________________________________________________________________
OpenSSL Project http://www.openssl.org
User Support Mailing List [EMAIL PROTECTED]
Automated List Manager [EMAIL PROTECTED]
______________________________________________________________________
OpenSSL Project http://www.openssl.org
User Support Mailing List [EMAIL PROTECTED]
Automated List Manager [EMAIL PROTECTED]