Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)
.Net already has a few classes for dealing with encryption - have a look under System.Security.Cryptography - plenty there to be going on with ;)

Ok been playing with it some more but i'm having problem i've attachd my code

Encrypt("hello") will retunr system.byte()

not the encrypted form of hello

 

thanx in advance

crypto.vb.txt

Edited by jorge
  • Administrators
Posted

You could just hard code some values - the example below isn't exactly the best choice of values ;)

Dim key() As Byte = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
Dim IV() As Byte = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
'set the key and IV.
myRijndael.Key = key
myRijndael.IV = IV

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Ok, i know this encryption is going a different way but this is the encryption i used to use in my older software, (the newer software uses a custom one which is alot more secure but i wont reveal the source code for that as its very long)

 

Dim epassword as Integer

Sub DomainSave()
       Dim letter As Char
       Dim strCode As String
       Dim i, charsInFile, Code As Short
       Dim path As String

       path = System.Windows.Forms.Application.StartupPath & "\dataCED.cde"

       If path <> "" Then
           strCode = epassword
           If strCode = "" Then Exit Sub 'if cancel clicked
           'save text with encryption scheme
           Code = CShort(strCode)
           charsInFile = txtDomain.Text.Length
           FileOpen(1, path, OpenMode.Output)
           For i = 0 To charsInFile - 1
               letter = txtDomain.Text.Substring(i, 1)
               'convert to number w/ Asc, then use Xor to encrypt
               Print(1, Asc(letter) Xor Code) 'and save in file
           Next
           FileClose(1)
       End If
   End Sub

   Sub DomainOpen()
       Dim ch As Char
       Dim strCode As String
       Dim Code, Number As Short
       Dim Decrypt As String = ""
       Dim inbox As String
       Dim path As String

       path = System.Windows.Forms.Application.StartupPath & "\DataCED.cde"

       If path <> "" Then
           Try 'open file and trap any errors using handler
               strCode = epassword
               If strCode = "" Then Exit Sub 'if cancel clicked
               Code = CShort(strCode)
               FileOpen(1, path, OpenMode.Input)
               Do Until EOF(1) 'read lines from file
                   Input(1, Number) 'read encrypted numbers
                   ch = Chr(Number Xor Code) 'convert with Xor
                   Decrypt = Decrypt & ch 'and build string
               Loop
               txtDomain.Text = Decrypt 'then display converted string
               txtDomain.Select(1, 0)   'remove text selection
               txtDomain.Enabled = True 'allow text cursor
           Catch

           Finally
               FileClose(1) 'close file
           End Try
       End If
   End Sub

And just declare wot epassword somewhere

epassword is the encryption number, it can be i think any 4 digit number, some 5 digit numbers work to

  • Administrators
Posted

Try this version of your original code, It should encrypt and decrypt correctly. It also Base64 encodes the encrypted string making it easier to store safely in a text file or similar.

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

Namespace encryption
Class Crypt

	Shared key() As Byte = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
	Shared IV() As Byte = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}

	Public Shared Function Encrypt(ByVal sdata As String) As String
		Dim roundtrip As String
		Dim textConverter As New ASCIIEncoding
		Dim myRijndael As New RijndaelManaged
		Dim fromEncrypt() As Byte
		Dim encrypted() As Byte
		Dim toEncrypt() As Byte
		'set the key and IV.
		myRijndael.Key = key
		myRijndael.IV = IV

		'Get an encryptor.
		Dim encryptor As ICryptoTransform = myRijndael.CreateEncryptor(key, IV)

		'Encrypt the data.
		Dim msEncrypt As New MemoryStream
		Dim csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
		'Convert the data to a byte array.
		toEncrypt = textConverter.GetBytes(sdata)

		'Write all data to the crypto stream and flush it.
		csEncrypt.Write(toEncrypt, 0, toEncrypt.Length)
		csEncrypt.FlushFinalBlock()

		'Get encrypted array of bytes.
		encrypted = msEncrypt.ToArray()
		Return Convert.ToBase64String(encrypted)
	End Function
	Public Shared Function Decrypt(ByVal sdata As String) As String
		Dim roundtrip As String
		Dim textConverter As New ASCIIEncoding
		Dim myRijndael As New RijndaelManaged
		Dim fromEncrypt() As Byte
		Dim toEncrypt() As Byte

		'Create a new key and initialization vector.
		myRijndael.GenerateKey()
		myRijndael.GenerateIV()



		'This is where the message would be transmitted to a recipient
		' who already knows your secret key. Optionally, you can
		' also encrypt your secret key using a public key algorithm
		' and pass it to the mesage recipient along with the RijnDael
		' encrypted message.            
		'Get a decryptor that uses the same key and IV as the encryptor.
		Dim decryptor As ICryptoTransform = myRijndael.CreateDecryptor(key, IV)

		'Now decrypt the previously encrypted message using the decryptor
		' obtained in the above step.
		Dim b() As Byte
		b = Convert.FromBase64CharArray(sdata.ToCharArray, 0, sdata.Length)


		Dim msDecrypt As New MemoryStream(b)
		Dim csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)

		fromEncrypt = New Byte(sdata.Length) {}

		'Read the data out of the crypto stream.
		csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length - 1)

		'Convert the byte array back into a string.
		roundtrip = textConverter.GetString(fromEncrypt)
		Return roundtrip
	End Function
	Public Shared Sub main()
		Dim bExit As Boolean = False
		Dim sData As String
		Dim sOut As String
		Console.WriteLine("########################")
		Console.WriteLine("#EnCrypter V1.1        #")
		Console.WriteLine("########################")
		Do While bExit = False
			sData = Console.ReadLine()
			If sData.ToLower.StartsWith("encrypt") Then
				sData = sData.Substring(8)

				sData = Encrypt(sData)

				Console.WriteLine(sData)
			ElseIf sData.ToLower.StartsWith("decrypt") Then
				sData = sData.Substring(8)
				sOut = Decrypt(sData)
				Console.Write(Decrypt(sData))
			ElseIf sData.ToLower.StartsWith("exit") Then
				bExit = True
			End If
		Loop
		End
	End Sub
End Class
End Namespace

 

I would recomend against using the XOR based method above as it is a simple method to crack and it also uses the legacy VB6 file handling functions.

If you wanted to make your code more secure you could store the key / iv in a per user config file (possibly requesting a password on a 1st run and storing a key based on their input) - be aware though if they lost the key / config file it would leave them unable to decrypt any of the files.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

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...