EldoS | Feel safer!

Software components for data protection, secure storage and transfer

probleme for signfile in c#

Also by EldoS: RawDisk
Access locked and protected files in Windows, read and write disks and partitions and more.
#17731
Posted: 10/05/2011 05:11:05
by Bruno Penguilly (Standard support level)
Joined: 09/30/2011
Posts: 12

that's exactly what I did, and actually I find differences

see : in c#

Code
  if (!Storage.Module.get_Slot(0).TokenPresent)
              {
                  MessageBox.Show("Clé introuvable dans le slot spécifié", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                  return;
              }
              if (Session != null)
              {

                  Session.Logout();
                  Storage.CloseSession(0);
                  Session = null;
              }
              RO = Storage.Module.get_Slot(0).ReadOnly;
              try
              {
                  Session = Storage.OpenSession(0, RO);
              }
              catch
              {
                  if (!RO)
                      Session = Storage.OpenSession(0, true);
                  else
                      throw;
              }
              try
              {
                  Session.Login((int)SBPKCS11Base.Unit.utUser, Password);
              }
              catch
              {
                  Storage.CloseSession(0);
                  Session = null;
              }
            
          
              TElRSAPublicKeyCrypto signature = new TElRSAPublicKeyCrypto(SBUtils.Unit.SB_CERT_ALGORITHM_SHA256_RSA_ENCRYPTION, null);
              CertStorage = new TElMemoryCertStorage();//creation de linstance de l'objet
              cert = Storage.get_Certificates(0); // chargement du certificat
              
              
              
              byte[] InBuffer, OutBuffer;
              FileStream Stream;
              FileStream Stream2;
              int InSize, OutSize, res;
            
              Stream2 = new FileStream(chemsignature, FileMode.CreateNew ,FileAccess.ReadWrite);

              Stream = new FileStream(_chem_import, FileMode.Open, FileAccess.Read);
              try
              {
                  InSize = (int)Stream.Length;
                  InBuffer = new byte[InSize];
                  Stream.Read(InBuffer, 0, InSize);
              }
              finally
              {
                  Stream.Close();
              }
              
              signature.InputEncoding = TSBPublicKeyCryptoEncoding.pkeBinary;
              signature.OutputEncoding = TSBPublicKeyCryptoEncoding.pkeBase64;
              signature.KeyMaterial = cert.KeyMaterial;
              signature.CryptoType = TSBRSAPublicKeyCryptoType.rsapktPKCS1;
        
              try
              {
                  signature.SignDetached(Stream, Stream2, InSize);
              }
            
              catch
              {
              
                  return;
              }


in delphi :
Code
function TForm1.SignaturePersonnelle(fnSourceSigner: TFileName): TFileName;
var
  InBuffer : ByteArray;
  OutBuffer : ByteArray;
  Stream : TFileStream;
  FichDest : TFileName;
  InSize : Integer;
  OutSize : Integer;
  Res : Integer;
  certstorag : TElMemoryCertStorage;
  Signature : TElRSAPublicKeyCrypto;
  fsSource : TFileStream;
  fsDest : TFileStream;

begin

  //Fichier de sortie de la Signature
  FichDest := 'DigitalSign.sig';
  CertStorage := TElMemoryCertStorage.Create(nil);
  Signature := TElRSAPublicKeyCrypto.Create(SB_CERT_ALGORITHM_SHA256_RSA_ENCRYPTION);
  Cert := Storage.Certificates[0];
  try
    try

      //Paramètres de la signature
      Signature.KeyMaterial := Cert.KeyMaterial;  //Cert => Voir fonction LoadCertificat(NumSlot: Integer);
      Signature.CryptoType := rsapktPKCS1;
      //signature.KeyMaterial.
      Signature.UseAlgorithmPrefix := True;

      //Format fichier source et destination
      Signature.InputEncoding := pkeBinary;  //Fichier de destination => Format binaire
      Signature.OutputEncoding := pkeBase64;  //Fichier source => Format B64

      //fnSourceSigner = Fichier source à signer
      //On aura traité le fichier en ôtant les caractères hexa 0A/0a = CR, 0D/0d = LF et 1A/1a = Ctrl-Z
      fsSource := TFileStream.Create(fnSourceSigner, fmOpenRead);
      try//fsSource
        fsDest := TFileStream.Create(FichDest, fmCreate);
        try//fsDest
          //Signature du document

          Signature.SignDetached(fsSource, fsDest);
        finally
          FreeAndNil(fsDest);
        end;//fsDest
      finally
        FreeAndNil(fsSource);
      end;//fsSource

    except
      Result := '';
      exit;
    end;

    Result := FichDest;

  finally
    FreeAndNil(Signature);
  end;
#17732
Posted: 10/05/2011 05:14:15
by Bruno Penguilly (Standard support level)
Joined: 09/30/2011
Posts: 12

here the file

Code
file to sign  "<?xml version="1.0" encoding="UTF-8"?><Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.02">  <pain.001.001.02>    <GrpHdr>      <MsgId>20/12/10 09:13:00</MsgId>      <CreDtTm>2011-03-29T09:25:54</CreDtTm>      <NbOfTxs>1</NbOfTxs>      <CtrlSum>25</CtrlSum>      <Grpg>MIXD</Grpg>      <InitgPty>        <Nm>COGIMA</Nm>      </InitgPty>    </GrpHdr>    <PmtInf>      <PmtInfId>REF</PmtInfId>      <PmtMtd>TRF</PmtMtd>      <PmtTpInf>        <SvcLvl>          <Cd>SEPA</Cd>        </SvcLvl>      </PmtTpInf>      <ReqdExctnDt>2010-12-20</ReqdExctnDt>      <Dbtr>        <Nm>COGIMA</Nm>      </Dbtr>      <DbtrAcct>        <Id>          <IBAN>FR7630010000050002560000068</IBAN>        </Id>      </DbtrAcct>      <DbtrAgt>        <FinInstnId>          <BIC>EBIXQUAL</BIC>        </FinInstnId>      </DbtrAgt>      <CdtTrfTxInf>        <PmtId>          <EndToEndId>aaaa</EndToEndId>        </PmtId>        <Amt>          <InstdAmt Ccy="EUR">25</InstdAmt>        </Amt>        <CdtrAgt>          <FinInstnId>            <BIC>12345678</BIC>          </FinInstnId>        </CdtrAgt>        <Cdtr>          <Nm>VILLEQUEY</Nm>        </Cdtr>        <CdtrAcct>          <Id>            <IBAN>FR972000100002025600023VL73</IBAN>          </Id>        </CdtrAcct>        <RmtInf>          <Ustrd>aaaa</Ustrd>        </RmtInf>      </CdtTrfTxInf>    </PmtInf>  </pain.001.001.02></Document>"


'digitalSign.sig' result in delphi
Code
"31sNc8o7jw2ZDCZEsZKKLKZGwy9J2J83QTjtlNh0L/jp2XJJqPM8bhD4aR5iBC1045WBlf9ZDat6xAFN9EcdAwpQyFo+DokFBMAL9HjROWZj1oWDjN54EUxgy34tvrXXInlVaGMYNDG0DeFZ2CD1Q1W/kOWtuD+qH2bzsv/wdurm5gpEJ//fXUT...p8J1/Vg9/uRBlg8JAd9qTleVCus+yOVA=="


result in c#
Code
"u8pG9HfIv9eXmyKRRPjlQ+WXnq...nTXA+KCTmxdKixUb9CUmilbWHh0VoWfgSvG+ukzgeF3Xpx0yoAZs8yC9gRUm6qWLCswVSeawpfjhc7+GraU...mpUJ+JTfQvCJtsDWrlVwPUqHU3aNMKSS40XHZTcmTDD1TwYcObIksZVZM/+CwwgnP9mxOeE9Zh4Tc+33Mg=="
#17733
Posted: 10/05/2011 05:21:52
by Vsevolod Ievgiienko (EldoS Corp.)

In your C# code signature.UseAlgorithmPrefix = true; is missing. This is the only difference from the first point of view.
#17734
Posted: 10/05/2011 06:13:02
by Eugene Mayevski (EldoS Corp.)

Bruno, please use "CODE" button and tag in the forum post editor to mark your code. This will collapse the code to scrollable box and not span it across the all page. The same applies to file contents (though files are better be posted as attachments).


Sincerely yours
Eugene Mayevski
#17735
Posted: 10/05/2011 07:12:35
by Bruno Penguilly (Standard support level)
Joined: 09/30/2011
Posts: 12

yes but i the object the default value is true.
I made ​​the following changes:
j have to add the management of a message in my catch
and the stream was closed
Now the program is completely frozen on the function
signature.SignDetached(Stream, Stream2, InSize);

the function returns no error but does not exit.









Code
try
              {
                  InSize = (int)Stream.Length;
                  InBuffer = new byte[InSize];
                  Stream.Read(InBuffer, 0, InSize);
              }
              finally
              {
                //  Stream.Close();
              }
              
              signature.InputEncoding = TSBPublicKeyCryptoEncoding.pkeBinary;
              signature.OutputEncoding = TSBPublicKeyCryptoEncoding.pkeBase64;
              signature.KeyMaterial = cert.KeyMaterial;
              signature.CryptoType = TSBRSAPublicKeyCryptoType.rsapktPKCS1;
              signature.UseAlgorithmPrefix = true;
              try
              {
                  signature.SignDetached(Stream, Stream2, InSize);
              }
            
              catch(Exception E)
              {
                  MessageBox.Show("erreur" + E.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                  return;
              }
              MessageBox.Show("The file has been succesfully signed", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
              Stream.Close();
              Stream2.Close();
#17736
Posted: 10/05/2011 07:23:17
by Bruno Penguilly (Standard support level)
Joined: 09/30/2011
Posts: 12

ok
my code is not really clean
everything works fine.
I'll be able to order.

best regards
Also by EldoS: CallbackFilter
A component to monitor and control disk activity, track file and directory operations (create, read, write, rename etc.), alter file data, encrypt files, create virtual files.

Reply

Statistics

Topic viewed 3816 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!