Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2007-08-09
10
Medium Priority
?
359 Views
Last Modified: 2013-11-05
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.
0
Comment
Question by:mphillip85
  • 6
  • 4
10 Comments
 
LVL 13

Expert Comment

by:iHadi
ID: 19667660
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
0
 

Author Comment

by:mphillip85
ID: 19673923
how do I take a file that alerady has the private and public key hash and use it with this program?
0
 
LVL 13

Expert Comment

by:iHadi
ID: 19674048
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

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:mphillip85
ID: 19674083
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?
0
 

Author Comment

by:mphillip85
ID: 19674102
oh, and destpath is like "C:\" right?
0
 
LVL 13

Expert Comment

by:iHadi
ID: 19674158
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
0
 
LVL 13

Expert Comment

by:iHadi
ID: 19674182
0
 

Author Comment

by:mphillip85
ID: 19674260
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



0
 
LVL 13

Expert Comment

by:iHadi
ID: 19674273
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>
0
 
LVL 13

Accepted Solution

by:
iHadi earned 2000 total points
ID: 19674282
Frankly, I don't this will success easilly. It needs some research about FileCrypt's output files and keys.

Why don't you consider the veridis Developer products for integration: https://www.veridis.com/pgp/products/filecrypt-products-for-developers.html
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month12 days, 11 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question