Results 1 to 3 of 3

Thread: Encryption !!!

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Encryption !!!

    I know that I have to use System.Security.CreateEncryptor
    but can someone plz give me an example?
    Lets say I have a text file and I want to encrypt the data in the file by using a "key" for the enctyption. How can I do that?

    I will appriciate if anyone can show a little example for me, I cant figure it out
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2
    Lively Member
    Join Date
    May 2002
    Posts
    94

    Have Fun with this one

    I saw this thread and it made me think, so after a few hours I came up with this....

    Create a new vb app and add a module, erase all the pre generated code and place this code inside... then on the properties of the form set the "startup object" to point to sub main

    Code:
    Imports System
    Imports System.Text
    Imports System.Security.Cryptography
    
    Module Module1
    
      Private RSA As New RSACryptoServiceProvider()
      Private KeyInfo As RSAParameters = RSA.ExportParameters(False)
      Private AE As ASCIIEncoding = New ASCIIEncoding()
    
      Sub Main()
        Dim StrToEncode As String = "This is the text that will be encrypted."
        Dim EncodedStr() As Byte ' This is the Array that will hold the Encrypted Data
    
    
        ' Display the Test to be encrypted
        MsgBox("String To Be Encrypted:" & vbCrLf & vbCrLf & StrToEncode)
    
        ' Encode the Data
        EncodedStr = DataEncrypt(StrToEncode)
    
        ' Do something with this Encrypted Data
        MsgBox("String Encoded:" & vbCrLf & vbCrLf & AE.GetString(EncodedStr) & vbCrLf & vbCrLf)
    
        ' Decode the Data
        StrToEncode = DataDecrypt(EncodedStr)
    
        'Display the Decrypted String to a Message Box
        MsgBox("String Decoded:" & vbCrLf & vbCrLf & StrToEncode)
      End Sub
    
      Private Function DataEncrypt(ByVal RawData As String) As Byte()
        Dim ByteArray() As Byte = AE.GetBytes(RawData)
        DataEncrypt = RSA.Encrypt(ByteArray, False)
      End Function
    
      Private Function DataDecrypt(ByVal RawData As Byte()) As String
        Dim ByteArray() As Byte = RSA.Decrypt(RawData, False)
        DataDecrypt = AE.GetString(ByteArray)
      End Function
    
      Private Function GetParamVal(ByVal RawData As Byte()) As String
        Dim i As Integer = 0
        GetParamVal = "{"
        For i = 0 To UBound(RawData)
          If i = UBound(RawData) Then
            GetParamVal = GetParamVal & RawData(i)
          Else
            GetParamVal = GetParamVal & RawData(i) & ", "
          End If
        Next
        GetParamVal = GetParamVal & "}"
      End Function
    
    End Module
    I didn't have the time to write the extra code that needs to save the "KeyInfo" data but I wrote a function that can turn each member of the class into a string for storage just call the GetParamVal function with the KeyInfo.propertyname and it will return the property in a string type, if you wish to access the encrypted data you will need the exact keyinfo data when the string was encrypted.

    Hope this helps, if not well at least I learned something about encryption....

  3. #3
    Thelonius
    Guest
    Another thing you could do is use the HashTable class to create your own encryption scheme.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width