Я пишу программу для реализации rc4 и пытаюсь записать соль в файл, а затем вернуть его и использовать для расшифровки. Тем не менее, нормальное шифрование / дешифрование работает нормально.
РЕДАКТИРОВАТЬ: запись в файл работает, но случайная часть продолжает давать мне разную длину каждый раз, когда это первая проблема.
Second и что я действительно изо всех сил пытается получить вторые 8 байтов из начала файла и сохранить их в неподписанном символе, чтобы я мог передать его как генератор ha sh.
/*******************************************ENCRYPTION*************************************************/
if(strcmp(argv[1],"-e") == 0){
unsigned char salt[8];// salt value buffer
RAND_bytes(salt,sizeof(salt));// randomize a salt value
string pass = "Salted__";// string salted
unsigned char hash[16];// hash value buffer
ifstream in(argv[2]);// open the first file
ofstream out;
string data = slurp(in);// save all the data into a string
out.open(argv[3],ios::binary);// open second value in binary
int datalen = data.size();// get the size of the file
unsigned char *buf = (unsigned char*)malloc(datalen + 17);// allocate a block in memory of datalength + 17 bytes
memset(buf, 0, datalen + 17);//fills memory with blank placeholder = 0
RC4_KEY key;// initialize rc4 key
//check for salt value
if(strcmp(argv[5],"-nosalt") == 0){// if no salt
EVP_BytesToKey(EVP_rc4(),EVP_sha256(),NULL,(unsigned char *)argv[4],sizeof(argv[4]),3,hash,NULL);//hash pass with no salt
RC4_set_key(&key,sizeof(hash),(const unsigned char*)hash);//set the hash value to the rc4 key
RC4(&key, datalen, (const unsigned char*)data.c_str(), buf);//encrypt using rc4 key and save it to buffer
string enc((char*)buf, datalen);//set the buffer with datalen to string enc
free(buf);//free the buffer
out.clear();//clear the file
out<<enc;// write to file
}else if(strcmp(argv[5],"-salt") == 0){// else default salt
EVP_BytesToKey(EVP_rc4(),EVP_sha256(),(const unsigned char*)salt,(unsigned char *)argv[4],sizeof(argv[4]),3,hash,NULL);//hash pass with salt
RC4_set_key(&key,sizeof(hash),(const unsigned char*)hash);// set hash value to the rc4 key
RC4(&key, datalen, (const unsigned char*)data.c_str(), buf);//set the
string enc((char*)buf, datalen);//set the buffer with datalen to string enc
free(buf);//free the buffer
out.clear();//clear the file
string finalData = pass + (char*)salt + enc ;
//out<<pass;// write Salted__ to file
//out<<;// write the salt to the file
out<<finalData;// write to file
}else{
printf("Error: specify salt\n");
exit(1);
}
out.close();
}else if(strcmp(argv[1],"-d") == 0){
/*******************************************DECRYPTION*************************************************/
string salt;// salt value buffer
unsigned char hash[16];// hash value buffer
ifstream in(argv[2]);// open the first file
ofstream out;
string data = slurp(in);// save all the data into a string
salt = data.substr(8,8);//get the value of salt from data
out.open(argv[3],ios::binary);// open second value in binary
RC4_KEY key;// initialize rc4 key
//check for salt value
if(strcmp(argv[5],"-nosalt") == 0){// if no salt
int datalen = data.size();// get the size of the file
unsigned char *buf = (unsigned char*)malloc(datalen + 1);// allocate a block in memory of datalength + 1 bytes
memset(buf, 0, datalen + 1);//fills memory with blank placeholder = 0
EVP_BytesToKey(EVP_rc4(),EVP_sha256(),NULL,(unsigned char *)argv[4],sizeof(argv[4]),3,hash,NULL);//hash pass with no salt
RC4_set_key(&key,sizeof(hash),(const unsigned char*)hash);//set the hash value to the rc4 key
RC4(&key, datalen, (const unsigned char*)data.c_str(), buf);//encrypt using rc4 key and save it to buffer
string enc((char*)buf, datalen);//set the buffer with datalen to string enc
free(buf);//free the buffer
out.clear();//clear the file
out<<enc;// write to file
}else if(strcmp(argv[5],"-salt") == 0){// else default salt
string trunData = data.substr(16);//truncate the first 16 bytes
int datalen = trunData.size();// get the size of the file
unsigned char *buf = (unsigned char*)malloc(datalen + 1);// allocate a block in memory of datalength + 1 bytes
memset(buf, 0, datalen + 1);//fills memory with blank placeholder = 0
EVP_BytesToKey(EVP_rc4(),EVP_sha256(), (const unsigned char*)salt.c_str(),(unsigned char *)argv[4],sizeof(argv[4]),3,hash,NULL);//hash pass with salt
RC4_set_key(&key,sizeof(hash),(const unsigned char*)hash);// set hash value to the rc4 key
RC4(&key, datalen, (const unsigned char*)trunData.c_str(), buf);//set the
string enc((char*)buf, datalen);//set the buffer with datalen to string enc
free(buf);//free the buffer
out.clear();//clear the file
out<<enc;// write to file
}else{
printf("Error: specify salt\n");
exit(1);
}
out.close();
}
else{
printf("Error: please use -e/encryption or -d/decryption\n");
exit(1);
}