Solved

.NET RSA CRYPTOSERVICEPROVIDER

Posted on 2004-03-29
2
620 Views
Last Modified: 2008-01-09
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
Comment
Question by:jainpriyanka
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 7

Accepted Solution

by:
culshaja earned 250 total points
ID: 10711567
Seems like what you are passing in as the key might be wrong. Can you post what the XML looks like?

James :-)
0
 

Author Comment

by:jainpriyanka
ID: 10718026
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display

738 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