HASH String

Discussion of open issues, suggestions and bugs regarding network security and data protection solution - SecureBridge
Post Reply
RuiSilva
Posts: 7
Joined: Thu 25 Nov 2010 00:44

HASH String

Post by RuiSilva » Thu 25 Nov 2010 01:03

Hello,

I just want to know if it is possible to import one RSA Private Key and
hash one string with TScKey. For example "FAC001:10-10-12T20:20:15"
would give a signed string Base64 with 172 length, something like "Am2K6+BP5....mgc/gpu=".. is it possible ? and if yes could you please give-me just an small example code, because I had already searched and found nothing.

Thank you,

Dimon
Devart Team
Posts: 2910
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Fri 26 Nov 2010 08:36

The TScKey class lets you generating new keys by using the Generate method. To import or save a key in one of formats, you should use the ImportFrom or ExportTo methods correspondingly.

The TScKey lets you to sign data with the private key and verify signature with the public key by using the Sign and VerifySign methods. Also TScKey can encrypt and decrypt data with Encrypt and Decrypt methods.

Code: Select all

var
  Key: TScKey;
  buf, res: TBytes;
  s: string;
begin
  Key.Ready := True;
  s := 'FAC001:10-10-12T20:20:15';
  SetLength(buf, Length(s));
  Move(s[1], buf[0], Length(s));
  res := Key.Encrypt(buf);
  res := Key.Sign(res);
end;

RuiSilva
Posts: 7
Joined: Thu 25 Nov 2010 00:44

Post by RuiSilva » Fri 26 Nov 2010 14:49

Hello,
thanks for the help,
it is raising an exception when Loading the key,
am I doing something wrong ?

key.ImportFrom('c:\Privatekey.pem','');
Key.Ready := True;
s := 'FAC001:10-10-12T20:20:15';

thanks once more,

Dimon
Devart Team
Posts: 2910
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Fri 26 Nov 2010 15:15

It is possible that you are trying to import a key which format is not supported by SecureBridge. Please give a more detailed description of the problem that arises in your application.

RuiSilva
Posts: 7
Joined: Thu 25 Nov 2010 00:44

Post by RuiSilva » Fri 26 Nov 2010 15:49

This was introduced by the Portugal General Directorate of Taxes and they said we could use openssl to generate Private and Public keys, and hash some lines on the Billing documents.

So we generated Private and Public Keys with it, and wanted to work with SecureBridge components to hash that lines.

The Private Key its something like "-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKB....wm-----END RSA PRIVATE KEY-----"

and when we try to import on SecureBridge Demo Application it works, but when I try to import on a new project it raises an Exception - Access Violation.

Do we need to create TSckey first ? we just declare the variable Key and then try to import the key on it. it is necessary to use the TscFileStorage?

thanks for your support,

RuiSilva
Posts: 7
Joined: Thu 25 Nov 2010 00:44

Post by RuiSilva » Fri 26 Nov 2010 16:09

I'm sorry to post again,
but even when we generate they Key without import it raises an execption,

Here's what we are doing,

var Algorithm : TScAsymmetricAlgorithm;
Key: TScKey;
buf, res: TBytes;
s: string;
begin

Algorithm := aaRSA;
Key.Generate(Algorithm, 1024);

Key.Ready := True;
s := 'FAC001:10-10-12T20:20:15';
SetLength(buf, Length(s));
Move(s[1], buf[0], Length(s));
res := Key.Encrypt(buf);
res := Key.Sign(res);
end;

RuiSilva
Posts: 7
Joined: Thu 25 Nov 2010 00:44

Post by RuiSilva » Sun 28 Nov 2010 17:31

Hello Dimon,

It's Working, we had some issues that are now solved..

Thanks for all the support,

Dimon
Devart Team
Posts: 2910
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Mon 29 Nov 2010 08:27

If any other questions come up, please contact me.

RuiSilva
Posts: 7
Joined: Thu 25 Nov 2010 00:44

Post by RuiSilva » Tue 30 Nov 2010 11:33

Hello Dimon,

We've another issue, do you want me to open a New post or use this one ?

Every time we encrypt one string it gives us diferent outputs, even if we generate or import the key, is this normal ?

By the way does Secure Bridge has any option to encode String on Base64 ?

Thanks for the Help,

Dimon
Devart Team
Posts: 2910
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Fri 03 Dec 2010 09:36

RuiSilva wrote:Every time we encrypt one string it gives us diferent outputs, even if we generate or import the key, is this normal ?
Yes, it is correct. RSA cryptoalgorithm adds random padding to the data being encrypted for strong encryption. But on decrypting you will always get the same correct result.

RuiSilva wrote:By the way does Secure Bridge has any option to encode String on Base64 ?
To encode/decode data to the Base64 format you can use the TBase64 class of the ScBase64 unit. This class releases the following methods:

Code: Select all

  class function Encode(const data: TBytes): TBytes;
  class function Decode(const data: TBytes): TBytes;

Post Reply