Page 1 of 1

HASH String

Posted: Thu 25 Nov 2010 01:03
by RuiSilva
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,

Posted: Fri 26 Nov 2010 08:36
by Dimon
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;

Posted: Fri 26 Nov 2010 14:49
by RuiSilva
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,

Posted: Fri 26 Nov 2010 15:15
by Dimon
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.

Posted: Fri 26 Nov 2010 15:49
by RuiSilva
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,

Posted: Fri 26 Nov 2010 16:09
by RuiSilva
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;

Posted: Sun 28 Nov 2010 17:31
by RuiSilva
Hello Dimon,

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

Thanks for all the support,

Posted: Mon 29 Nov 2010 08:27
by Dimon
If any other questions come up, please contact me.

Posted: Tue 30 Nov 2010 11:33
by RuiSilva
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,

Posted: Fri 03 Dec 2010 09:36
by Dimon
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;