Page 1 of 1

RSA cryptography problem

Posted: Thu Feb 02, 2012 8:00 pm
by RocketRider
Hello,
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
I hope someone can help me.
Best regards,
RocketRider

Re: RSA cryptography problem

Posted: Fri Feb 03, 2012 7:51 pm
by RocketRider
I found the problem, it is not my fault, it is windows. Windows saves the RSA keys and use them to decrypt it :(
http://blogs.msdn.com/b/alejacma/archiv ... asics.aspx

If Windows should not save the Key use #CRYPT_VERIFYCONTEXT (-268435456).

Code: Select all

If CryptAcquireContext_(@hProv, #Null, #MS_STRONG_PROV, #PROV_RSA_FULL, #CRYPT_VERIFYCONTEXT) = 0
  CryptAcquireContext_(@hProv, #Null, #MS_STRONG_PROV, #PROV_RSA_FULL, #CRYPT_NEWKEYSET|#CRYPT_VERIFYCONTEXT)
EndIf

Re: RSA cryptography problem

Posted: Fri Feb 17, 2012 11:49 pm
by PeterH
This is madly nice. Unfortunately I'm a bit too dumb/unskilled to use it. I bet that some functions for encrypting and decrypting as well as loading (or even generating if possible) certificates and whatnot would really put this on the map. Encryption is always fun, especially if it's simple to use yet still as powerful.

Re: RSA cryptography problem

Posted: Sat Feb 18, 2012 1:09 am
by dhouston

Re: RSA cryptography problem

Posted: Mon Feb 20, 2012 12:51 pm
by Num3
Hi again, i'm coming from the RSA2048 thread...

I tried your code, but i haven't had any luck signing a message with a private key.
I am unable to load the bloody thing! always throws an error, maybe you can help me!

Here's my code:

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_SHA1 = $8004   
#CALG_RC4 = $6801   

#CRYPT_VERIFYCONTEXT = -268435456

#SIMPLEBLOB      =    $01;
#PUBLICKEYBLOB   =    $06;
#PRIVATEKEYBLOB   =   $07;
#PLANTEXTKEYBLOB   =   $08;
#OPAQUEKEYBLOB   =   $09;
#PUBLICKEYBLOBEX   =   $0A;
#SYMMETRICWRAPKEYBLOB   =   $0B;
#AT_SIGNATURE = 2
#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
    
  file.s=OpenFileRequester("Chave Privada","*.w32","W32 Certificate (*.w32)|*.w32",0) ; Load a saved Private Key
  If file
    If ReadFile(0, file)
      length = Lof(0)                            ; get the length of opened file
      PrivateKey = AllocateMemory(length)         ; allocate the needed memory
      If PrivateKey
        PrivateKeybytes = ReadData(0, PrivateKey, length)   ; read all data into the memory block
        Debug "Number of bytes read: " + Str(PrivateKeybytes)
      EndIf
      CloseFile(0)
    EndIf
    
   EndIf 
    
    string.s="2008-03-10;2008-03-10T15:58:00;FT 1/1;28.07;"
    length=StringByteLength(string)
    orglenth=length
    cipherBlock=AllocateMemory(1024)
    CopyMemory(@string, cipherBlock, length)
    
    If CryptImportKey_(hProv,@PrivateKey,PrivateKeybytes,#Null,#Null,@hkey) 
      If CryptCreateHash_(hProv, #CALG_SHA1, 0, 0, @hHash) ; SHA1 Mandatory for this one
        If CryptHashData_(hHash,@cipherBlock,length, 0)
          If CryptSignHash_(hHash, #AT_SIGNATURE, #Null, 0, #Null, @size)
            cipherBlock=AllocateMemory(size)
            Debug size
            CopyMemory(@string, cipherBlock, length)
            CryptSignHash_(hHash, #AT_SIGNATURE, #Null, 0, @cipherBlock, @dwSigLen)
            Debug PeekS(cipherBlock) ;
          EndIf
        EndIf
      EndIf 
    Else
      Debug -1
      ;ErrorMessage()
    EndIf
    
    FreeMemory(cipherBlock)
    FreeMemory(PrivateKey)
    CryptReleaseContext_(hProv,0)
  EndIf
The W32 certificate i'm using is at my Dropbox >>here<<