How to encrypt bulk plain text using rsa asymmetric alg with parameters
I have one problem while encrypting and decrypting the message using RSA methods.
I have generated 2 keys public and private, which are stored into the database either 1024 or 2048 formats.
RSA KEY GENERATION:
Dim RSA As RSACryptoServiceProvider = New RSACryptoServiceProvider
Dim privateParams As RSAParameters
privateParams = RSA.ExportParameters(True)
Dim publicParams As RSAParameters
publicParams = RSA.ExportParameters(False)
If rblKeyPair.SelectedValue = "1024" Then
RSA = New RSACryptoServiceProvider(1024)
Else
RSA = New RSACryptoServiceProvider(2048)
End If
Dim privateKeys As System.String = RSA.ToXmlString(True)
Dim publicKeys As System.String = RSA.ToXmlString(False)
--------------
I have retrieved these 2 keys i have encrypted the message by the following ways
1. I converted the plain text into byte array
2. I encrypted this byte array using rsa encryption method with the help of receiver public key. Result will store in byte array format.
3. I converted this byte array to string type, then i stored this into database.
4. For plain text byte array i found hash values using SHA Algorithm with the help of sender private key.
5. I encrypted this hash values, which is in byte array format.
6. I converted this string format and stored into the database.
the code is as follows
RSA KEY ENCRYPTION:
Receiver public key:
publicKey = dsSelect.Tables(0).Rows(0).Item(0)
Sender private key:
privateKey = dsSelect.Tables(0).Rows(0).Item(0)
Dim cspParams As CspParameters = New CspParameters
cspParams.Flags = CspProviderFlags.UseMachineKeyStore
Dim rsaProvider As RSACryptoServiceProvider = New RSACryptoServiceProvider(cspParams)
'Dim rsa2 As New RSACryptoServiceProvider
rsaProvider.FromXmlString(publicKey)
Dim toencrypt() As Byte
Dim encrypted() As Byte
Dim encryptData As String
toencrypt = System.Text.Encoding.Unicode.GetBytes(txtText.Text)
encrypted = rsaProvider.Encrypt(toencrypt, False)
encryptData = System.Text.Encoding.Unicode.GetString(encrypted)
Dim result() As Byte
Dim signatureBlock() As Byte
Dim encryptPrivate As String
Dim sha As New SHA1CryptoServiceProvider
result = sha.ComputeHash(toencrypt)
Dim cspPrivate As CspParameters = New CspParameters
cspPrivate.Flags = CspProviderFlags.UseMachineKeyStore
Dim rsaPrivate As RSACryptoServiceProvider = New RSACryptoServiceProvider(cspPrivate)
'Dim rsaPriKey As New RSACryptoServiceProvider
rsaPrivate.FromXmlString(privateKey)
signatureBlock = rsaPrivate.Encrypt(result, False)
encryptPrivate = System.Text.Encoding.Unicode.GetString(signatureBlock)
Here it won't perform the encryption operation because it is displaying run time exception like baddata.
RSA DECRYPTION:
Receiver Privatekey:
privateKey = dsPrivate.Tables(0).Rows(0).Item(0)
Sender PublicKey:
publicKey = dsPublic.Tables(0).Rows(0).Item(0)
i have done reverse process for decryption. but i got Run time exception like Bad Data.
Dim cspPar As CspParameters = New CspParameters
cspPar.Flags = CspProviderFlags.UseMachineKeyStore
cspPar.ProviderName = dsPrivate.Tables(0).Rows(0).Item(0)
Dim rsaPriParams As RSACryptoServiceProvider = New RSACryptoServiceProvider(cspPar)
'Dim rsa1 As New RSACryptoServiceProvider
rsaPriParams.FromXmlString(privateKey)
Dim toDecrypt() As Byte
Dim decryp() As Byte
toDecrypt = System.Text.Encoding.Unicode.GetBytes(dsReceiver.Tables(0).Rows(0).Item(4))
decryp = rsaPriParams.Decrypt(toDecrypt, False)
while decrypting rsaPriParams.Decrypt method has thrown exception as Bad Data.
Can any body help me? Where i have done mistake and which the best way to solve this problem. It is very urgent for me.
Actually my requirement is as follows
Every person must have one public key and one private key.
If sender wants to send a message receiver first he must know the receiver's public key. Then the Sender encrypt his message using the receiver's public key and store it in database. Then Sender finds hash values for his plain text and encrypt this hash values using Sender private key and it is store into the database.
whenever receiver check his messages first receiver decrypt the encrypted message using his private key and finds the hash values for decrypted message.Then receiver decrypt the hash encrypted message,which contains hash values.
If these two hash values are same. Message will be displayed. otherwise some one hacks the message.
This is my requirement. If u have any suggestions please forward to following mail id:
jagan.penikalapati@gmail.com
jagan.5583@yahoo.com
jagan.penikalapati@yahoo.co.in (http://)

