jorge Posted July 17, 2004 Posted July 17, 2004 Hey, Any one have and easy to use encryption class? or a peice of code to do it? Cause i'm transfering a user+pass over the network(user:pass@command) but it can easyly be read and that i don't wan't. thanx in advance Quote Jorge - http://www.blackdot.be/?page=apache.htm
Administrators PlausiblyDamp Posted July 17, 2004 Administrators Posted July 17, 2004 .Net already has a few classes for dealing with encryption - have a look under System.Security.Cryptography - plenty there to be going on with ;) Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
jorge Posted July 17, 2004 Author Posted July 17, 2004 (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 advancecrypto.vb.txt Edited July 17, 2004 by jorge Quote Jorge - http://www.blackdot.be/?page=apache.htm
jorge Posted July 17, 2004 Author Posted July 17, 2004 Hmmz have got the encrypt funtion to work, not decrypt jet, still crashes, but i notice something every thime i encrypt i get something else as result :/ any ideas? Quote Jorge - http://www.blackdot.be/?page=apache.htm
Administrators PlausiblyDamp Posted July 17, 2004 Administrators Posted July 17, 2004 You are getting different results after every run because you are generating a new key and new IV every run, you may be better of pre-generating a key / IV and storing those somewhere. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
jorge Posted July 17, 2004 Author Posted July 17, 2004 You are getting different results after every run because you are generating a new key and new IV every run' date=' you may be better of pre-generating a key / IV and storing those somewhere.[/quote'] How would i manualy create them? Quote Jorge - http://www.blackdot.be/?page=apache.htm
Administrators PlausiblyDamp Posted July 17, 2004 Administrators Posted July 17, 2004 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 Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
jorge Posted July 17, 2004 Author Posted July 17, 2004 yeah thanx it works like a charm No to fix the decrypt thing and am done :D , the decrypt thing can't be that ahard lol Quote Jorge - http://www.blackdot.be/?page=apache.htm
jorge Posted July 17, 2004 Author Posted July 17, 2004 Ok how would i convert a string to a byte array? Quote Jorge - http://www.blackdot.be/?page=apache.htm
cdoverlaw Posted July 17, 2004 Posted July 17, 2004 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 Quote
jorge Posted July 18, 2004 Author Posted July 18, 2004 Thanx, i'll play with this a bit Quote Jorge - http://www.blackdot.be/?page=apache.htm
Administrators PlausiblyDamp Posted July 18, 2004 Administrators Posted July 18, 2004 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
jorge Posted July 18, 2004 Author Posted July 18, 2004 Cool this works fine :) thanx a lot, now it's a normal string :p Quote Jorge - http://www.blackdot.be/?page=apache.htm
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.