flutter-rsa icon indicating copy to clipboard operation
flutter-rsa copied to clipboard

How can we know the length of the key?

Open KeithMorning opened this issue 4 years ago • 3 comments

Great flutter RSA library! I have a question may be stupid... When we encrypt or decrypt a file lang than the key size, how can we know the length(128Bytes or 256Bytes) of the private key or public key? I didn't find this api in the library...

KeithMorning avatar Jun 07 '20 15:06 KeithMorning

HI @KeithMorning at this moment there is no method to obtain the information of the keys, I think it would be useful a method like this, I will investigate about it to see how it can be implemented in these days

note: if you create a key using the Generate method you'll know that value but it's probably not what you need

jerson avatar Jun 07 '20 15:06 jerson

In most programming languages, the PrivateKey and PublicKey entity (class, struct, whatever it's called) usually have Size or Length property/field or something similar.

the key length can be either shown in bit(2048) or in Byte(2048/8 = 256)

the max length of the data that can be encrypted is calculated like this (all sizes are in Byte) :

max lenght for PKCS1v15 = publickKey.Size - 11 max length for OAEP = publicKey.Size - (hash.Size*2) - 2

for example, if we have RSA 2048 key we have: size of RSA-2048 = 256 size of SHA265 = 32

for PKCS1v15 256 - 11 = 245 for OAEP 256 - (32*2) - 2 = 190

ref : https://go.dev/src/crypto/rsa/pkcs1v15.go#L46 https://go.dev/src/crypto/rsa/rsa.go#L416

patachi avatar Dec 18 '21 17:12 patachi

Hi @patachi thanks for that, currently this method is implemented in rsa-mobile so we only need to include in flatbuffers and in dart https://github.com/jerson/rsa-mobile/blob/master/rsa/metadata.go

jerson avatar Dec 18 '21 19:12 jerson