user971840
user971840

Reputation: 75

RSA String Encryption\Decryption

How can I encrypt/decrypt a string myself with N, P, Q, and public/private keys e and d?

I tried casting each individual character as a int, performing the calculations, and casting it back as a char, but I seem to get the same character after encryption for different characters.

That is, the characters don't seem to be mapping one to one.

for( int i = 0; i < message.length() - 1; i++ )
{
    ori = (int)message[ i ];

    for( int j = 0; j < e; j++ )
        ori = ( (int)message[ i ] * ori ) % N;

     message[ i ] = (char)ori;
}

N is the product of two primes, e is the exponent of the number I'm trying to encrypt.

Upvotes: 0

Views: 1562

Answers (1)

Keith Irwin
Keith Irwin

Reputation: 5668

Some things to note about this:

  1. you're not raising it to e, you're raising it to e+1 because you start with message[i] to the 1 and then multiply it by message[i] e more times, giving message[i] to the e+1.
  2. When you cast it back as a char, this is unlikely to work because there's no reason to believe that it will be within the char range. It's more likely that most times it will be greater than 255 unless N is less than 255.
  3. In general, you don't encode strings with RSA this way because each string will encode to a unique number, so it'll be vulnerable to letter frequency attacks. Instead, actual protocols almost always deal with this problem by encoding a secret key with RSA and then using the secret key to encode the message using a symmetric key block or stream cipher.

Upvotes: 1

Related Questions