Search code examples
encryptioncryptographyasciirsacomputer-science

Encrypting a letter with RSA


I want to encrypt the letter 'Z' using ASCII (Z=90), using a key with two numbers: (17, 3233)

RSA: 90^17 mod3233 = 1668

I am wondering if there is any way of encrypting this letter into a char value.


Solution

  • RSA is not a stream cipher. The encrypted result always has the size (bits) of the modulus - in your case 3233.

    The number 3233 requires 12 bits - however one byte/character provides only 8 bits. Hence you can't send pack the RSA encrypted text as one byte. You need at least 2 bytes.

    If you can pack the integer in a char depends on your definition of a char:

    char = (printable) ASCII character

    A printable ASCII character usually has 7 bit. You can't store 12 bits in 7 bit.

    char = byte

    A standard character is equivalent of a byte and allows to store 8 bits. You can't store 12 bits in 8 bit.

    char = Java UTF-16 char

    Considering that a Java char is an UTF-16 character you may be able to save the integer as one character, however storing binary data in a Java UTF-16 char is a very unclean and hackish solution. I strongly recommend not implement this! Binary data should not be saved in a character(Array) without proper conversion and encoding (e.g. base64 of hexadecimal encoding).