Re: Encrypt / Sign ? Not really sure



I didn't read your code thoroughly, but I think you're dealing with a conceptual error here.

Asymmetric encryption is generally used when you want others to be able to send you stuff that only you can read. Hence you ENCRYPT with your public key (the one you give out) and you DECRYPT with the private key (the one only you know).

However, you sound like you want to be the only one with either key (only you can send, only you can receive). In that case, you may be better off with symmetric encryption which uses the same key for both encryption and decryption. It's easier to manage (half the keys) and runs faster.

If switching keys doesn't do the trick, I have some test bench code that might help should you need it.

Rory Becker wrote:
Ok
I need to put a small amount of inforamtion on a public server.
This information should be encoded so as to make it less than easily read by a human.
I need a program I write to be able to access this info and decrypt it.
This information needs to be checkable as having been created by me and not messed with by someone else.

I remember learning about Public-Private Key encryption at Uni (Some 8-10 years ago - it all made some kind of sense then.) I remember that once a private and public key are created, they act more or less like the ying and yang of each other.

Given an encryption algorithm, if you use 1 key to encrypt then the other is the key to the decryption.

So I thought I'd encrypt my data using my private key and then have my program decrypt it with my public key.
I found RSACryptoServiceProvider and set to work creating the following code:
-------------------------------------------------------------
Imports System
Imports System.Security.Cryptography
Imports System.Text
Public Class Cryptography
Private Shared mPublicKey As String
Private Shared mPrivateKey As String

' This function used only once to generate the Constants above
Public Shared Sub GenerateKeys()
Dim RSA As RSACryptoServiceProvider = New RSACryptoServiceProvider()
mPublicKey = RSA.ToXmlString(False) ' gets the public key
mPrivateKey = RSA.ToXmlString(True) ' gets the private key
End Sub

' This function should work but throws a runtime error talking about bad data
Public Shared Sub TestKeys()
Call GenerateKeys()
Dim StartString As String = "Hello EveryBody"
Debug.Print(String.Format("Start String:'{0}'", StartString))
Dim EncryptedString As String = EncryptString(StartString, mPrivateKey)
Dim EndString As String = DecryptString(EncryptedString, mPublicKey)
Debug.Print(String.Format("End String:'{0}'", EndString))
End Sub

' Utility Funcs
Private Shared Function EncryptString(ByVal StringToEncode As String, ByVal KeyInfo As String) As String
Dim RSAEncoder As New RSACryptoServiceProvider()
RSAEncoder.FromXmlString(KeyInfo)
Dim DecodedBytes As Byte() = Encoding.Unicode.GetBytes(StringToEncode)
Dim EncodedBytes As Byte() = RSAEncoder.Encrypt(DecodedBytes, False)
Return Convert.ToBase64String(EncodedBytes)
End Function
Private Shared Function DecryptString(ByVal EncodedString As String, ByVal KeyInfo As String) As String
Dim RSADecoder As New RSACryptoServiceProvider()
RSADecoder.FromXmlString(KeyInfo)
Dim EncodedBytes As Byte() = Convert.FromBase64String(EncodedString)
Dim DecodedBytes As Byte() = RSADecoder.Decrypt(EncodedBytes, False)
Return Encoding.Unicode.GetString(DecodedBytes)
End Function
End Class
-------------------------------------------------------------

However I seem to get a "Bad Data" Cryptographic Exception

Can some one tell me where I'm going wrong?

Any help gratefully recieved.

Thanks

--
Rory


.



Relevant Pages

  • Re: encrypting/ decrypting with RSA
    ... should not be able to distinguish encryption of two messages of his/her own ... otherwise semantic security is totally compromised*. ... > Function RSADecrypt(ByVal instring As String) As String ... > Dim RSA As RSACryptoServiceProvider = New ...
    (microsoft.public.dotnet.security)
  • Re: Encrypt My.Settings setting?
    ... dim EncClass as new Encryption ... dim txtPlainTextPassword as string = "ThisIsMyNewPasswordSoThere!" ... Here's the Encryption Class... ... ' Create the encoder to write to the stream. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Asymetric Encryption - What am I doing wrong?
    ... > Encryption works fine, but decryption fails with 'Bad Key' every time. ... > Public Function EncryptText(ByVal sToEncrypt As String) As String ... > Dim RSA As New RSACryptoServiceProvider ...
    (microsoft.public.dotnet.security)
  • Re: Database Connectivity
    ... String, ByVal vstrEncryptionKey As String) As String ... Dim bytValue() As Byte ... Dim intLength As Integer ... with my encryption table. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Database Connectivity
    ... String, ByVal vstrEncryptionKey As String) As String ... Dim bytValue() As Byte ... Dim intLength As Integer ... I created my own "encryption array" and flush out certain chars with others ...
    (microsoft.public.dotnet.languages.vb)