Saving keys in DB

Discussion of open issues, suggestions and bugs regarding network security and data protection solution - SecureBridge
Post Reply
baech
Posts: 10
Joined: Thu 25 Aug 2016 09:30

Saving keys in DB

Post by baech » Mon 12 Sep 2016 12:25

Hi, instead of saving the keys generated to a file, I need to save them directly in a database.

Now after i Call TScKey.Generate how can i access the two keys seperately for saving public in 1 field and private in another?

And is there any possibility to specify where the private key will be saved to, if I don't want them laying in the root folder of my project.

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: Saving keys in DB

Post by ViktorV » Tue 13 Sep 2016 09:48

To resolve your issue, you can use the TScKey.ExportTo method. See more details about this method in SecureBridge help: https://www.devart.com/sbridge/docs/tsckey_exportto.htm

baech
Posts: 10
Joined: Thu 25 Aug 2016 09:30

Re: Saving keys in DB

Post by baech » Tue 13 Sep 2016 09:55

ViktorV wrote:To resolve your issue, you can use the TScKey.ExportTo method. See more details about this method in SecureBridge help: https://www.devart.com/sbridge/docs/tsckey_exportto.htm
This doesn't Stop TScKey.Generate to leave a private.key in the root folder?

I Have used TScKey.ExportTo, but the private key still lingers in root. Is there no way to specify where generate puts the key ?

Another quirk I stumbled upon is that if the Boolean argument 'PublicKeyOnly' for ExportTo is set to false - it will ONLY export the Private key?

EDIT:
Nevermind, I guess i forgot to set the path property on the FileStorage I am using

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: Saving keys in DB

Post by ViktorV » Wed 14 Sep 2016 08:19

In order not to save the key automatically on calling the TScKey.Generate method, you should use TScMemoryStorage instead of TScFileStorage.
The private key always contains the public key, therefore, to export the private key, you should call the TScKey.ExportTo method with the PublicKeyOnly argument set to False.

baech
Posts: 10
Joined: Thu 25 Aug 2016 09:30

Re: Saving keys in DB

Post by baech » Wed 14 Sep 2016 13:38

Okay I see, thank you it works!

Another small question, if I have a key stored in a .txt document and want to import that key, not from the file, but directly from a string. would that be possible ?

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: Saving keys in DB

Post by ViktorV » Thu 15 Sep 2016 08:36

To resolve the task, you can use the following code

Code: Select all

var
  Key: TscKey;
  Private_Key: String;
  SStream: TStringStream;
...
  Key := TScKey.Create(ScMemoryStorage.Keys);
  SStream:=TStringStream.Create(Private_Key);
  Key.ImportFrom(SStream);

baech
Posts: 10
Joined: Thu 25 Aug 2016 09:30

Re: Saving keys in DB

Post by baech » Thu 15 Sep 2016 09:52

Okay thanks, I have one last question (last i swear! I know I have been bombarding the forums lately).

I have saved my keys in a table with this structure:

Code: Select all

CREATE TABLE KEYPAIRS
(	ID INT,
	Name NVARCHAR(50),
	EditTime DATETIME,
	EditedBy NVARCHAR(25),
	PublicKey NVARCHAR(MAX),
	PrivateKey NVARCHAR(MAX),
	BitCount INT,
	Algorithm INT
)
I save them Like so

Code: Select all

  oPrivateKey := TMemoryStream.Create;
  try
    Key.ExportTo(oPrivateKey, False, '');
    oKeyPair.PrivateKey := EncdDecd.EncodeBase64(oPrivateKey.Memory, oPrivateKey.Size);
  finally
    oPrivateKey.Free;
  end;
I use this code to import the key from the DB:

Code: Select all

oMemoryStream := TMemoryStream.Create;
    oKeyPairList.LoadAll;
    oKeyPair := oKeyPairList.GetByName(cbbKeys.Text);
    oB64Key := EncdDecd.DecodeBase64(oKeyPair.PublicKey);

    if Length(oB64Key) > 0 then
      oMemoryStream.WriteBuffer(oB64Key[0], Length(oB64Key));

    oKey := TScKey.Create;
    oKey.ImportFrom(oMemoryStream);
But if I look at the original KEY it looks like this:
-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCiAykdjETNiioMo1EfhlfLiaE+fMXetTsOwbsf032TGmUwQS/p
b9pWQ9uYb94Dxhu8OMjXXBP5BGbwmv+yZW//7ybxjnRs3VEAOrPBYMzxQeaEqpLw
8Hiw2yABpycCHjTlhEhLhTyqeqtH/zGUs8/3F0MTzcWpZ3QT1QfxJkz7HwIDAQAB
AoGAaN9w4v6knwRb2dhyL9zvwgJyTRfJojNgaln/vkdR10JT1BuuXwaXqBW+9Xsc
lQbNd1aus7GYAC7YT9GVyS4eg95lIJnitJDGGwgvDq3LUPig3xKmKfOOAYirid1x
n7pIpfN5/ZBTU9cLctz3oENwpGej601Z28iy48peRhySD0ECQQC/OUw0efq7Xg9e
IhtoGJFchFK0rNkK00pE25UgaQa1WS1x9N5TL/RcoqOjNgb/43S9Z3ldUm+UgvCd
IXWqQZahAkEA2OStwFniWLnNPQkKfpXPu6mJT/GROLwgQFFoKFFkENr+VIAKAVDk
z0tag1k4FFZIPMgmBVG+8Nze8u0QJPv5vwJBAJDzT+6fYA5TeHa0h+3aYaWtsDvA
oi1wiGW1xU7yDeG9nZM6OKT6PtNRfZUkSR4DPuSr4PV6nZYHw3h4eob6fMECQFjS
tdFbPhXl6YZAaJrUkWEiGgiyPi3E5VRRwl0Yj+lTNeq3EOHfFgVKr8OwywWRHD5A
HUn6Yz7SxT8pFH6A7pMCQA6vRvBftmcM0jhwm9zagq3PmbpyIAHOkihGomYtb+Mr
GmbOI9T7IJR6ukWl/hZKkcZQn9fmt3PC0OKHS92ZVLQ=
-----END RSA PRIVATE KEY-----
but the key I imported (which should be that same key) looks like this:
-----BEGIN RSA PRIVATE KEY-----
MBsCAQACAQACAQACAQACAQACAQACAQACAQACAQA=
-----END RSA PRIVATE KEY-----
please help :)

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: Saving keys in DB

Post by ViktorV » Thu 15 Sep 2016 12:23

Please make sure, that each line of your key, except the last one, contains an end of line control character. In addition, before calling the oKey.ImportFrom method, please insert the following code:

Code: Select all

  oMemoryStream.Position := 0;
If this doesn't help solve the issue, please compose a small sample demonstrating the described behavior and send it to viktorv*devart*com.

baech
Posts: 10
Joined: Thu 25 Aug 2016 09:30

Re: Saving keys in DB

Post by baech » Fri 16 Sep 2016 11:34

That did it, thanks!

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: Saving keys in DB

Post by ViktorV » Fri 16 Sep 2016 12:03

It is good to see that the issue has been solved.
Feel free to contact us if you have any further questions about our products.

Post Reply