Link to home
Start Free TrialLog in
Avatar of mphillip85
mphillip85Flag for United States of America

asked on

RSACryptoServiceProvider - Need visual basic .net 2005 code to decrypt/encrypt a file.

need code to use the RSACryptoServiceProvider.  I have a public and private key with a secret key and need code to open / unlock the PGP file that i have.
Avatar of iHadi
iHadi
Flag of Syrian Arab Republic image

Hi mphillip85

The following methods encrypt and decrypt files according to the supplied keys:

    ''' <summary>
    ''' This method encrypts a file using RSA algorithm
    ''' </summary>
    ''' <param name="SrcFile">The path of the file you want to encrypt</param>
    ''' <param name="DestPath">The output file path</param>
    ''' <param name="KeyData">The Public and Private keys data</param>
    Public Sub Encrypt(ByVal SrcFile As String, ByVal DestPath As String, ByVal KeyData As String)
        Dim algo As RSACryptoServiceProvider = New RSACryptoServiceProvider()
        Dim SrcFileData As Byte() = File.ReadAllBytes(SrcFile)
        Dim DestFileData As Byte()

        ' import the public and private keys into the algorithm
        algo.FromXmlString(KeyData)
        DestFileData = algo.Encrypt(SrcFileData, False)
        File.WriteAllBytes(DestPath, DestFileData)
    End Sub

    ''' <summary>
    ''' This method decrypts a file using RSA algorithm
    ''' </summary>
    ''' <param name="SrcFile">The path of the file you want to decrypt</param>
    ''' <param name="DestPath">The output file path</param>
    ''' <param name="KeyData">The Public and Private keys data</param>
    Public Sub Decrypt(ByVal SrcFile As String, ByVal DestPath As String, ByVal KeyData As String)
        Dim algo As RSACryptoServiceProvider = New RSACryptoServiceProvider()
        Dim SrcFileData As Byte() = File.ReadAllBytes(SrcFile)
        Dim DestFileData As Byte()

        ' import the public and private keys into the algorithm
        algo.FromXmlString(KeyData)
        DestFileData = algo.Decrypt(SrcFileData, False)
        File.WriteAllBytes(DestPath, DestFileData)
    End Sub
Avatar of mphillip85

ASKER

how do I take a file that alerady has the private and public key hash and use it with this program?
Load it from the file and pass it in the KeyData parameter of the Encrypt and Decrypt methods. The following method gets the key data from the file and returns it as string:

    Public Function GetKeysFromFile(ByVal FilePath As String)
        Dim tmpstr As String

        tmpstr = File.ReadAllText(FilePath)
        Return tmpstr
    End Function

Will this work with PGP encrypted files?

What can I do or insert that will tell me that there is an error or failure in the unlocking process?
oh, and destpath is like "C:\" right?
It will work if the PGP file is using the same algorithm and the file has only hte encrypted data and no additional data.

If there was an error in the unlocking process an exception will be raised eg:

        string key = GetKeysFromFile("C:\Key.txt");

        Try
            Decrypt("C:\Src.txt", "C:\Dest.txt", key)
            MessageBox.Show("Successed")
        Catch
            MessageBox.Show("Failed")
        End Try
I seem to be having an xml problem:

The beginning of the key file is as stated below:

"-----BEGIN PGP PRIVATE KEY BLOCK-----

Version: FileCrypt eBusiness 3.5

Comment: http://www.veridis.com



The keys should be of the following format:

<RSAKeyValue><Modulus>1r5OOOKtZb4rAsLSiStPsynkcJwJkpjxnhvguHmFU4FCY9a3oXkqkqBOEefewy/DZ4LF9IRI0nvmxIYz3Y+9VEGPFX/CcBN7K3KaJv833b4bKrvIhBCFKsitB0CV7P8r7nLYqmpL1fANIwbMrcaNcZd74koACufhQxCuWsMiRLk=</Modulus><Exponent>AQAB</Exponent><P>7uY+vxw+5KZLTXn4gDSBPEfCdE4tL743tmIBEhAz4lTcV1SkylVwFldz0fvohbIcKS9G+/GZCkc499EB05fzZQ==</P><Q>5h1ow5skRnuOevDvOvcuC2n5yYM0tcygZaGDtQkcc2Vr1bXAB15p0a1NidO/py0Uo6XDSsTWfN0PYlgV6PCYxQ==</Q><DP>hGgiUMOz7ABq7gby/wOauTFAijul+bcgo7oPw9YtG31THifwqZ/O3uMbcz9+MVN1HjJw0kSmQLcW0OsLax3lrQ==</DP><DQ>rCieg5FjT1QLIdF1ubDKAwITPf7qhYUFD2QKDHw3i+H6DlY+x4SMOLGmjBIela01fsV6WUrwiVQ9uT7bjX3LXQ==</DQ><InverseQ>L5xokHHhsYJasIAGmfIQrYJW75MwdIYiNTTKoshz+D0+ohJPRFmlNsv+RcWpamtsVnduVAgc1GnIKw6diPPA/A==</InverseQ><D>MQaNcSs/DwVVaHH2At7ieTLNNnBJNzC3qLMQoM0NhTzzXTu9/J1e/M6gRDI9hy42XEFc1DAHOBaVXrSMzr5sKI8jXd7xUOcSzTQfSUBNz6W0+EbFzwUk1Q8aO/FkY5fDnIQ/TWBDAoUK9NFGpqqpP52EbbiCa3iwbESTM/Qj0CE=</D></RSAKeyValue>
ASKER CERTIFIED SOLUTION
Avatar of iHadi
iHadi
Flag of Syrian Arab Republic image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial