Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

.NET RSA CRYPTOSERVICEPROVIDER

Posted on 2004-03-29
2
Medium Priority
?
626 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 750 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

719 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