mphillip85
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.
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
Public Function GetKeysFromFile(ByVal FilePath As String)
Dim tmpstr As String
tmpstr = File.ReadAllText(FilePath)
Return tmpstr
End Function
ASKER
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?
What can I do or insert that will tell me that there is an error or failure in the unlocking process?
ASKER
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.tx t");
Try
Decrypt("C:\Src.txt", "C:\Dest.txt", key)
MessageBox.Show("Successed ")
Catch
MessageBox.Show("Failed")
End Try
If there was an error in the unlocking process an exception will be raised eg:
string key = GetKeysFromFile("C:\Key.tx
Try
Decrypt("C:\Src.txt", "C:\Dest.txt", key)
MessageBox.Show("Successed
Catch
MessageBox.Show("Failed")
End Try
These are some good urls to related topic:
http://cephas.net/blog/2004/09/03/pgp-decryption-using-c/
http://www.codeproject.com/csharp/sharpprivacy.asp
http://cephas.net/blog/2004/09/03/pgp-decryption-using-c/
http://www.codeproject.com/csharp/sharpprivacy.asp
ASKER
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 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>1r5O OOKtZb4rAs LSiStPsynk cJwJkpjxnh vguHmFU4FC Y9a3oXkqkq BOEefewy/D Z4LF9IRI0n vmxIYz3Y+9 VEGPFX/CcB N7K3KaJv83 3b4bKrvIhB CFKsitB0CV 7P8r7nLYqm pL1fANIwbM rcaNcZd74k oACufhQxCu WsMiRLk=</ Modulus><E xponent>AQ AB</Expone nt><P>7uY+ vxw+5KZLTX n4gDSBPEfC dE4tL743tm IBEhAz4lTc V1SkylVwFl dz0fvohbIc KS9G+/GZCk c499EB05fz ZQ==</P><Q >5h1ow5skR nuOevDvOvc uC2n5yYM0t cygZaGDtQk cc2Vr1bXAB 15p0a1NidO /py0Uo6XDS sTWfN0PYlg V6PCYxQ==< /Q><DP>hGg iUMOz7ABq7 gby/wOauTF Aijul+bcgo 7oPw9YtG31 THifwqZ/O3 uMbcz9+MVN 1HjJw0kSmQ LcW0OsLax3 lrQ==</DP> <DQ>rCieg5 FjT1QLIdF1 ubDKAwITPf 7qhYUFD2QK DHw3i+H6Dl Y+x4SMOLGm jBIela01fs V6WUrwiVQ9 uT7bjX3LXQ ==</DQ><In verseQ>L5x okHHhsYJas IAGmfIQrYJ W75MwdIYiN TTKoshz+D0 +ohJPRFmlN sv+RcWpamt sVnduVAgc1 GnIKw6diPP A/A==</Inv erseQ><D>M QaNcSs/DwV VaHH2At7ie TLNNnBJNzC 3qLMQoM0Nh TzzXTu9/J1 e/M6gRDI9h y42XEFc1DA HOBaVXrSMz r5sKI8jXd7 xUOcSzTQfS UBNz6W0+Eb FzwUk1Q8aO /FkY5fDnIQ /TWBDAoUK9 NFGpqqpP52 EbbiCa3iwb ESTM/Qj0CE =</D></RSA KeyValue>
<RSAKeyValue><Modulus>1r5O
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(DestPat
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(DestPat
End Sub