Saving keys in DB
Saving keys in DB
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.
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.
Re: Saving keys in DB
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
Re: Saving keys in DB
This doesn't Stop TScKey.Generate to leave a private.key in the root folder?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
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
Re: Saving keys in DB
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.
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.
Re: Saving keys in DB
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 ?
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 ?
Re: Saving keys in DB
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);
Re: Saving keys in DB
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:
I save them Like so
I use this code to import the key from the DB:
But if I look at the original KEY it looks like this:

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
)
Code: Select all
oPrivateKey := TMemoryStream.Create;
try
Key.ExportTo(oPrivateKey, False, '');
oKeyPair.PrivateKey := EncdDecd.EncodeBase64(oPrivateKey.Memory, oPrivateKey.Size);
finally
oPrivateKey.Free;
end;
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 the key I imported (which should be that same key) 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-----
please help-----BEGIN RSA PRIVATE KEY-----
MBsCAQACAQACAQACAQACAQACAQACAQACAQACAQA=
-----END RSA PRIVATE KEY-----

Re: Saving keys in DB
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:
If this doesn't help solve the issue, please compose a small sample demonstrating the described behavior and send it to viktorv*devart*com.
Code: Select all
oMemoryStream.Position := 0;
Re: Saving keys in DB
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.
Feel free to contact us if you have any further questions about our products.