I want to encrypt and decrypt ASCII messages using an RSA algorithm written in assembly. I read that for security and efficiency reasons the encryption is normally not called character-wise but a number of characters is grouped and encrypted together (e.g. wikipedia says that 3 chars are grouped). Let us assume that we want to encrypt the message "aaa" grouping 2 characters. "aaa" is stored as 61616100. If we group two characters and encrypt the resulting halfwords the result for the 6161 block can in fact be something like 0053. This will result in an artificial second '\0' character which corrupts the resulting message. Is there any way to work around this problem? Using padding or anything similar is unfortunately not an option since I am required to use the same function for encrypting and decrypting.
Asked
Active
Viewed 869 times
1
-
Is this for a class assignment? If so, what are the exact requirements? – Jan 26 '15 at 21:57
-
Is the 'ARM' tag in error? What does the question have to do with ARM? See: [RSA padding on Wikipedia](http://en.wikipedia.org/wiki/RSA_%28cryptosystem%29#Padding). – artless noise Jan 26 '15 at 22:00
-
@duskwuff Yes, the requirement is that I use one function: char *crypt(char *message, unsigned int e, unsigned int N) for RSA decryption and RSA encryption of an ASCII message (which is possible in rsa if you choose e correctly) – Ozelotl Jan 26 '15 at 22:03
-
@artlessnoise I have to implement the algorithm on an ARM assembler. I don't know if there is a better way to differentiate between the "real" '\0' and a calculated one in e.g. c It seems the tag is misleading though, sorr, I will remove it. – Ozelotl Jan 26 '15 at 22:07
-
Use a buffer (on stack or static). In your example, the block size is 4. So keep *decrementing 4* until you need the padding (or done). Then copy the data to buffer (with extra space), pad it and do your final encrypt/decrypt. Copy the final values back (only 3 bytes in your example). Encrypt/decrypt must agree on padding sizes. – artless noise Jan 26 '15 at 22:22
1 Answers
0
The output of RSA is a number. Usually this number is encoded as an octet string (or byte array). You should not treat the result as a character string. You need to treat it as a byte array with the same length as the modulus (or at least the length of the modulus in bytes).
Besides the result containing a zero (null-terminator) the characters may have any value, including non-printable characters such as control characters and 7F
. If you want to treat the result as a printable string, convert to hex or base64.

Maarten Bodewes
- 90,524
- 13
- 150
- 263
-
The size of the modulus in bits is also the key size, by the way. – Maarten Bodewes Jan 26 '15 at 23:49