We help IT Professionals succeed at work.

Encrypt a file with RSA in .net

I am working on an ecryption module for an app, and I need to be able to encrypt an entire file to send it over the network to the server.

The functionality I am looking for is similar to what gnu pgp provides (or the Windows counterpart, Cryptophane).

Problem I am running into is that RSA appears to only work up to a certain size (and not very big at that).

How would I use RSA to encrypt a 500k or 1mb file?

Worst case scenario, can it be used to encrypt 8192 bytes at a time so I can encrypt the chunks being sent to the server?

Thanks in advance.
Comment
Watch Question

Most Valuable Expert 2012
Top Expert 2014

Commented:
As this tutorial suggests, RSA is recommended for encrypting small amounts of data such as passwords
http://www.dreamincode.net/forums/showtopic67705.htm

Is using some other encryption an option?
Most Valuable Expert 2012

Author

Commented:
Other encryption is definitely an option.

As long as I can encrypt / decrypt using .net, I don't care what the encryption is. (stronger the better obviously!)
Most Valuable Expert 2012

Author

Commented:
oh... I also prefer asymmetric encryption that uses a public and private key pair.
Most Valuable Expert 2012
Top Expert 2014

Commented:
I have a utility class which i use for encryption/decryption in my projects. I can post the code if you are interested.
Most Valuable Expert 2012

Author

Commented:
Yes. Please do.
Most Valuable Expert 2012
Top Expert 2014
Commented:
Here you go




Imports System
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography
 
Public Class TripleDES
    Private key() As Byte = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}
    Private iv() As Byte = {65, 110, 68, 26, 69, 178, 200, 219}
 
    Public Function Encrypt(ByVal plainText As String) As Byte()
        ' Declare a UTF8Encoding object so we may use the GetByte
        ' method to transform the plainText into a Byte array.
        Dim utf8encoder As UTF8Encoding = New UTF8Encoding()
        Dim inputInBytes() As Byte = utf8encoder.GetBytes(plainText)
 
        ' Create a new TripleDES service provider
        Dim tdesProvider As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
 
        ' The ICryptTransform interface uses the TripleDES
        ' crypt provider along with encryption key and init vector
        ' information
        Dim cryptoTransform As ICryptoTransform = tdesProvider.CreateEncryptor(Me.key, Me.iv)
 
        ' All cryptographic functions need a stream to output the
        ' encrypted information. Here we declare a memory stream
        ' for this purpose.
        Dim encryptedStream As MemoryStream = New MemoryStream()
        Dim cryptStream As CryptoStream = New CryptoStream(encryptedStream, cryptoTransform, CryptoStreamMode.Write)
 
        ' Write the encrypted information to the stream. Flush the information
        ' when done to ensure everything is out of the buffer.
        cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
        cryptStream.FlushFinalBlock()
        encryptedStream.Position = 0
 
        ' Read the stream back into a Byte array and return it to the calling
        ' method.
        Dim result(encryptedStream.Length - 1) As Byte
        encryptedStream.Read(result, 0, encryptedStream.Length)
        cryptStream.Close()
        'Dim myutf As UTF8Encoding = New UTF8Encoding()
        'Return myutf.GetString(result)
        Return result
    End Function
 
    Public Function Decrypt(ByVal inputInBytes() As Byte) As String
        ' UTFEncoding is used to transform the decrypted Byte Array
        ' information back into a string.
        Dim myutf As UTF8Encoding = New UTF8Encoding()
 
        'Dim inputInBytes() As Byte = myutf.GetBytes(input)
 
        Dim utf8encoder As UTF8Encoding = New UTF8Encoding()
        Dim tdesProvider As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
 
        ' As before we must provide the encryption/decryption key along with
        ' the init vector.
        Dim cryptoTransform As ICryptoTransform = tdesProvider.CreateDecryptor(Me.key, Me.iv)
 
        ' Provide a memory stream to decrypt information into
        Dim decryptedStream As MemoryStream = New MemoryStream()
        Dim cryptStream As CryptoStream = New CryptoStream(decryptedStream, cryptoTransform, CryptoStreamMode.Write)
        cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
        cryptStream.FlushFinalBlock()
        decryptedStream.Position = 0
 
        ' Read the memory stream and convert it back into a string
        Dim result(decryptedStream.Length - 1) As Byte
        decryptedStream.Read(result, 0, decryptedStream.Length)
        cryptStream.Close()
        Return myutf.GetString(result)
    End Function
End Class
 
 
 
Public NotInheritable Class Encryption
 
    Private TripleDes As New TripleDESCryptoServiceProvider
 
    Private svKey As String = "justsomewordstobeusedasacryptionkey"
 
    Sub New(ByVal key As String)
 
        ' Initialize the crypto provider.
 
        TripleDes.Key = TruncateHash(key, TripleDes.KeySize \ 8)
 
        TripleDes.IV = TruncateHash("", TripleDes.BlockSize \ 8)
 
    End Sub
 
    Sub New()
 
        TripleDes.Key = TruncateHash(svKey, TripleDes.KeySize \ 8)
 
        TripleDes.IV = TruncateHash("", TripleDes.BlockSize \ 8)
 
    End Sub
 
    Private Function TruncateHash(ByVal key As String, ByVal length As Integer) As Byte()
 
        Dim sha1 As New SHA1CryptoServiceProvider
 
        ' Hash the key.
 
        Dim keyBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(key)
 
        Dim hash() As Byte = sha1.ComputeHash(keyBytes)
 
        ' Truncate or pad the hash.
 
        ReDim Preserve hash(length - 1)
 
        Return hash
 
    End Function
 
    Public Function EncryptData(ByVal plaintext As String) As String
 
        ' Convert the plaintext string to a byte array.
 
        Dim plaintextBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(plaintext)
 
        ' Create the stream.
 
        Dim ms As New System.IO.MemoryStream
 
        ' Create the encoder to write to the stream.
 
        Dim encStream As New CryptoStream(ms, TripleDes.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write)
 
        ' Use the crypto stream to write the byte array to the stream.
 
        encStream.Write(plaintextBytes, 0, plaintextBytes.Length)
 
        encStream.FlushFinalBlock()
 
        ' Convert the encrypted stream to a printable string.
 
        Return Convert.ToBase64String(ms.ToArray)
 
    End Function
 
    Public Function DecryptData(ByVal encryptedtext As String) As String
 
        ' Convert the encrypted text string to a byte array.
 
        Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedtext)
 
        ' Create the stream.
 
        Dim ms As New System.IO.MemoryStream
 
        ' Create the decoder to write to the stream.
 
        Dim decStream As New CryptoStream(ms, TripleDes.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write)
 
        ' Use the crypto stream to write the byte array to the stream.
 
        decStream.Write(encryptedBytes, 0, encryptedBytes.Length)
 
        decStream.FlushFinalBlock()
 
        ' Convert the plaintext stream to a string.
 
        Return System.Text.Encoding.Unicode.GetString(ms.ToArray)
 
    End Function
 
End Class

Open in new window

Commented:
I quoted CodeCruiser on this post, if he/she wishes to comment for points:

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_24845774.html#a25670865