HelpAnyone have some experience with the Windows Crypto Api

Posts 12 of 2 · Page 1 of 1
Anyone have some experience with the Windows Crypto Api
I need a RSA public key encryption function, i'm new to the Windows Crypto Api

The encryption is using a public key PEM.RSA
# Base64 encoded encrypted data

I’ve written all the below code my self but can’t get it to work, if someone as a simple dll or c++ code and can provided me with detail information on how to compile that code, I will be grateful.

Code:
--constant serverPublicKey ="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDEmgEmQcgLd0mvWqL6AKmhzj" & "JfZoAmZC0PUmG8K9CB1Ml68P00S3eU+TSL5aG8Mg3Tipvs02gC2veC10knRi7r" & "EsUwL8+h22EsjnpKZ/7K5YV9cefryTMnS0x4QGZbSkdPz/rLh0uGwk8Zu0cEKb" & "xQyvd3+pSmqZ5/ZQGaFjm9TQIDAQAB\n"

constant serverPublicKey ="-----BEGIN PUBLIC KEY-----\n" & "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDEmgEmQcgLd0mvWqL6AKmhzj" & "JfZoAmZC0PUmG8K9CB1Ml68P00S3eU+TSL5aG8Mg3Tipvs02gC2veC10knRi7r" & "EsUwL8+h22EsjnpKZ/7K5YV9cefryTMnS0x4QGZbSkdPz/rLh0uGwk8Zu0cEKb" & "xQyvd3+pSmqZ5/ZQGaFjm9TQIDAQAB\n" & "-----END PUBLIC KEY-----"

constant crypt32 = registerw32Library("Crypt32.dll")

constant xCryptBinaryToString = registerw32Function(crypt32, "CryptBinaryToStringA", {C_POINTER,C_LONG,C_LONG,C_POINTER,C_POINTER}, C_INT)
constant xCryptStringToBinary = registerw32Function(crypt32, "CryptStringToBinaryA", {C_POINTER,C_LONG,C_LONG,C_POINTER,C_POINTER,C_POINTER,C_POINTER}, C_INT)

constant xCryptDecodeObjectEx = registerw32Function(crypt32, "CryptDecodeObjectEx", {C_LONG, C_POINTER,C_POINTER,C_LONG,C_LONG,C_POINTER,C_POINTER,C_POINTER}, C_INT)


constant CRYPT_STRING_BASE64= #0001
constant CRYPT_STRING_NOCRLF= #40000000
constant CRYPT_STRING_NOCR =#80000000

constant X509_ASN_ENCODING = 1
constant CRYPT_ENCODE_ALLOC_FLAG = #8000


--typedef struct _CRYPT_BIT_BLOB {
--  DWORD cbData;
--  BYTE  *pbData;
--  DWORD cUnusedBits;
--} CRYPT_BIT_BLOB, *PCRYPT_BIT_BLOB;

constant
-- Type CRYPT_BIT_BLOB
cbData1       = w32allot( DWord ),
pbData1       = w32allot( Byte ),
cUnusedBits  = w32allot( DWord ),
SIZEOF_CRYPT_BIT_BLOB  = w32allotted_size()

--typedef struct _CRYPTOAPI_BLOB {
--  DWORD cbData;
--  BYTE  *pbData;
--} CRYPT_INTEGER_BLOB, *PCRYPT_INTEGER_BLOB, CRYPT_UINT_BLOB, *PCRYPT_UINT_BLOB, CRYPT_OBJID_BLOB, *PCRYPT_OBJID_BLOB, CERT_NAME_BLOB, CERT_RDN_VALUE_BLOB, *PCERT_NAME_BLOB, *PCERT_RDN_VALUE_BLOB, CERT_BLOB, *PCERT_BLOB, CRL_BLOB, *PCRL_BLOB, DATA_BLOB, *PDATA_BLOB, CRYPT_DATA_BLOB, *PCRYPT_DATA_BLOB, CRYPT_HASH_BLOB, *PCRYPT_HASH_BLOB, CRYPT_DIGEST_BLOB, *PCRYPT_DIGEST_BLOB, CRYPT_DER_BLOB, PCRYPT_DER_BLOB, CRYPT_ATTR_BLOB, *PCRYPT_ATTR_BLOB;

constant
-- Type CRYPT_BIT_BLOB
cbData2       = w32allot( DWord ),
pbData2       = w32allot( Byte ),
SIZEOF_CRYPT_OBJID_BLOB  = w32allotted_size()


--typedef struct _CRYPT_ALGORITHM_IDENTIFIER {
--  LPSTR            pszObjId;
--  CRYPT_OBJID_BLOB Parameters;
--} CRYPT_ALGORITHM_IDENTIFIER, *PCRYPT_ALGORITHM_IDENTIFIER;

constant
-- Type CRYPT_ALGORITHM_IDENTIFIER
pszObjId       = w32allot( Lpsz ),
Parameters = w32allot( SIZEOF_CRYPT_OBJID_BLOB ),
SIZEOF_CRYPT_ALGORITHM_IDENTIFIER  = w32allotted_size()


--typedef struct _CERT_PUBLIC_KEY_INFO {
--  CRYPT_ALGORITHM_IDENTIFIER Algorithm;
--  CRYPT_BIT_BLOB             PublicKey;
--} CERT_PUBLIC_KEY_INFO, *PCERT_PUBLIC_KEY_INFO;

constant
-- Type CERT_PUBLIC_KEY_INFO
Algorithm = w32allot( SIZEOF_CRYPT_ALGORITHM_IDENTIFIER ),
PublicKey = w32allot( SIZEOF_CRYPT_BIT_BLOB ),
SIZEOF_CERT_PUBLIC_KEY_INFO  = w32allotted_size()



function CryptDecodeObjectEx(atom pderPubKey, atom derPubKeyLen)

     --Decode from DER format to CERT_PUBLIC_KEY_INFO

    --if ( !CryptDecodeObjectEx( X509_ASN_ENCODING, X509_PUBLIC_KEY_INFO, derPubKey, derPubKeyLen,
    --                           CRYPT_ENCODE_ALLOC_FLAG, NULL, &publicKeyInfo, &publicKeyInfoLen ) )
    --{
    --    fprintf( stderr, "CryptDecodeObjectEx 1 failed. Err: %p\n", GetLastError() );
    --    return -1;
    --}


--X509_PUBLIC_KEY_INFO
--(LPCSTR) 8
--The pvStructInfo parameter is a pointer to a CERT_PUBLIC_KEY_INFO structure.

  atom lpszStructType

  atom  mset,publicKeyInfo,ppublicKeyInfoLen
  mset = w32new_memset()
  publicKeyInfo  = w32acquire_mem(mset, SIZEOF_CERT_PUBLIC_KEY_INFO)
  ppublicKeyInfoLen=allocate(4)
  lpszStructType=allocate_string("8")


  if w32Func( xCryptDecodeObjectEx, { X509_ASN_ENCODING, lpszStructType, pderPubKey, derPubKeyLen, CRYPT_ENCODE_ALLOC_FLAG,0, publicKeyInfo, ppublicKeyInfoLen } )=0 then

    ? w32Func(xGetLastError,{})
  	puts(1,"error CryptDecodeObjectEx\n")
  end if

  return 0
end function

function toBase64(sequence st)
  atom st_add,dst_add,dword_add
  integer size
  sequence res
  st_add=allocate_string(st)
  dst_add=allocate(4024)
  dword_add=allocate(4)

  if w32Func( xCryptBinaryToString, { st_add, length(st), CRYPT_STRING_BASE64, dst_add, dword_add } )<=0 then
  	puts(1,"error tobase64\n")
  end if	

  size=bytes_to_int( { peek(dword_add), peek(dword_add+1), peek(dword_add+2), peek(dword_add+3) } )
  --?size
  res=""
  for i=0 to size-2 do
   	 res&=peek(dst_add+i)
  end for

  free(st_add)
  free(dst_add)
  free(dword_add)
  return res
end function


function fromBase64(sequence st)
  atom st_add,dst_add,dword_add
  integer size
  sequence res
  st_add=allocate_string(st)
  dst_add=allocate(4024)
  dword_add=allocate(4)

  if w32Func( xCryptStringToBinary, { st_add, length(st), CRYPT_STRING_BASE64, dst_add, dword_add,0,0 } )<=0 then
  	puts(1,"error frombase64\n")
  end if	

  size=bytes_to_int( { peek(dword_add), peek(dword_add+1), peek(dword_add+2), peek(dword_add+3) } )
  --?size
  res=""
  for i=0 to size-1 do
   	 res&=peek(dst_add+i)
  end for

  free(st_add)
  free(dst_add)
  free(dword_add)
  return res
end function



function pem_to_der(sequence st)
  atom st_add,dst_add,dword_add
  integer size
  sequence res
  st_add=allocate_string(st)
  dst_add=allocate(4024)
  dword_add=allocate(4)

  poke4(dword_add,4024)

  if w32Func( xCryptStringToBinary, { st_add, 0, 0, dst_add, dword_add,0,0 } )=0 then
  	puts(1,"error frombase64\n")
  end if	

  size=bytes_to_int( { peek(dword_add), peek(dword_add+1), peek(dword_add+2), peek(dword_add+3) } )
  --?size
  res=""
  for i=0 to size-1 do
   	 res&=peek(dst_add+i)
  end for

  free(st_add)
  --free(dst_add)
  free(dword_add)
  return {dst_add,size}
end function



sequence der
der=pem_to_der(serverPublicKey)
?der
--puts(1,fromBase64(toBase64("testing base64 encoding")))
puts(1,"----\n")
--?frompemtoBase64(serverPublicKey)
?CryptDecodeObjectEx(der[1],der[2])

---	return w32Func( xGetAsyncKeyState, { key } )
Solved using Crypto++

Code:
   
RSAFunction rs;
   Integer it("138058324507555049234246734000441140752323800141033219108446290424209651149061550256257123074054375779322028941830252756690799794787415386877876745524597141573809959460210645347060554401228507084399841512197648738006400503980279149209427806825118624928692857963634531857449103468249102954313978230908185591117");
    
   rs.SetModulus(it);
   rs.SetPublicExponent(65537);
  
   //take the above information writes to a file
   HexEncoder  pubFile(new FileSink("pkey.txt"));
   rs.DEREncode(pubFile);
   pubFile.MessageEnd();
 
   //perform encryption
   FileSource pubFile2("pkey.txt", true, new HexDecoder);
   RSAES_PKCS1v15_Encryptor pub(pubFile2);
 
   RandomPool randPool;
  
   std::string result;
   StringSource("message to be encyrpted", true, new PK_EncryptorFilter(randPool,   pub,    new Base64Encoder(new StringSink(result))));
Posts 12 of 2 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?