PGP Encyption and decryption

i am creating a desktop application where i want to use PGP for encryption and decryption of files (.txt, .sql etc).
Can anyone please help me to do this?
It would be nice if i get sample code.

Thanks in advance!

[code]Function PGPdecrypt(ToDecrypt as String, optional myPrivateKey as string) As String

	  // Description:   Decrypt input-string with parameter myPrivateKey
	  
	  Try
	    		    
	    If myPrivateKey <> "" Then
	      
	      Dim sToDecrypt As String = DecodeHex(ToDecrypt)
	      Dim msg As New MemoryBlock(Len(sToDecrypt)) 
	      msg = sToDecrypt
	      
	      Dim decryptedData As MemoryBlock = Crypto.RSADecrypt(msg, myPrivateKey)
	      If decryptedData <> Nil Then
	        Return decryptedData
	      Else
	        Return ""
	      End If
	      
	    Else
	      Return ""
	    End If
	    
	    
	  Catch err As Runtimeexception
	    
	    Return "" 
	    
	  End Try
	End Function
	
	// ----------------------------
	
			Function PGPencrypt(ToEncrypt as String, optional myPublicKey as String) As String
	  // Description:   Encrypt input-string with parameter myPublicKey 
	  
	  // WARNING :  max length of 'ToEncrypt' = 86 characters
	  
	  Try
	    
    
	    If myPublicKey <> "" Then
	      
	      Dim msg As New MemoryBlock(Len(ToEncrypt))
	      // max length of input for crypto.RSAEncrypt seems to be 86 characters.
	      // so if my input is more than 86 characters and RuntimeException should be raised,
	      msg = DefineEncoding(Left(ToEncrypt,86), Nil)
	      
	      Dim encryptedData As MemoryBlock = Crypto.RSAEncrypt(msg, myPublicKey)
	      If encryptedData <> Nil Then
	        Return EncodeHex(encryptedData)
	      Else
	        Return ""
	      End If
	      
	    Else
	      Return ""
	    End If
	    
	  Catch err As Runtimeexception
	    
	    Return "" 
	    
	  End Try
	  
	End Function[/code]

Hi, Joost Rongen
Thanks for reply. i have tried this code but getting an Crypto exception.
Error Number = 1
Message = BER decode error
Reason = BER decode error

Ah, forgot to tell you that you need to generate a key-pair first

[code]Dim privateKey As String
Dim publicKey As String

If Crypto.RSAGenerateKeyPair( 1024, privateKey, publicKey ) Then
// 1024-bit private and public keys were generated
End If[/code]

what are the benefits, if the above solution works apparently well w/o additional cost?

Just a spammer…

2 Likes

yet another reason to close old threads :slight_smile: