I need a asymmetric cryptography, because I need private key for decrypting and a public key for encrypting.
So I want to use the RSA cryptography to protect a string.
I get it to work, but now I can decrypt the string with the public key?
What I'm doing wrong?
Code: Select all
#PROV_RSA_FULL = 1
#CRYPT_NEWKEYSET = 8
#MS_DEF_PROV = "Microsoft Base Cryptographic Provider v1.0"
#MS_ENHANCED_PROV = "Microsoft Enhanced Cryptographic Provider v1.0"
#MS_STRONG_PROV = "Microsoft Strong Cryptographic Provider"
#CALG_RSA_KEYX = 41984
#CALG_RSA_SIGN = $2400
#CALG_RC4 = $6801
#CRYPT_VERIFYCONTEXT = -268435456
#SIMPLEBLOB = $01;
#PUBLICKEYBLOB = $06;
#PRIVATEKEYBLOB = $07;
#PLANTEXTKEYBLOB = $08;
#OPAQUEKEYBLOB = $09;
#PUBLICKEYBLOBEX = $0A;
#SYMMETRICWRAPKEYBLOB = $0B;
#CRYPT_EXPORTABLE = 1
If CryptAcquireContext_(@hProv, #Null, #MS_STRONG_PROV, #PROV_RSA_FULL, 0) = 0
CryptAcquireContext_(@hProv, #Null, #MS_STRONG_PROV, #PROV_RSA_FULL, #CRYPT_NEWKEYSET)
EndIf
If hProv
CryptGenKey_(hProv, #CALG_RSA_KEYX, #CRYPT_EXPORTABLE, @hSessionKey)
CryptExportKey_(hSessionKey, 0, #PUBLICKEYBLOB, 0, 0, @publicKeyLen)
publicKey=AllocateMemory(publicKeyLen)
CryptExportKey_(hSessionKey, 0, #PUBLICKEYBLOB, 0, publicKey, @publicKeyLen)
CryptExportKey_(hSessionKey, 0, #PRIVATEKEYBLOB, 0, 0, @privateKeyLen)
privateKey=AllocateMemory(privateKeyLen)
CryptExportKey_(hSessionKey, 0, #PRIVATEKEYBLOB, 0, privateKey, @privateKeyLen)
CryptDestroyKey_(hSessionKey)
string.s="Hallo12345678"
length=StringByteLength(string)
orglenth=length
cipherBlock=AllocateMemory(1024)
CopyMemory(@string, cipherBlock, length)
CryptImportKey_(hProv, publicKey, publicKeyLen,0,0, @hSessionKey2)
CryptEncrypt_(hSessionKey2, 0, 1, $40, cipherBlock, @length, 1024)
CryptDestroyKey_(hSessionKey2)
Debug PeekS(cipherBlock, length)
;CryptImportKey_(hProv,privateKey,privateKeyLen,0,0,@hSessionKey3)
CryptImportKey_(hProv, publicKey, publicKeyLen,0,0, @hSessionKey3)
CryptDecrypt_(hSessionKey3, 0, 1, $40, cipherBlock, @length)
CryptDestroyKey_(hSessionKey3)
Debug PeekS(cipherBlock, length)
FreeMemory(publicKey)
FreeMemory(privateKey)
FreeMemory(cipherBlock)
CryptReleaseContext_(hProv,0)
EndIf
Best regards,
RocketRider