Solved

.NET RSA CRYPTOSERVICEPROVIDER

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

830 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