RSA cryptography problem

Just starting out? Need help? Post your questions and find answers here.
User avatar
RocketRider
User
User
Posts: 87
Joined: Tue Aug 19, 2008 11:01 am

RSA cryptography problem

Post 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
User avatar
RocketRider
User
User
Posts: 87
Joined: Tue Aug 19, 2008 11:01 am

Re: RSA cryptography problem

Post 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
PeterH
User
User
Posts: 28
Joined: Sun Apr 11, 2010 11:01 am

Re: RSA cryptography problem

Post 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.
User avatar
dhouston
Enthusiast
Enthusiast
Posts: 430
Joined: Tue Aug 21, 2007 2:44 pm
Location: USA (Cincinnati)
Contact:

Re: RSA cryptography problem

Post by dhouston »

http://davehouston.org
Mac Mini (Intel) 10.6.8 - iMac G4 (PPC) 10.4.11
Dell Dimension 2400 W98SE,W2K,XP,Vista,W7,Debian,Ubuntu,Kubuntu,Xubuntu,Fedora,Mandriva,Mint
(on swappable HDDs)
Vizio VTAB1008 - Android 3.1
MK808 miniAndroidPC (Android 4.1)
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Re: RSA cryptography problem

Post 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<<
Post Reply