Skip to content

Saving private key to file #22

@Vineet77

Description

@Vineet77

Module keys
Branch save_key

Now that we have the ability to generate public and private keys. We need the ability to save the private key to a file. We will save the key in a PEM file format.

The header will be PRIVATE KEY.

The function is savePrivateKeyToFile. The function will take a private key and a name, and it will say that key (PEM encoded) in a file <name>.pem. The file should be located under the resources directory.

In order to save to the resources directory, you will need to look into Java's classloader behavoir.
Here are a couple of links to give you a way forward:

Unit tests for the function are already provided, so in other for the issue to be complete, they must pass.

In order to encode a private key to a PEM file, you must encode in into the DER format which, for the Elliptic Curve we use, have the following format:

Let says we have the following private key
0xf01daae1f05fab90ff1fd1c5ca7d6e4cbc2f4e63e6c12cddd4d4a0e0198c564f

And the following public key:
Public key X: 0x91b6287f3393e22f0f3c5f54f192e942f4927d594e6f23404b334beb968cc569
Public key Y: 0x0ba2dbd69c4941b0b4f2c3cb8acd8599bb2715873ed607a4db321d4c431404d9

Then the DER encoding would be:

0x30 -First Byte, declares the start of an ASN.1 sequence
0x77 -Second Byte, length of following sequence
0x02 -Third Byte, declares the start of an integer
0x01 -Fourth Byte, length of integer in bytes (1 byte)
0x01 -Fifth Byte,value of integer (1)
0x04 -Sixth Byte, declares the start of an octet string
0x20 -Seventh Byte, length of string to follow (32 bytes)
 -Eight Byte to 39th byte, private key: 
0xf01daae1f05fab90ff1fd1c5ca7d6e4cbc2f4e63e6c12cddd4d4a0e0198c564f
0xa0 -40st Byte, declares the start of context-specific tag 0
0x0a -41nd Byte, length of context-specific tag
0x06 -42rd Byte, declares the start of an object ID
0x08 -43th Byte, length of object ID to follow 
 -44th Byte to 51th byte, object ID of prime256v1: 
0x2a8648ce3d030107
0xa1 -52nd Byte, declares the start of context-specific tag 1
0x44 -53rd Byte, declares the length of context-sepcifc tag (68 bytes)
0x03 -54th Byte, declares the start of a bit string
0x42 -55th Byte, length of bit string to follow (66 bytes)
0x00 -56th Byte, padding?
0x04 -57th Byte, Indicating uncompressed public key
 -58th Byte to 89th byte, public key x: 
0x91b6287f3393e22f0f3c5f54f192e942f4927d594e6f23404b334beb968cc569
 -90th Byte to 121th byte, public key y: 
0x0ba2dbd69c4941b0b4f2c3cb8acd8599bb2715873ed607a4db321d4c431404d9

Then you take that binary encoding, encode in Base64. Insert a newline character every 64 characters and then add -----BEGIN EC PRIVATE KEY----- and -----END EC PRIVATE KEY----- at the beginning and end respectively.

The end result will be something like this:

-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIPAdquHwX6uQ/x/Rxcp9bky8L05j5sEs3dTUoOAZjFZPoAoGCCqGSM49
AwEHoUQDQgAEkbYofzOT4i8PPF9U8ZLpQvSSfVlObyNASzNL65aMxWkLotvWnElB
sLTyw8uKzYWZuycVhz7WB6TbMh1MQxQE2Q==
-----END EC PRIVATE KEY-----

And then you save this to the file.

Here are some helpful links to understand this stuff better:

If you have any problems with this issues, feel free to contact me.

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions