Crypto.RSAEncrypt

Can Crypto.RSAEncrypt be used to encrypt a textfile which can be decrypted with Crypto.RSADecrypt on iOS?

Sure ,why not?

Because I got error:

RSA/OAEP-MGF1(SHA-1): message length of 9824 exceeds the maximum of 86 for this public key

[code]
// rsakeypublic = Generated by Crypto.RSAGenerateKeyPair( 1024, privateKey, publicKey )
// rsakeypublic length = 320 bytes
// data = all bytes of a TextInputStream

dim privateKey as String
dim publicKey as String = rsakeypublic
dim data as MemoryBlock = myInputFile.ReadAll
dim encryptedData as MemoryBlock

try
encryptedData=Crypto.RSAEncrypt( data, publicKey )
catch e as CryptoException
MsgBox("CryptoException: " + e.Message)
end try[/code]

You will need to encrypt the data in smaller chunks. Here’s how it appears to be calculated:

It looks like the header size is 42 (go figure):

1024 ÷ 8 = 128 - 42 = 86 2048 ÷ 8 = 256 - 42 = 214 4096 ÷ 8 = 512 - 42 = 470

So for a 2048 bit key, you divide by 8 to get 128, subtract 42 for the header and you get a maximum of 86 bytes per chunk.

From what I’ve seen, the way you normally encrypt a larger stream of data is to use another encryption method like blowfish, use RSA to encrypt the blowfish encryption key and then concat the two together for transmission. This is done because RSA is relatively slow compared to other methods.

Oke thanks!
I only have the Xojo Crypto in iOS so it will be difficult to use something else…

Somewhere, there is a Xojo implementation of blowfish. I know I’ve seen it, I just can’t remember where.

This would probably work:

https://sites.google.com/site/skydancerstudios/rb-blowfish

Greg - You might have been referring to Kem’s Blowfish classes?

https://github.com/ktekinay/Blowfish

My classes won’t work in iOS though.

The other class looks rather complex as well.

It might be complex because Blowfish is complex. Mine, on the other hand, is “sophisticated”.

:stuck_out_tongue:

I simply encrypt in 214 byte chunks and co cat the result together.

And I tried to fix my iPhone’s autocorrect of “concat” into “co cat” and screwed myself from the edit ability…

Seriously, co cat?

I’m working on encryption for iOS :slight_smile:
Hope to have some thing to show soon.

Indeed, the combination of Asymmetric encryption (RSA public key) along with symmetric encryption (such as blowfish) is common. If you want to do the symmetric part in pure Xojo code there’s a reasonably fast RC4 algorithm we worked out here: https://forum.xojo.com/13818-faster-encyrption-obscuration-than-rc4-for-large-24mb-15mb-file/p1#p110903