ايران ويج

نسخه‌ی کامل: کمک در مورد Base64 and PK11 Decryption
شما در حال مشاهده‌ی نسخه‌ی متنی این صفحه می‌باشید. مشاهده‌ی نسخه‌ی کامل با قالب بندی مناسب.
سلام دوستان

بعنوان یک پروژه دانشگاهی مجبورم این کد C++ رو به زبان ویژوال بيسيک بنويسم

کسی میتونه کمک کنه برای تبدیلش به زبان VB ط?


اگر هم نیاز به پرداخت هزینه هست لطفا بهم بگین چون واقعا بهش نیاز دارم

مرسی





کد:
#include <stdio.h>
#include <windows.h>

struct SECItem
{
    char type;
    unsigned char *data;
    unsigned int len;
};


typedef enum SECStatus
{
    SECSuccess = 0
};

#define PRBool   int
#define PRUint32 unsigned int
#define PR_TRUE  1
#define PR_FALSE 0

#define NSS_LIBRARY_NAME   "lib1.dll"
#define PLC_LIBRARY_NAME   "lib2.dll"
#define NSPR_LIBRARY_NAME  "lib3.dll"
#define PLDS_LIBRARY_NAME  "lib4.dll"
#define SOFTN_LIBRARY_NAME "lib5.dll"

#define LOADLIBRARY(x)  LoadLibrary(x)
#define GETPROCADDRESS  GetProcAddress

typedef struct PK11SlotInfoStr PK11SlotInfo;

// NSS Library functions
typedef SECStatus      (*NSS_Init) (const char *configdir);
typedef SECStatus      (*NSS_Shutdown) (void);
typedef PK11SlotInfo * (*PK11_GetInternalKeySlot) (void);
typedef void           (*PK11_FreeSlot) (PK11SlotInfo *slot);
typedef SECStatus      (*PK11_CheckUserPassword) (PK11SlotInfo *slot,char *pw);
typedef SECStatus      (*PK11_Authenticate) (PK11SlotInfo *slot, PRBool loadCerts, void *wincx);
typedef SECStatus      (*PK11SDR_Decrypt) (SECItem *data, SECItem *result, void *cx);

// PLC Library functions
typedef char *         (*PL_Base64Decode)( const char *src, PRUint32 srclen, char *dest);


// Function declarations..
int InitializeLibrary(char *DirPath);
int InitializeNSSLibrary(char *profilePath);

int PK11Decrypt(char *decodeData, int decodeLen, char **clearData, int *finalLen);
int Base64Decode(char *cryptData, char **decodeData, int *decodeLen);
int DecryptSecretString(char *cryptData, char **clearData);


NSS_Init                NSSInit = NULL;
PK11_GetInternalKeySlot PK11GetInternalKeySlot = NULL;
PK11_FreeSlot           PK11FreeSlot = NULL;
PK11_Authenticate       PK11Authenticate = NULL;
PK11SDR_Decrypt         PK11SDRDecrypt = NULL;
PL_Base64Decode         PLBase64Decode = NULL;

int IsNSSInitialized = 0;


HMODULE libnss = NULL;
HMODULE libplc = NULL;
HMODULE libtmp = NULL;

HMODULE LoadLibrary(char *AppDir, char *libName)
{

    //Libraries are in the same folder with the application
    libtmp = LOADLIBRARY("");

    if( !libtmp )    return 0;

    return libtmp;
}


/**
*     Loads the libraries from the specified path. On failure, it tries
*     to load the libraries from current path. Then extracts the required functions
*     from it.
*
*     @return   1   success & 0 on failure.
*/
int InitializeLibrary(char *DirPath)
{
    
    libnss = libplc = NULL;

    // load libraries

    if( !libnss )
    {
        
        libnss =LOADLIBRARY(NSS_LIBRARY_NAME);
        libplc =LOADLIBRARY(PLC_LIBRARY_NAME);

        if( !libnss || !libplc ) return 0;
    }


    // Extract the required functions....
    NSSInit                   = (NSS_Init) GETPROCADDRESS(libnss, "NSS_Init");
    PK11GetInternalKeySlot = (PK11_GetInternalKeySlot) GETPROCADDRESS(libnss, "PK11_GetInternalKeySlot");
    PK11FreeSlot           = (PK11_FreeSlot) GETPROCADDRESS(libnss, "PK11_FreeSlot");
    PK11Authenticate       = (PK11_Authenticate) GETPROCADDRESS(libnss, "PK11_Authenticate");
    PK11SDRDecrypt         = (PK11SDR_Decrypt) GETPROCADDRESS(libnss, "PK11SDR_Decrypt");


    // Get the functions from PLC library
    PLBase64Decode     = ( PL_Base64Decode ) GETPROCADDRESS(libplc, "PL_Base64Decode");

    return 1;

}


/**
*     Initializes the NSS library and checks for the master password.
*  
*     @param(in) profilePath
*
*      @return    1 on success and 0 on error.
*
*/
int InitializeNSSLibrary(char *profilePath)
{
    IsNSSInitialized = 0;

    // Initialize the NSS library
    if( (*NSSInit) (profilePath) != 0 )
    {
        return 0;
    }

    IsNSSInitialized = 1;

    return 1;

}


/**
*     This function decrypts the encrypted data. First it performs base64 decoding and
*     then performs TRIPLE DES decryption.
*  
*     @param(in)  cryptData   encrypted data
*     @param(out) clearData   clear text data ( null terminated)
*
*      @return     1 on success and 0 on error.
*
*/
int DecryptSecretString(char *cryptData, char **clearData)
{
int decodeLen = 0;
int finalLen = 0;
char *decodeData = NULL;
char *finalData = NULL;

    // treat zero-length crypt string as a special case
    if(cryptData[0] == '\0')
    {
        *clearData  = (char*) malloc(1);
        **clearData = 0;
        return 1;
    }
    
    // base64 decoding
    
        if(  (Base64Decode(cryptData, &decodeData, &decodeLen) == 0) || (decodeData == NULL) )
        {
            return 0;
        }

        
        // PK11 decryption        
        if( (PK11Decrypt(decodeData, decodeLen, &finalData, &finalLen) == 0) || (finalData == NULL))
        {
            return 0;
        }

        // Decrypted string is not NULL terminated
        // So we will create new NULL terminated string here
    
        *clearData = (char*) malloc( finalLen + 1 );

        if( *clearData == NULL )
        {
            return 0;
        }

        memcpy(*clearData, finalData, finalLen);
        *(*clearData + finalLen) = 0;    // Null terminate the string....
        

    return 1;
}


/**
*     Performs base64 decoding of the encrypted data..
*  
*     @param(in)  cryptData   encrypted data
*     @param(out) decodeData  base64 decoded data
*     @param(out) decodeLen   length of base64 decoded data
*
*      @return     1 on success and 0 on error.
*
*/

int Base64Decode(char *cryptData, char **decodeData, int *decodeLen)
{
    int len = strlen( cryptData );
    int adjust = 0;

    // Compute length adjustment
    if (cryptData[len-1] == '=')
    {
      adjust++;
      if (cryptData[len-2] == '=')
          adjust++;
    }

    *decodeData = ( char *)(*PLBase64Decode)(cryptData, len, NULL);

    if( *decodeData == NULL )
    {
        return 0;
    }
    
    *decodeLen = (len*3)/4 - adjust;
    
    return 1;
}


/**
*     Performs TRIPLE DES decryption of base64 decoded data
*  
*     @param(in)  decodeData  base64 decoded data
*     @param(in)  decodeLen   length of base64 decoded data
*     @param(out) clearData   decrypted data
*     @param(out) finalLen    length of decrypted data
*
*      @return     1 on success and 0 on error.
*
*/
int PK11Decrypt(char *decodeData, int decodeLen, char **clearData, int *finalLen)
{
    PK11SlotInfo *slot = 0;
    SECStatus status;
    SECItem request;
    SECItem reply;

    // Find token with SDR key
    slot = (*PK11GetInternalKeySlot)();
    
    if (!slot)
    {
        return 0;
    }

    
    if ( (*PK11Authenticate)(slot, PR_TRUE, NULL) != SECSuccess)
    {
        return 0;
    }
    
    // Decrypt the string
    request.data = (unsigned char *)decodeData;
    request.len = decodeLen;
    reply.data = 0;
    reply.len = 0;

    status = (*PK11SDRDecrypt)(&request, &reply, NULL);

    if (status != SECSuccess)
    {
        return 0;
    }

    // WARNING : This string is not NULL terminated..
    *clearData = (char*)reply.data;
    *finalLen  = reply.len;

    // Free the slot
    (*PK11FreeSlot)(slot);

    return 1;
}