C++ Cryptlib

Bu başlık kilitlidir. Yeni mesajlar gönderilemez veya mevcut mesajlar düzenlenemez.
xhusox
27-03-2006, 11:20   |  #1  
OP Taze Üye
Teşekkür Sayısı: 0
2 mesaj
Kayıt Tarihi:Kayıt: Mar 2006

Merhaba Arkadaslar,
Türkcem biraz kitdir, kusurabakmayin.


Benim Projem de, bir Smartcardlan bir file li Signature edmeliyim.

Peter Guttman'in Cryptlib len calisiyorum.

Bu liblen calisan oldugsa bana insallah yardim edebillir.


Hata [CODE]cryptgetPrivatekey[/CODE] 4. parameter ne oldugunu tam anlamadim


Source Codum su

[CODE]#include <iostream>
#include "cryptlib.h"
#include <string>
using namespace std;

CRYPT_DEVICE cryptDevice;
CRYPT_USER cryptUser;
CRYPT_CONTEXT cryptContext, hashContext, signContext;
CRYPT_ALGO_TYPE cryptAlgo;
CRYPT_KEYSET keyset;

#define SIGNLENGTH 8192
#define SKeyslength 5

int main()
{

char        text[100] = "TEXT...TEXT...";
int  code = 0;
char signature[SIGNLENGTH];
int signatureLength;

/* Initialise cryptlib */
code = cryptInit();
     cout<<"InitBegin : "<<code<<endl;
       
        CRYPT_OPTION_DEVICE_PKCS11_DVR01;

code =  cryptSetAttributeString( CRYPT_UNUSED,CRYPT_OPTION_DEVICE_PKCS11_DVR01,"pkcs201n.dll", 12 );
  cout<<"SetAttributeString : "<< code<<endl;
  
code =        cryptSetAttribute( CRYPT_UNUSED, CRYPT_OPTION_CONFIGCHANGED, FALSE );
  cout<<"SetAttribute : "<< code<<endl;

code =  cryptDeviceOpen( &cryptDevice, CRYPT_UNUSED, CRYPT_DEVICE_PKCS11, "[Autodetect]" );
  cout<<"DeviceOpen : "<< code<<endl;


/*LOGIN*/
code = cryptSetAttributeString( cryptDevice, CRYPT_DEVINFO_AUTHENT_USER, "1234",4);

  if (code == 0)
  {
          cout<<endl<<"<<LOGIN Erfolgreich>> "<<endl<<endl;       
  }
  else
          cout<<endl<<"<<LOGIN Fehlgeschlagen>> (Fehlercode:"<<code<<")"<<endl<<endl;


//SignContext
  code =  cryptGetPrivateKey(cryptDevice, &signContext, CRYPT_KEYID_NAME, "  ", NULL); //4Parameter ???
  cout<<"GetPrivateKey : "<< code<<endl;


// Create a hash context
code =  cryptCreateContext( &hashContext,CRYPT_UNUSED, CRYPT_ALGO_MD5 );
  cout<<"CreateHashContext: "<< code<<endl;

code =        cryptEncrypt( hashContext, text, strlen(text));
  cout<<"cryptEncrypt: "<< code<<endl;


//Signatur Erstellen
code =  cryptCreateSignatureEx(signature, SIGNLENGTH, &signatureLength,
        CRYPT_FORMAT_PKCS7 ,signContext, hashContext, CRYPT_UNUSED);
  
  cout<<endl<<"<<CreateSignature>>: "<< code<<endl;


/*Destroy - Close*/
code =  cryptDestroyContext (hashContext );
  cout<<"DestroyHashContext: "<< code<<endl;

code =  cryptDestroyContext( signContext );
  cout<<"DestroyDeviceContext: "<< code<<endl;

code =  cryptDeviceClose( cryptDevice );
  cout<<endl<<endl<<"DeviceClose: "<< code<<endl;

/* Shut down cryptlib */
code =  cryptEnd();
  cout<<"InitEnd : "<< code<<endl;

        return 0;
}
[/CODE]


Sagol yardimlariniz icin

nepaizen
27-03-2006, 16:35   |  #2  
Yeni Üye
Teşekkür Sayısı: 0
24 mesaj
Kayıt Tarihi:Kayıt: Mar 2006

hata derleme zaman1nda oluyorsa, 4. parametrede tür uyumsuzlugu vard1r, neden oraya içi bos bir string veriyorsun?

xhusox
29-03-2006, 12:55   |  #3  
OP Taze Üye
Teşekkür Sayısı: 0
2 mesaj
Kayıt Tarihi:Kayıt: Mar 2006

Pardon görmemisim hata ama source da degil hata Smartcard daydi smartcarda iki tane ayni ismili PrivateKey vardi. O hatayi cösdüm simdi ama baska problemim var.


#include <iostream>
#include "cryptlib.h"
#include <string>
using namespace std;

CRYPT_DEVICE cryptDevice;
CRYPT_USER cryptUser;
CRYPT_CONTEXT cryptContext, hashContext, signContext, cryptKeyset, cryptPrivateKey;
CRYPT_ALGO_TYPE cryptAlgo;

int errorLocus;
int errorType;
#define SIGNLENGTH 8192


int main()
{

char        text[100] = "TEXT TEXT TEXT";
int                code = 0 ;
char        signature[SIGNLENGTH];
int                signatureLength;

/* Initialise cryptlib */
code = cryptInit();
        cout<<"Status InitBegin : "<<code<<endl;
       
        CRYPT_OPTION_DEVICE_PKCS11_DVR01;

code =  cryptSetAttributeString( CRYPT_UNUSED,CRYPT_OPTION_DEVICE_PKCS11_DVR01,"pkcs201n.dll", 12 );
                cout<<"Status SetAttributeString : "<< code<<endl;
               
code =        cryptSetAttribute( CRYPT_UNUSED, CRYPT_OPTION_CONFIGCHANGED, FALSE );
                cout<<"Status SetAttribute : "<< code<<endl;

code =  cryptDeviceOpen( &cryptDevice, CRYPT_UNUSED, CRYPT_DEVICE_PKCS11, "[Autodetect]" );
                cout<<"Status DeviceOpen : "<< code<<endl;

code =  cryptCreateContext(&cryptContext,CRYPT_UNUSED, CRYPT_ALGO_RSA);
                cout<<"CreateContext : "<< code<<endl;


/*LOGIN*/
code = cryptSetAttributeString( cryptDevice, CRYPT_DEVINFO_AUTHENT_USER, "1234",4);

                if (code == 0)
                {
                        cout<<endl<<"<<LOGIN Erfolgreich>> "<<endl<<endl;       
                }
                else
                if(code == -1)
                {
                        cout<<endl<<"<<Karte Einlegen>> (Fehlercode:"<<code<<")"<<endl<<endl;
                       
                }
                else
                if(code == -22)
                {
                        cout<<endl<<"<<PIN Falsch>> (Fehlercode:"<<code<<")"<<endl<<endl;

                }
                else
                        cout<<endl<<"<<LOGIN Fehlgeschlagen>> (Fehlercode:"<<code<<")"<<endl<<endl;


//GetPrivateKey
code =  cryptGetPrivateKey( cryptDevice, &signContext, CRYPT_KEYID_NAME, "SKeys", NULL );
                cout<<"Status GetPrivateKey : "<< code<<endl;

// Create a Hash context
code =  cryptCreateContext(&hashContext,CRYPT_UNUSED, CRYPT_ALGO_MD5 );
                cout<<"Status CreateHashContext: "<< code<<endl;


code =        cryptEncrypt( hashContext, text,strlen(text));
                cout<<"Status CryptEncrypt: "<< code<<endl;

//Signatur Erstellen
code =  cryptCreateSignatureEx(signature, SIGNLENGTH, &signatureLength, CRYPT_FORMAT_PKCS7,
                                                           signContext,hashContext, CRYPT_USE_DEFAULT);

//code =  cryptCreateSignature(signature, SIGNLENGTH, &signatureLength, signContext, hashContext);

                cout<<"Status CreateSignature: "<< code<<endl;


//Error Reporting
if(cryptStatusError(code))
{
        int errorCode, errorStringLength;
        char *errorString;

        errorString = (char*)malloc(1024);


cout<<endl<<endl<<"Error Reporting"<<endl;

cryptGetAttribute( signContext , CRYPT_ATTRIBUTE_ERRORLOCUS, &errorLocus);
               
                cout<<"CreateSignatureEx Error Locus : "<<errorLocus<<endl;


cryptGetAttribute( signContext, CRYPT_ATTRIBUTE_ERRORTYPE, &errorType);
               
                cout<<"CreateSignatureEx Error Type : "<<errorType<<endl;


cryptGetAttribute( signContext , CRYPT_ATTRIBUTE_INT_ERRORCODE, &errorCode);
               
                cout<<"CreateSignatureEx Error Code : "<<errorCode<<endl;


cryptGetAttributeString( signContext , CRYPT_ATTRIBUTE_INT_ERRORMESSAGE, errorString, &errorStringLength );
               
                cout<<"CreateSignatureEx Error StringLength : "<<errorStringLength<<endl<<endl<<endl;
}

/*Destroy - Close*/
code =  cryptDestroyContext(hashContext);
                cout<<"Status DestroyHashContext: "<< code<<endl;

code =  cryptDestroyContext(signContext);
                cout<<"Status DestroySignContext: "<< code<<endl;


code =  cryptDestroyContext(cryptContext);
                cout<<"Status DestroyCryptContext: "<< code<<endl;

code =  cryptDeviceClose(cryptDevice);
                cout<<"Status DeviceClose: "<< code<<endl;

/* Shut down cryptlib */
code =  cryptEnd();
                cout<<"Status InitEnd : "<< code<<endl;

        return 0;
}

CMD:

Status InitBegin : 0
Status SetAttributeString : 0
Status SetAttribute : 0
Status DeviceOpen : 0
CreateContext : 0

<<LOGIN Erfolgreich>>

Status GetPrivateKey : 0
Status CreateHashContext: 0
Status CryptEncrypt: 0
Status CreateSignature: -5


Error Reporting
CreateSignatureEx Error Locus : 0
CreateSignatureEx Error Type : 0
CreateSignatureEx Error Code : 0
CreateSignatureEx Error StringLength : -1


Status DestroyHashContext: 0
Status DestroySignContext: 0
Status DestroyCryptContext: 0
Status DeviceClose: 0
Status InitEnd : 0