I am trying to first encrypt one file and send it to server, both client and server are all written in C openssl. I try to encrypt one file and decrypt it in server, but when decrypting I got an error:
error:0407106B:rsa routines:RSA_padding_check_PKCS1_type_2:block type is not 02
I checked this error for a long time and fail to modify it.
Here is part of my code:
RSA:I think this part is fine, becaue in the client I decrypt it immediately after encryption, and it success.
int padding = RSA_PKCS1_PADDING;
RSA * createRSA(unsigned char * key,int public)
{
RSA *rsa= NULL;
BIO *keybio ;
keybio = BIO_new_mem_buf(key, -1);
if (keybio==NULL)
{
printf( "Failed to create key BIO");
return 0;
}
if(public)
{
rsa = PEM_read_bio_RSA_PUBKEY(keybio, &rsa,NULL, NULL);
}
else
{
rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa,NULL, NULL);
}
if(rsa == NULL)
{
printf( "Failed to create RSA");
}
return rsa;
}
char publicKey[]="-----BEGIN PUBLIC KEY-----\n"\
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAy8Dbv8prpJ/0kKhlGeJY\n"\
"ozo2t60EG8L0561g13R29LvMR5hyvGZlGJpmn65+A4xHXInJYiPuKzrKUnApeLZ+\n"\
"vw1HocOAZtWK0z3r26uA8kQYOKX9Qt/DbCdvsF9wF8gRK0ptx9M6R13NvBxvVQAp\n"\
"fc9jB9nTzphOgM4JiEYvlV8FLhg9yZovMYd6Wwf3aoXK891VQxTr/kQYoq1Yp+68\n"\
"i6T4nNq7NWC+UNVjQHxNQMQMzU6lWCX8zyg3yH88OAQkUXIXKfQ+NkvYQ1cxaMoV\n"\
"PpY72+eVthKzpMeyHkBn7ciumk5qgLTEJAfWZpe4f4eFZj/Rc8Y8Jj2IS5kVPjUy\n"\
"wQIDAQAB\n"\
"-----END PUBLIC KEY-----\n";
char privateKey[]="-----BEGIN RSA PRIVATE KEY-----\n"
/* ----8<---------------------*/
"-----END RSA PRIVATE KEY-----\n";
int public_encrypt(unsigned char * data,int data_len,unsigned char * key, unsigned char *encrypted)
{
RSA * rsa = createRSA(key,1);
int result = RSA_public_encrypt(data_len,data,encrypted,rsa,padding);
return result;
}
int private_decrypt(unsigned char * enc_data,int data_len,unsigned char * key, unsigned char *decrypted)
{
RSA * rsa = createRSA(key,0);
int result = RSA_private_decrypt(data_len,enc_data,decrypted,rsa,padding);
if(result==-1){
printf("in\n");
unsigned int errCode = ERR_get_error();
printf("\nError: %s\n", ERR_error_string(errCode, NULL));
}
return result;
}
Socket:
int readData(int s, void *buf, int buflen)
{
int total = 0;
char *pbuf = (char*) buf;
while (buflen > 0) {
int numread = recv(s, pbuf, buflen, 0);
if (numread <= 0) return numread;
pbuf += numread;
buflen -= numread;
total += numread;
}
return total;
}
int sendData(int s, void *buf, int buflen)
{
int total = 0;
char *pbuf = (char*) buf;
while (buflen > 0) {
int numsent = send(s, pbuf, buflen, 0);
if (numsent <= 0) return numsent;
pbuf += numsent;
buflen -= numsent;
total += numsent;
}
return total;
}
client:(this is not the whole code I just pick some important code, the decrypt here just to test, and it success to decrypt)
unsigned char encrypted[8192]={};
unsigned char decrypted[8192]={};
char buffer[8192];
bzero(buffer,8192);
stream = fopen(save_name,"w+t");
struct stat st ;
stat( put_name, &st );
fread(buffer,1,st.st_size,stream))
int encrypted_length=public_encrypt(buffer,st.st_size,publicKey,encrypted);
printf("after encrypted%s\n", encrypted);
int decrypted_length= private_decrypt(encrypted,256,privateKey,decrypted);
printf("afterdecrypted: %s\n",decrypted);
sendData(fd,encrypted,strlen(encrypted))
server:
readData(connfd,buf,intsize);
("recieve data buf:%s\n", buf);
unsigned char decrypted[8192]={};
bzero(decrypted,8192);
int decrypted_length = private_decrypt(buf,256,privateKey,decrypted);
I used md5 to check the sent and received data, they are the same. Could you find the reason?
Here is another question, with same error, but I can't get any idea from here. Encryption and decryption error 0x0407106B using OpenSSL
I use
printf("in server: \ndata:%s\nlength:%d\nkeylen:%d\n",buf,strlen(buf),strlen(privateKey) );
to print all perimeters and they are same.
in server:
data:k???
U??uE????^??%?^{?N?-?pg???5?|??
???$???ěQ????zܯ?(T?n>f&??J?C??x?
D
length:82
keylen:1675
in client:
data:k???
U??uE????^??%?^{?N?-?pg???5?|??
???$???ěQ????zܯ?(T?n>f&??J?C??x?
D
length:82
keylen:1675
When I use the valgrind, it shows a lot of problem, the first is:
==21631== 8 bytes in 1 blocks are indirectly lost in loss record 1 of 35
==21631== at 0x4A06A2E: malloc (vg_replace_malloc.c:270)
==21631== by 0x31CF06AC2D: CRYPTO_malloc (in /usr/lib64/libcrypto.so.1.0.1e)
==21631== by 0x31CF0A72C1: ??? (in /usr/lib64/libcrypto.so.1.0.1e)
==21631== by 0x31CF0A742C: bn_expand2 (in /usr/lib64/libcrypto.so.1.0.1e)
==21631== by 0x31CF0A7674: BN_copy (in /usr/lib64/libcrypto.so.1.0.1e)
==21631== by 0x31CF0A7899: BN_dup (in /usr/lib64/libcrypto.so.1.0.1e)
==21631== by 0x31CF0ABE2C: BN_BLINDING_create_param (in /usr/lib64/libcrypto.so.1.0.1e)
==21631== by 0x31CF0CC500: RSA_setup_blinding (in /usr/lib64/libcrypto.so.1.0.1e)
==21631== by 0x31CF0C4483: ??? (in /usr/lib64/libcrypto.so.1.0.1e)
==21631== by 0x31CF0C49C7: ??? (in /usr/lib64/libcrypto.so.1.0.1e)
==21631== by 0x402263: put_file (client.c:442)
==21631== by 0x402675: main (client.c:628)
the line 442 is my decrypt function. So, any problem with my decryption function.