Beispiel #1
0
/*
 * js private key encrypt
*/
char * js_private_encrypt(const char *plain_text,int * encode_len ,char *private_key)
{
	RSA *rsa_privateKey = NULL;
	int rsa_private_len;
	int i;
	// private_key = rsa_key_seliaze(private_key_str);
	//BIO* p_bio = BIO_new_mem_buf(PRIVATE_KEY, -1);
	BIO* p_bio = BIO_new_mem_buf(private_key, -1);
    rsa_privateKey = PEM_read_bio_RSAPrivateKey(p_bio, NULL, 0, NULL); //
	if ( rsa_privateKey == NULL ) {
		   printf("RSA is NULL\n");
		   return NULL;
	}
    rsa_private_len = RSA_size(rsa_privateKey);
   // printf("RSA private length: %d\n", rsa_private_len);
    // 11 bytes is overhead required for encryption
    int chunk_length = rsa_private_len - 11;
    // plain text length
    int plain_char_len = (int)strlen(plain_text);
    // calculate the number of chunks
    int num_of_chunks = (int)(strlen(plain_text) / chunk_length) + 1;
    int total_cipher_length = 0;
    // the output size is (total number of chunks) x (the key length)
    int encrypted_size = (num_of_chunks * rsa_private_len);
    unsigned char *cipher_data = malloc(encrypted_size + 1);
    char *err = NULL;
    for ( i = 0; i < plain_char_len; i += chunk_length) {
        // get the remaining character count from the plain text
        int remaining_char_count = plain_char_len - i;
        // this len is the number of characters to encrypt, thus take the minimum between the chunk count & the remaining characters
        // this must less than rsa_private_len - 11
        int len = JSMIN(remaining_char_count, chunk_length);
        unsigned char *plain_chunk = malloc(len + 1);
        memset(plain_chunk,0,len + 1);
        // take out chunk of plain text
        memcpy(&plain_chunk[0], &plain_text[i], len);
       // printf("Plain chunk: %s\n", plain_chunk);
        unsigned char *result_chunk = malloc(rsa_private_len + 1);
        memset(result_chunk,0,rsa_private_len + 1);
        int result_length = RSA_private_encrypt(len, plain_chunk, result_chunk, rsa_privateKey, RSA_PKCS1_PADDING); // RSA_NO_PADDING RSA_PKCS1_PADDING
       // printf("Encrypted Result chunk: %s\nEncrypted Chunk length: %d\n", result_chunk, result_length);
        free(plain_chunk);
        if (result_length == -1) {
            ERR_load_CRYPTO_strings();
            fprintf(stderr, "Error %s\n", ERR_error_string(ERR_get_error(), err));
            fprintf(stderr, "Error %s\n", err);
        }
        memcpy(&cipher_data[total_cipher_length], &result_chunk[0], result_length);
        total_cipher_length += result_length;
        free(result_chunk);
    }
    //printf("Total cipher length: %d\n", total_cipher_length);

    RSA_free(rsa_privateKey);
    // size_t total_len = 0;
    // char *encrypted = base64_encode(cipher_data, encrypted_size, &total_len);
    // printf("Final result: %s\n Final result length: %zu\n", encrypted, total_len);
     *encode_len = encrypted_size;
    //free(cipher_data);

    return cipher_data;
}
Beispiel #2
0
char *js_public_encrypt(const char *plain_text, const char *public_key_path) {
    RSA *rsa_publicKey = NULL;
    FILE *fp_publicKey;
    int rsa_public_len;

    if ((fp_publicKey = fopen(public_key_path, "r")) == NULL) {
        printf("Could not open %s\n", public_key_path);
        return '\0';
    }


    if ((rsa_publicKey = PEM_read_RSA_PUBKEY(fp_publicKey, NULL, NULL, NULL)) == NULL) {
        fclose(fp_publicKey);
        printf("Error loading RSA Public Key File.");
        return '\0';
    }
    fclose(fp_publicKey);

    rsa_public_len = RSA_size(rsa_publicKey);
    printf("RSA public length: %d\n", rsa_public_len);

    // 11 bytes is overhead required for encryption
    int chunk_length = rsa_public_len - 11;
    // plain text length
    int plain_char_len = (int)strlen(plain_text);
    // calculate the number of chunks
    int num_of_chunks = (int)(strlen(plain_text) / chunk_length) + 1;

    int total_cipher_length = 0;

    // the output size is (total number of chunks) x (the key length)
    int encrypted_size = (num_of_chunks * rsa_public_len);
    unsigned char *cipher_data = malloc(encrypted_size + 1);

    char *err = NULL;
    for (int i = 0; i < plain_char_len; i += chunk_length) {

        // get the remaining character count from the plain text
        int remaining_char_count = plain_char_len - i;

        // this len is the number of characters to encrypt, thus take the minimum between the chunk count & the remaining characters
        // this must less than rsa_public_len - 11
        int len = JSMIN(remaining_char_count, chunk_length);
        unsigned char *plain_chunk = malloc(len + 1);
        // take out chunk of plain text
        memcpy(&plain_chunk[0], &plain_text[i], len);

        printf("Plain chunk: %s\n", plain_chunk);

        unsigned char *result_chunk = malloc(rsa_public_len + 1);

        int result_length = RSA_public_encrypt(len, plain_chunk, result_chunk, rsa_publicKey, RSA_PKCS1_PADDING);
        printf("Plain char len: %d\n", i);
        printf("Encrypted Result chunk: %s\nEncrypted Chunk length: %d\n", result_chunk, result_length);

        free(plain_chunk);

        if (result_length == -1) {
            ERR_load_CRYPTO_strings();
            fprintf(stderr, "Error %s\n", ERR_error_string(ERR_get_error(), err));
            fprintf(stderr, "Error %s\n", err);
        }

        memcpy(&cipher_data[total_cipher_length], &result_chunk[0], result_length);

        total_cipher_length += result_length;

        free(result_chunk);
    }
    printf("Total cipher length: %d\n", total_cipher_length);

    RSA_free(rsa_publicKey);
    size_t total_len = 0;
    char *encrypted = base64_encode(cipher_data, encrypted_size, &total_len);
    printf("Final result: %s\n Final result length: %zu\n", encrypted, total_len);

    free(cipher_data);

    return encrypted;
}
Beispiel #3
0
/*
 * js pubilc key encrypt
*/
char * js_public_encrypt(const char *plain_text,int * length, char *public_key)
{
    RSA *rsa_publicKey = NULL;
    int rsa_public_len;
    int i;
    //public_key = rsa_key_seliaze(public_key_str);
    BIO* p_bio = BIO_new_mem_buf(public_key, -1);
   // printf("rsa_encrypt is %p \n",p_bio);
    //rsa_publicKey = PEM_read_bio_RSAPublicKey(p_bio, NULL, NULL, NULL); //PEM_read_bio_RSAPrivateKey
    PEM_read_bio_RSA_PUBKEY(p_bio, &rsa_publicKey, NULL, NULL);
    if ( rsa_publicKey == NULL ) {
        printf("RSA is NULL\n");
        return NULL;
    }
    rsa_public_len = RSA_size(rsa_publicKey);
   // printf("RSA public length: %d\n", rsa_public_len);
    // 11 bytes is overhead required for encryption
    int chunk_length = rsa_public_len - 11;
    // plain text length
    int plain_char_len = (int)strlen(plain_text);
    // calculate the number of chunks
    int num_of_chunks = (int)(strlen(plain_text) / chunk_length) + 1;
    int total_cipher_length = 0;
    // the output size is (total number of chunks) x (the key length)
    int encrypted_size = (num_of_chunks * rsa_public_len);
    char *cipher_data = malloc(encrypted_size + 1);
    if (cipher_data) {
        memset(cipher_data,0,encrypted_size + 1);
    }else{
        return NULL;
    }
    char *err = NULL;
    for ( i = 0; i < plain_char_len; i += chunk_length) {

        // get the remaining character count from the plain text
        int remaining_char_count = plain_char_len - i;

        // this len is the number of characters to encrypt, thus take the minimum between the chunk count & the remaining characters
        // this must less than rsa_public_len - 11
        int len = JSMIN(remaining_char_count, chunk_length);
        unsigned char *plain_chunk = calloc(len + 1,1);
        memset(plain_chunk,0,len+1);
        // take out chunk of plain text
        memcpy(&plain_chunk[0], &plain_text[i], len);
//        printf("Plain chunk: %s\n", plain_chunk);
        unsigned char *result_chunk = calloc(rsa_public_len + 1,1);
        memset(result_chunk,0,rsa_public_len + 1);
        int result_length = RSA_public_encrypt(len, plain_chunk,result_chunk, rsa_publicKey, RSA_PKCS1_PADDING);

//        printf("Plain char len: %d\n", i);

      //  printf("\n\n%ld,鍔犲瘑鍓嶆暟鎹�s \n", strlen((char *)plain_chunk), plain_chunk);
       // printf("\鍔犲瘑缁撴灉闀垮害: %d\n", result_length);
       // printf("*********%d*****************\n", strlen(result_chunk));
        free(plain_chunk);
        if (result_length == -1) {
            ERR_load_CRYPTO_strings();
            fprintf(stderr, "Error %s\n", ERR_error_string(ERR_get_error(), err));
            fprintf(stderr, "Error %s\n", err);
            break;
        }
        memcpy(&cipher_data[total_cipher_length], &result_chunk[0], result_length);
    //    strcat(cipher_data, (const char *)result_chunk);
        total_cipher_length += result_length;
        free(result_chunk);
    }
   // printf("\n 鍔犲瘑鎬婚暱搴�d\n", total_cipher_length);
    cipher_data[total_cipher_length] = '\0';
    *length = encrypted_size;
    //printf("\n鍔犲瘑杩斿洖缁撴灉闀垮害%d\n", strlen(cipher_data));
    RSA_free(rsa_publicKey);
    return cipher_data;
}