Results 1 to 9 of 9

Thread: Load File into stream and Decrypt

  1. #1

    Thread Starter
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622

    Load File into stream and Decrypt

    I need to load a file into a varible type System.IO.Stream and then decrypt it (or decrypt it and then load it into a stream). I need to know whats the best encryption for the job. Baiscly I dont want to store the file on the users hard drive.

    Any one have any source or links that could help? Is this even possible?
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  2. #2

    Thread Starter
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    figured out how to load a file into a Stream now i just need to figure out a good encryption / decryption method
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  3. #3

    Thread Starter
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    using the class crypto.vb

    VB Code:
    1. Private Sub DecFile()
    2.         Dim fsRead As New FileStream("c:\pic.jpg", FileMode.Open)
    3.         Dim msEncrypted As New MemoryStream(fsRead.Length)
    4.         Dim msPlain As MemoryStream
    5.         Dim brRead As New BinaryReader(fsRead)
    6.  
    7.         Dim bytesRead As Byte() = brRead.ReadBytes(fsRead.Length)
    8.         msEncrypted.Write(bytesRead, 0, fsRead.Length)
    9.  
    10.         Dim x As New Crypto("password")
    11.  
    12.         msPlain = x.DecryptStream(msEncrypted)
    13.  
    14.         'write to fswrite
    15.  
    16.         Dim fsWrite As New FileStream("c:\orignalfile.jpg", FileMode.Create, FileAccess.Write)
    17.  
    18.         Dim decryptedFile() As Byte
    19.  
    20.         decryptedFile = msEncrypted.ToArray
    21.  
    22.         Dim I As Long
    23.  
    24.         For I = 0 To UBound(decryptedFile)
    25.             fsWrite.WriteByte(decryptedFile(I))
    26.         Next
    27.  
    28.         fsWrite.Close()
    29.         fsRead.Close()
    30.         msPlain.Close()
    31.         msEncrypted.Close()
    32.         brRead.Close()
    33.  
    34.     End Sub
    35.  
    36.     Private Sub EncFile()
    37.         Dim fsRead As New FileStream("c:\test.jpg", FileMode.Open)
    38.  
    39.         Dim msPlain As New MemoryStream(fsRead.Length)
    40.         Dim msEncrypted As MemoryStream
    41.         Dim brRead As New BinaryReader(fsRead)
    42.  
    43.         'Read File into msPlain
    44.         Dim bytesRead As Byte() = brRead.ReadBytes(fsRead.Length)
    45.         msPlain.Write(bytesRead, 0, fsRead.Length)
    46.  
    47.         'Encrypt msPlain
    48.  
    49.         Dim x As New Crypto("password")
    50.  
    51.         msEncrypted = x.EncryptStream(msPlain)
    52.  
    53.         'Write to fsWrite somehow
    54.  
    55.         Dim fsWrite As New FileStream("c:\streamout.jpg", FileMode.Create, FileAccess.Write)
    56.         Dim EncryptedFile() As Byte
    57.  
    58.         EncryptedFile = msEncrypted.ToArray
    59.  
    60.         Dim I As Long
    61.  
    62.         For I = 0 To UBound(EncryptedFile)
    63.             fsWrite.WriteByte(EncryptedFile(I))
    64.         Next
    65.  
    66.         fsWrite.Close()
    67.         fsRead.Close()
    68.         msPlain.Close()
    69.         msEncrypted.Close()
    70.         brRead.Close()
    71.  
    72.     End Sub

    was having trouble with decrypting the streams so I wrote this to test and the decryption function (in the class) is not working (i think) anyone know whats wrong?

    (how come .vb is not a vaild extension?)
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Umm , what VS.NET version have you got ? Because MS were talking about some differences in Cryptograph Class a while back .

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Try this file en/de-cryption .
    Attached Files Attached Files

  6. #6

    Thread Starter
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    So Is this right? (I want to load the encrypted file with out decrypting it to the users disk) Im using VS .NET 03.

    VB Code:
    1. Private Sub DecryptFile()
    2.             Try
    3.                 If password.Length > 8 Then
    4.                     password = password.Substring(0, 8)
    5.                 Else
    6.                     If password.Length < 8 Then
    7.                         Dim add As Integer = 8 - password.Length
    8.                         Dim i As Integer
    9.                         For i = 0 To add - 1
    10.                             password = password + i
    11.                         Next i
    12.                     End If
    13.                 End If
    14.                 Dim UE As New UnicodeEncoding
    15.                 Dim key As Byte() = UE.GetBytes(password)
    16.  
    17.                 Dim fsCrypt As New FileStream(textBox2.Text, FileMode.Open)
    18.  
    19.                 Dim RMCrypto As New RijndaelManaged
    20.  
    21.                 Dim cs As New CryptoStream(fsCrypt, RMCrypto.CreateDecryptor(key, key), CryptoStreamMode.Read)
    22.  
    23.                 Dim msDec As New MemoryStream
    24.  
    25.                 Dim data As Integer
    26.                 While (data = cs.ReadByte()) <> -1
    27.                     msDec.WriteByte(CByte(data))
    28.                 End While
    29.                 'Load Into picturebox
    30.  
    31.                 Picturebox1.Image = New Bitmap (msdec)                
    32.  
    33.                 msDec.Close()
    34.                 cs.Close()
    35.                 fsCrypt.Close()
    36.                 MessageBox.Show("Everything is OK", "OK")
    37.             Catch
    38.             End Try
    39.         End Sub 'DecryptFile
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  7. #7

    Thread Starter
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    I dont know whether there was some thing wrong with the code you gave me or I changed it by accident but I figured it out and got it working propertly. Now im gonna strip the code down and throw it into a class.
    Last edited by <ABX; Sep 1st, 2003 at 03:37 AM.
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  8. #8

    Thread Starter
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    {Class attached}

    Sample Usage

    form declarations
    VB Code:
    1. WithEvents Crypt as cCrypt ' change withEvents to dim if you 'dont want to trap the error event

    encryption code

    VB Code:
    1. Dim Crypt as new cCrypt
    2.  
    3. With Crypt
    4.     .Password = "password"
    5.     .PlainFile = "c:\test.txt" 'this is the file you want to encrypt
    6.     .EncryptedFile = "c:\encrypted test.txt" 'this is where the encrypted file is stored
    7.     .EncryptFile ' to Encrypt the file
    8. End with

    VB Code:
    1. Dim Crypt as new cCrypt
    2.  
    3. With Crypt
    4.     .Password = "password"
    5.     .PlainFile = "c:\decrypted test.txt" 'this is where you want the decrypted file to go
    6.     .EncryptedFile = "c:\encrypted test.txt" 'this is where the encrypted file is stored
    7.     .DecryptFile ' to decrypt the file
    8.     'If you want to look at a encrypted jpg w/o decrypting it to disk
    9.     'use this
    10.      Picturebox1.Image = .DecryptPicture
    11.      'this decrypts the file in memory
    12. End with
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  9. #9
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Good it's working now . Yes the proj is converted into VB.NET 2003 .

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