Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I combine n and e to create the public key in RSA?

I have a 128-byte (1024-bit) modulus (in a byte array format) and my exponent (also in a byte array format). I need to create a 128-byte array representing the public key.

According to Wikipedia, "The public key consists of the modulus n and the public (or encryption) exponent e." But that doesn't tell me how to mix both.

What is the right operation to do?
- n^e (will that stay 128 bytes long?)
- just n?
- n followed by e?
- n added to e?
- something else?

like image 279
Lazlo Avatar asked Feb 28 '23 08:02

Lazlo


2 Answers

There are many different formats to represent RSA public keys. One of the more wide-spread ones is PKCS#1. In RFC 3447, the definition of the public key format is given as

  RSAPublicKey ::= SEQUENCE {
      modulus           INTEGER,  -- n
      publicExponent    INTEGER   -- e
  }

To represent a key in that format, you need to apply the ASN.1 DER encoding to this data structure.

Another choice is the SubjectPublicKeyInfo, from RFC 3280:

  SubjectPublicKeyInfo  ::=  SEQUENCE  {
     algorithm            AlgorithmIdentifier,
     subjectPublicKey     BIT STRING  
  }

For RSA, the algorithm should be 1.2.840.113549.1.1.1.

There are several other formats, such as the ones used for SSL.

like image 96
Martin v. Löwis Avatar answered Mar 02 '23 21:03

Martin v. Löwis


You won't be able to create such an array. The "public key" has two parts: the exponent and the modulus. They are separate numbers that must be kept separate, since both are needed to perform encryption and decryption later on. Although your n is 1024 bits, the public key altogether is necessarily longer.

like image 39
VoteyDisciple Avatar answered Mar 02 '23 22:03

VoteyDisciple