Hi, ok i had a file looking like this:

SilkroadR,,321,13,321,Gobi,False,False,False,False,False
SilkroadR,,321,321321,321,Gobi,False,False,False,False,False
SilkroadR,,213,321,231,Gobi,False,False,False,False,False
SilkroadR,,3251325,3215,3251,Gobi,False,False,False,False,False
SilkroadR,,,,,Gobi,False,False,False,False,False

I encrypted it and it looks like this:

Hdgr6oHm47q2SC5AEjqUY9Boi5WfUT5FTfLvMVYLtorgmufM5GiH6wMg12raZ/YpW/OPZ7bL94wbSWiauy59nrEHlJdO3o1voedaWb/XV7lxNzbUURqDgGtkpr6Ex++d0YAA4sCnkB/GcDtuFd+vqF2IJoI3cC+WKPF73zM5eNz6mJd1hjZK+40je7/LbUyU7JQ8nmvBpEy7J391xM5X5dfKlNziYahufdVsxbO8mTAosQcEZ8JtUoJg2ZWjXO8nPqDci8iy/JgBYnd8Z4fpbfwY5jKwTrq2+TW9k/+I2F9PXUT6OzUUTk4tRFfvWVXBFj/C5s60puxaYJQxgzUzKE2yWls4N3qk3TcZBH1kKCB8h0lS69X77mOxJ0ixkOm1a+deaIWXsBfFHq8ej30ShH2WFf09iAr3

Ok, thats what i wanted, i used to load my accounts like how it looked before the encrypting with this code:

Dim iCount As Integer = CInt("0")
Accounts = New List(Of Account)
My.Forms.Main_frm.AccNumber.Items.Clear()
For Each Info As String In System.IO.File.ReadAllLines(AccFileR)
    Accounts.Add(New Account(Info.Split(CChar(","))))
    My.Forms.Main_frm.AccNumber.Items.Add(iCount)
    iCount += 1
Next
My.Forms.Main_frm.AccNumber.SelectedIndex = 0
phFunctions.SelectedAccount()

What it does that it reads the file and for each line its 1 account, and each string of the account is the value after each "," at the end its just a fuction to switch between accounts with a numericUpDown, well i tried modifying it to work with the encryption but no success this what i have:

Dim iDecode As New Security()
Dim iAccs As String = Nothing
Dim iCount As Integer = CInt("0")
Accounts = New List(Of Account)
My.Forms.Main_frm.AccNumber.Items.Clear()
iAccs = iDecode.decryptString(System.IO.File.ReadAllText(AccFile))
For Each dInfo As String In iAccs
    Accounts.Add(New Account(iAccs.Split(CChar(","))))
    My.Forms.Main_frm.AccNumber.Items.Add(iCount)
    iCount += 1
Next
My.Forms.Main_frm.AccNumber.SelectedIndex = 0
phFunctions.SelectedAccount()

And this is what i use to encrypt/decrypt:

Imports System.IO
Imports System.Security.Cryptography
Imports System.Text
Public Class Security
    Protected key As String = "12345678"
    Public Sub New()
        'constructor
    End Sub
    Public Function encryptString(ByVal strtext As String) As String
        Return Encrypt(strtext, key)
    End Function
    Public Function decryptString(ByVal strtext As String) As String
        Return Decrypt(strtext, key)
    End Function
    'The function used to encrypt the text
    Private Function Encrypt(ByVal strText As String, ByVal strEncrKey _
             As String) As String
        Dim byKey() As Byte = {}
        Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
        Try
            byKey = System.Text.Encoding.UTF8.GetBytes(Left(strEncrKey, 8))
            Dim des As New DESCryptoServiceProvider()
            Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes(strText)
            Dim ms As New MemoryStream()
            Dim cs As New CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write)
            cs.Write(inputByteArray, 0, inputByteArray.Length)
            cs.FlushFinalBlock()
            Return Convert.ToBase64String(ms.ToArray())
        Catch ex As Exception
            Return ex.Message
        End Try
    End Function
    'The function used to decrypt the text
    Private Function Decrypt(ByVal strText As String, ByVal sDecrKey _
               As String) As String
        Dim byKey() As Byte = {}
        Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
        Dim inputByteArray(strText.Length) As Byte
        Try
            byKey = System.Text.Encoding.UTF8.GetBytes(Left(sDecrKey, 8))
            Dim des As New DESCryptoServiceProvider()
            inputByteArray = Convert.FromBase64String(strText)
            Dim ms As New MemoryStream()
            Dim cs As New CryptoStream(ms, des.CreateDecryptor(byKey, 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 ex As Exception
            Return ex.Message
        End Try
    End Function
End Class

i need help editing the first code to the code the file to a string or w.e, and then read each line of that string and split it with "," to give a value to each structure of string that i have for a List (Of).

Try

For Each Info As String In System.IO.File.ReadAllLines(AccFileR)
    Info = IDecode.decryptString(Info)
    'Debug.WriteLine(Info)
    Accounts.Add(New Account(Info.Split(CChar(","))))
    My.Forms.Main_frm.AccNumber.Items.Add(iCount)
    iCount += 1
Next

I get "Index was outside the bounds of the array."
I took out the ex.message and it leads me to

    Public Sub New(fields() As String)
        Me.Version = fields(0)
        Me.Login = fields(1)
        Me.Username = fields(2)
        Me.Password = fields(3)
        Me.Character = fields(4)
        Me.Server = fields(5)
        Me.ReturnLogin = fields(6) = "True"
        Me.LoginStart = fields(7) = "True"
        Me.RelogDisco = fields(8) = "True"
        Me.DisMap = fields(9) = "True"
        Me.SpecialAcc = fields(10) = "True"
    End Sub

Me.Password = fields(3) < out of index

Actually my bad, it works now, the thing is it only reads it once, so only the first line is read(Only 1 account is filled)

 For Each Info As String In System.IO.File.ReadAllLines(AccFile)
     Info = IDecode.decryptString(Info)
     'Debug.WriteLine(Info)
     Accounts.Add(New Account(Info.Split(CChar(","))))
     My.Forms.Main_frm.AccNumber.Items.Add(iCount)
     iCount += 1
 Next

The problem about that is that the original file can be 100 lines, but when encrypted it will be 1 line, so using .ReadAllLines will read the only line, which means it will only fill 1 account. Im still not sure how to decrypt it and then read all lines from a string because when you decrypt to a string ti already has many lines and all that.

When you encrypt it you are also encrypting the carriage return and line feed characters. As such, ReadAllLines can't parse the text back into separate lines. You'll have to encrypt and write each line separately to preserve the delimiters.

commented: Exactly what i had to do, thank you for the reply. +0

Ok, I will try that later after I come from the beach :P

Assuming this is the code that you are using to write the file (found in your other thread),

Private Sub WriteAccounts(filename As String, Accounts As List(Of Account))
    Dim lines As String = ""
    For Each acct As Account In Accounts
        lines &= acct.ToString() & vbCrLf
    Next
    System.IO.File.WriteAllText(filename, lines)
End Sub

you could modify it something like this

    Private Sub WriteAccounts(ByVal filename As String, ByVal Accounts As List(Of account))
        Dim encrypt As New Security
        Dim sb As New System.Text.StringBuilder(500)
        For Each acct As account In Accounts
            sb.AppendLine(encrypt.encryptstring(acct.ToString))
        Next
        System.IO.File.WriteAllText(filename, sb.ToString)
    End Sub

    Private Sub ReadAccounts(ByVal filename As String, ByVal Accounts As List(Of account))
        Dim encrypt As New Security
        Accounts.Clear()
        Dim lines() As String = System.IO.File.ReadAllLines(filename)
        Dim decrypted As String
        For Each ln As String In lines
         decrypted = encrypt.decryptstring(ln)
         Accounts.Add(New account(decrypted.Split(","c)))
        Next
    End Sub
commented: Went ahead of me and did it for me XD thanks for saving my time :) worked perfectly +1

Jim, thx you were right, and TnTin thx for providing the code i was gonna do it, but i was on the beach and you went ahead of me so i didn't actually had to do anything, i will vote up for you 2 :) also im making the thread solved, even if its solved i want to make a little request or question, is it possible to re-encrypt the encrypted file ? example its encrypted is it able to convert the encryption to binary (0100001001101001011011100110000101110010011110010010000001000011011011110110010001100101), and be able to read it and then decrypt and then load it to the accounts ? because the binary will be only 1 lin not multiple. Thx :)

want to make a little request or question, is it possible to re-encrypt the encrypted file ? example its encrypted is it able to convert the encryption to binary (0100001001101001011011100110000101110010011110010010000001000011011011110110010001100101), and be able to read it and then decrypt and then load it to the accounts ? because the binary will be only 1 lin not multiple. Thx :)

Unless you have a program that reads one byte at a time and converts it to a string representation of eight bits that comprise each byte, you will never see "10101111.....".
All files are stored in binary. Howver, there is this thing called encoding that defines how the bytes and their values are to be interpreted. To convert each bit to a 1 or 0 and store that string so that you could view it in say Notpad) would be extremely inefficient as each bit of the original will now occupy eight bits in the stored.

If you want only one line to appear in the editor when you open the data file, then this modification will produce that.

    Private Sub WriteAccounts(ByVal filename As String, ByVal Accounts As List(Of Account))
      Dim Security As New Security

      'create file on each save
      Dim fs As IO.FileStream = IO.File.Open(filename, IO.FileMode.Create, IO.FileAccess.Write)
      Dim bw As New IO.BinaryWriter(fs)
      'write a binary Account to the file
      For Each acct As Account In Accounts
         bw.Write(Security.encryptString(acct.ToString))
      Next
      bw.Close()
    End Sub

   Private Sub ReadAccounts(ByVal filename As String, ByVal Accounts As List(Of Account))
      Dim Security As New Security

      Dim fs As IO.FileStream = IO.File.Open(filename, IO.FileMode.Open, IO.FileAccess.Read)
      Dim br As New IO.BinaryReader(fs)
      Accounts.Clear()
      Dim decrypted As String
      Do Until fs.Position = fs.Length
         decrypted = Security.decryptString(br.ReadString)
         Accounts.Add(New Account(decrypted.Split(","c)))
      Loop
      br.Close()
   End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.