Solved

.NET RSA CRYPTOSERVICEPROVIDER

Posted on 2004-03-29
2
613 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
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
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 video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now