Asymmetric Deterministic Encryption (RSA) with Ruby

I was wondering if anyone knows a way to deterministically encrypt a value in Ruby using an asymmetric encryption algorithm.

For most use cases, you only need to get "A" when encrypting "A", when you decrypt it, that is, you do not care about the encrypted value itself. You only care about the complete circuit.

However, for the application I'm developing, I really need the result to be deterministic. That is, I need to encrypt something using RSA without a variable padding.

When I try to encrypt a value using OpenSSL::PKey::RSA::NO_PADDING , an error is returned:

 OpenSSL::PKey::RSAError Exception: data too small for key size 

Does anyone have an idea on how I can get a deterministic encrypted value using RSA?

Yours faithfully,

ABD

+4
source share
2 answers

This error comes from crypto / rsa / rsa_none.c

 int RSA_padding_add_none(unsigned char *to, int tlen, const unsigned char *from, int flen) { if (flen > tlen) { RSAerr(RSA_F_RSA_PADDING_ADD_NONE,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); return(0); } if (flen < tlen) { RSAerr(RSA_F_RSA_PADDING_ADD_NONE,RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE); return(0); } memcpy(to,from,(unsigned int)flen); return(1); } 

Rypto / rsa / rsa_eay.c is called

 static int RSA_eay_public_encrypt(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding) ... i=RSA_padding_add_none(buf,num,from,flen); 

flen is a len message; and tlen has the meaning: num=BN_num_bytes(rsa->n);

So,. Your data has the same byte length as your RSA key parameter N

In addition, as I know, your data should be less than N (if you count one long long binary number)

+1
source

You can fill in the corresponding key length yourself using non-random data

+2
source

Source: https://habr.com/ru/post/1332561/


All Articles