Hi Harpeet
It seems to me that changing the design of your application will fix the problem. Changing the underlying algorithm is not likely to be as effective.
All cryptographic systems follow the same 3 steps:
1. Generate keys
2. Exchange keys
3. Use keys
In your initial design, you mixed up steps 1 and 2 and tried to use the same public/private key pair for two different applications. Your used the Microsoft-supplied keys database to store the key pairs. As you discovered, this is not a very reliable design.
As a general rule, always use symmetric encryption to protect the privacy of messages because:
1. Symmetric key encryption is quicker than public key encryption
2. Key management is easier to implement
Here's some symmetric key code that uses the Microsoft the RijndaelManaged class to protect the privacy of messages.
1. Generate keys
Imports System
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography
Public Class Crypto
Structure stKeys ' Stores symmetric key and initialisation vector
Dim key() As Byte
Dim IV() As Byte
End Structure
Dim RM As New RijndaelManaged ' New instance of the RijndaelManaged class
Dim keys As New stKeys ' Create an instance of the Symmetric Keys structure
Sub New()
'Create a new symmetric key and a new initialization vector for this session
RM.GenerateKey()
RM.GenerateIV()
'Copy the session key and initialisation vector into byte arrays
keys.key = RM.Key
keys.IV = RM.IV
End Sub
2. Exchange keys
Make a note of the values of Key and IV and hard code them into your programs.
3. Use keys
Each program encrypts / decrypts data using hard-coded symmetric keys. Because both applications use exactly the same keys there cannot be any problems with user login ID, operating system version, .NET Framework version, or anything else to do with your mate's machine.
I leave it to you to finesse the stream IO. Hint: a VB string can contain approximately 2 billion characters.
#Region "Symmetric Key Encryption helpers"
Sub SymmetricEncrypt(ByVal strFileName As String, ByVal plainText As String)
'Create a symmetrically encrypted (File) stream and write some data to it
Try
'Create a file stream
Dim fs As New FileStream(strFileName, FileMode.OpenOrCreate, FileAccess.Write)
'Create an encrypted stream that writes to the underlying file stream
Dim CryptStream As New CryptoStream(fs, RM.CreateEncryptor(RM.Key, RM.IV), CryptoStreamMode.Write)
'Create a StreamWriter to write to the encrypted stream.
Dim SWriter As New StreamWriter(CryptStream)
'Write the plain text string to the encrypted stream
SWriter.Write(plainText)
' Close everything
SWriter.Close()
CryptStream.Close()
fs.Close()
Catch ex As Exception
MessageBox.Show("Error : " & ex.ToString, "Crypto Object", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Function SymmetricDecrypt(ByVal strfileName As String) As String
'Read and return the contents of a symmetrically encrypted (File) stream
Dim plainText As String
Try
'Create a file stream
Dim fs As New FileStream(strfileName, FileMode.OpenOrCreate, FileAccess.Read)
'Create an encrypted stream that reads from the underlying file stream
Dim CryptStream As New CryptoStream(fs, RM.CreateDecryptor(RM.Key, RM.IV), CryptoStreamMode.Read)
'Create a StreamReader to read the encrypted stream
Dim SReader As New StreamReader(CryptStream)
' Read the encrypted stream and convert into plain text
plainText = SReader.ReadToEnd()
' Close everything and return the plain text string
SReader.Close()
CryptStream.Close()
fs.Close()
Catch ex As Exception
MessageBox.Show("Error : " & ex.ToString, "Crypto Object", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Return (plainText)
End Function
#End Region
End Class
-R