Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

What cryptography method I would use for the encryption AND decryption of a file via a specified key? (I searched on the forum for this topic, but what I'm looking for didn't come up)

 

Example:

 

Program A encrypts file A using the key wiehr234hih

 

Program A outputs (encrypted) file B

 

User A does not know the key, but tries the key 293ywah

 

Program B is decrypted, but is not the original file because it is corrupted due to the incorrect key given

 

User B knows the key, so inputs the key wiehr234hih

 

Program B is decrypted correctly

This is only a test of the emergency broadcast system

This is a product of hysterical mass confusion

A ship of fools adrift on the sea of our pollution

Rudderless and powerless on the sea of our delusion

pennywise - this is only a test

Posted

This is the module that I use. It should return you a decypted text with no validation...

 

and as an added bonus, this is XML safe :)

 

Imports System
Imports System.IO
Imports System.Xml
Imports System.Text
Imports System.Security.Cryptography

' Namespace: YourCompany.Utils.Encryption
' Uses DES private key and vector to provide HTTP / XMLDOM - safe base64 string encryption
' Encrypted string such as account info, passwords, etc can be safely placed in XML element
' for transmission over the wire without any illegal characters

Public Class Encryption64

   ' Use DES CryptoService with Private key pair
   Private key() As Byte = {} ' we are going to pass in the key portion in our method calls
   Private IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}

   Public Function DecryptFromBase64String(ByVal stringToDecrypt As String, ByVal sEncryptionKey As String) As String
       Dim inputByteArray(stringToDecrypt.Length) As Byte
       ' Note: The DES CryptoService only accepts certain key byte lengths
       ' We are going to make things easy by insisting on an 8 byte legal key length

       Try
           key = System.Text.Encoding.UTF8.GetBytes(Left(sEncryptionKey, 8))
           Dim des As New DESCryptoServiceProvider()
           ' we have a base 64 encoded string so first must decode to regular unencoded (encrypted) string
           inputByteArray = Convert.FromBase64String(stringToDecrypt)
           ' now decrypt the regular string
           Dim ms As New MemoryStream()
           Dim cs As New CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write)
           cs.Write(inputByteArray, 0, inputByteArray.Length)
           cs.FlushFinalBlock()
           Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
           Return encoding.GetString(ms.ToArray())
       Catch e As Exception
           Return e.Message
       End Try
   End Function

   Public Function EncryptToBase64String(ByVal stringToEncrypt As String, ByVal SEncryptionKey As String) As String
       Try
           key = System.Text.Encoding.UTF8.GetBytes(Left(SEncryptionKey, 8))
           Dim des As New DESCryptoServiceProvider()
           ' convert our input string to a byte array
           Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes(stringToEncrypt)
           'now encrypt the bytearray
           Dim ms As New MemoryStream()
           Dim cs As New CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write)
           cs.Write(inputByteArray, 0, inputByteArray.Length)
           cs.FlushFinalBlock()
           ' now return the byte array as a "safe for XMLDOM" Base64 String
           Return Convert.ToBase64String(ms.ToArray())
       Catch e As Exception
           Return e.Message
       End Try
   End Function

End Class

Posted

>.<

 

File encryption, not string.

This is only a test of the emergency broadcast system

This is a product of hysterical mass confusion

A ship of fools adrift on the sea of our pollution

Rudderless and powerless on the sea of our delusion

pennywise - this is only a test

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...