Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 630
  • Last Modified:

.NET RSA CRYPTOSERVICEPROVIDER

I am having a problem when I run the following code. I need to get this up and running real quick.

Public Function Decrypt(ByVal args As String, ByVal privKey As String) As String Implements iDecrypt.Decrypt
     
        Dim plaintext As String
        Dim xmlDoc As New XmlDocument()
        xmlDoc.LoadXml(args)
       
        'Retrieve the Ciphertext from the xml doc recvd
        Dim ciphertext() As Byte
        ciphertext = Convert.FromBase64String(xmlDoc.DocumentElement.ChildNodes(0).InnerText())
       
        'Retrieve the Cipherkey from the xml doc recvd
        Dim cipherKey() As Byte
        cipherKey = Convert.FromBase64String(xmlDoc.DocumentElement.ChildNodes(1).InnerText)
     
        'Retrieve the CipherIV from the xml doc recvd
        Dim cipherIV() As Byte
        cipherIV = Convert.FromBase64String(xmlDoc.DocumentElement.ChildNodes(2).InnerText)
     
        'Load the private key into RSACryptoProvider and decrypt to get asym key & IV
        'Now, create a new RSA key and import the parameters
        Dim cspParam As New CspParameters()
        cspParam.Flags = CspProviderFlags.UseMachineKeyStore
        Dim rsa As RSACryptoServiceProvider
        rsa = New RSACryptoServiceProvider(cspParam)      

        rsa.FromXmlString(privKey)
       
       'Decrypt the plainKey
        Dim plainKey() As Byte
        plainKey = rsa.Decrypt(cipherKey, False)
       
        'Decrypt the plainIV
        Dim plainIV() As Byte
        plainIV = rsa.Decrypt(cipherIV, False)
       
        'Load the rc2CryptoProvider with these params just decrypted and decrypt the message
        Dim rc2 As New RC2CryptoServiceProvider()

        'Move the Memory Pointer to the Beginning of Memory Stream
        Dim mMemStr As New MemoryStream(ciphertext, True)
        mMemStr.Position = 0
        'Try to Decrypt values in the memory
        'First Create the Decryptor, By Passing Key and IV
        'Dim key(4) As Byte
        'key = plainKey
        'Dim IV(7) As Byte
        'IV = plainIV

        rc2.Key = plainKey
        rc2.IV = plainIV

        Dim mDecrypt As ICryptoTransform = rc2.CreateDecryptor()
        'Create Crypto Stream
        Dim mCSReader As New CryptoStream(mMemStr, mDecrypt, CryptoStreamMode.Read)
        Dim plainTextBytes() As Byte
        ReDim plainTextBytes(ciphertext.Length - 1)

        'Start decrypting.
        Dim decryptedCount As Integer
        decryptedCount = mCSReader.Read(plainTextBytes, 0, plainTextBytes.Length)
     
       'Close both streams.
        mMemStr.Close()
        mCSReader.Close()

        'Convert decrypted data to a string.
        'Let us assume that the original plaintext string was UTF8-encoded.
        Dim utf8 As UTF8Encoding
        Dim plainTextToSend As String

        utf8 = New UTF8Encoding()
        plainTextToSend = utf8.GetChars(plainTextBytes, 0, decryptedCount)

        'Return decrypted string.  
 
        Decrypt = plaintext

    End Function


I AM Getting the following error right after  rsa.FromXmlString(privKey)
Description : Bad Data
Number : 5
Source : mscorlib

Please HELP ASAP!
0
jainpriyanka
Asked:
jainpriyanka
1 Solution
 
culshajaCommented:
Seems like what you are passing in as the key might be wrong. Can you post what the XML looks like?

James :-)
0
 
jainpriyankaAuthor Commented:
Sorry but I figured it out I was encrypting the message but wasnt decrypting it properly (with the wrong key) I would like to give u half the points but im not sure how to split it up! pls let me know thnx. also for now i am having a .NET Email prb im not sure of ull be able to help if so i can just give u all the points then.
I have a network here at work. I am going to have my computer as the main Server. I have another computer that will be used to sending mail. Do i need to setup the SMTP server ? If so where on my computer or the Mail Server ? Im a newbie at this so pls HELP! Here is the code:

'Set the smtp server
SmtpMail.SmtpServer = "greg"

'Send the email
SmtpMail.Send(objMail)

ERROR MESSAGE:
An error occured while processing the email. Please review the following. Error sending email Could not access 'CDO.Message' object. Error String System.Web.HttpException: Could not access 'CDO.Message' object. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException (0x80040213): The transport failed to connect to the server. --- End of inner exception stack trace --- at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters) at System.RuntimeType.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParameters) at System.Web.Mail.LateBoundAccessHelper.CallMethod(Object obj, String methodName, Object[] args) --- End of inner exception stack trace --- at System.Web.Mail.LateBoundAccessHelper.CallMethod(Object obj, String methodName, Object[] args) at System.Web.Mail.CdoSysHelper.Send(MailMessage message) at System.Web.Mail.SmtpMail.Send(MailMessage message) at ClinicLogin.sendEmail.btnSend_Click(Object sender, EventArgs e) in C:\WEB_ROOT\WebForms\ClinicLogin\sendEmail.aspx.vb:line 164
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now