Easy encryption

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 2475 Views - Last Post: 04 August 2013 - 12:04 PM Rate Topic: -----

#1 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Easy encryption

Posted 03 June 2013 - 05:19 PM

I'm starting a really basic encryption project, this is my main idea on how it would work:
1-user types the text
2-get the string and turn to a char array
3-associate each char to a number using ASCII table
4-run through aritmetics
5-transform each number back to char again
6-display the new string, formed by all the chars.
Right now I'm having one main problem, how to associate each char to he corresponding value on the table (dec values are the ones that matter). Any ideas on how to do that?

Is This A Good Question/Topic? 1
  • +

Replies To: Easy encryption

#2 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: Easy encryption

Posted 03 June 2013 - 05:34 PM

View PostRaminator, on 04 June 2013 - 12:19 AM, said:

I'm starting a really basic encryption project, this is my main idea on how it would work:
1-user types the text
2-get the string and turn to a char array
3-associate each char to a number using ASCII table
4-run through aritmetics
5-transform each number back to char again
6-display the new string, formed by all the chars.
Right now I'm having one main problem, how to associate each char to he corresponding value on the table (dec values are the ones that matter). Any ideas on how to do that?

Ok, so i solved that by myself
  Private Function CharsToAscii(ByVal chars As IEnumerable(Of Char)) As Integer()
        Return (From c In chars Select Asc(c)).ToArray
    End Function

sorry, i now realise it was a stupid question.
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9208
  • View blog
  • Posts: 34,589
  • Joined: 12-June 08

Re: Easy encryption

Posted 03 June 2013 - 05:40 PM

Like this?
        Console.WriteLine(Asc(foo(0))) '-- from a character to a number

        Console.WriteLine(Convert.ToChar(Asc(foo(0)))) '-- from a number to a character



http://msdn.microsof...v=vs.71%29.aspx
http://msdn.microsof...v=vs.71%29.aspx
Was This Post Helpful? 1
  • +
  • -

#4 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: Easy encryption

Posted 04 June 2013 - 04:29 PM

View Postmodi123_1, on 04 June 2013 - 12:40 AM, said:

Like this?
        Console.WriteLine(Asc(foo(0))) '-- from a character to a number

        Console.WriteLine(Convert.ToChar(Asc(foo(0)))) '-- from a number to a character



http://msdn.microsof...v=vs.71%29.aspx
http://msdn.microsof...v=vs.71%29.aspx

yes, thanks.
Another problem, i started a basic (actually stupid) algorithm, it gets the ASCII value, for example a=97 and the encrypted result is that -1, so a=97->97-1=96=`.The problem is, the code will encrypt the first char i input, but the others its just ignores, rather like the attached picture. What is causing this?

Code for now:
Option Strict On
Public Class MainF
    'tick for random keys
    Dim objRandom As New System.Random(CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))
    Private Function CharsToAscii(ByVal chars As IEnumerable(Of Char)) As Integer()
        'converts a char to ASCII value
        Return (From c In chars Select Asc(c)).ToArray
    End Function
    Private Function AsciiToChars(ByVal nums As IEnumerable(Of Integer)) As Char()
        'converts ASCII value to char
        Return (From c In nums Select Chr(c)).ToArray
    End Function
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'set random key
        KeyE.Text = CStr(GetRandomNumber(10000, 99999))
    End Sub
    Public Function GetRandomNumber(Optional ByVal Low As Integer = 1, Optional ByVal High As Integer = 100) As Integer
        'generate a randomic number, for the random key
        Return objRandom.Next(Low, High + 1)
    End Function
    Private Sub GenKey_Click(sender As Object, e As EventArgs) Handles GenKey.Click
        'renew key
        KeyE.Text = CStr(GetRandomNumber(10000, 99999))
    End Sub
    Private Sub En_TextChanged(sender As Object, e As EventArgs) Handles En.TextChanged
        'encrypt
        If (String.IsNullOrEmpty(En.Text)) Then
            Enres.Text = ""
        Else
            Dim charA() As Char = En.Text.ToCharArray
            Dim intA() As Integer = CharsToAscii(charA)
            Dim Restring As String
            For value As Integer = 0 To intA.Length - 1
                Dim resint(intA.Length - 1) As Integer
                resint(value) = intA(value) - 1
                If resint(value) = -1 Then
                    resint(value) = 255
                End If
                Restring = Restring & AsciiToChars(resint)
                Enres.Text = Restring
            Next
        End If
    End Sub

    Private Sub De_TextChanged(sender As Object, e As EventArgs) Handles De.TextChanged
        'decrypt-future work
        If (String.IsNullOrEmpty(De.Text)) Then
            DeRes.Text = ""
        Else

        End If
    End Sub
End Class

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9208
  • View blog
  • Posts: 34,589
  • Joined: 12-June 08

Re: Easy encryption

Posted 04 June 2013 - 06:13 PM

Where in that do you go through each character in the string (aka character array) and feed it to the CharsToAscii?
Was This Post Helpful? 1
  • +
  • -

#6 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: Easy encryption

Posted 05 June 2013 - 01:40 PM

View Postmodi123_1, on 05 June 2013 - 01:13 AM, said:

Where in that do you go through each character in the string (aka character array) and feed it to the CharsToAscii?

On line 30,
Dim charA() As Char = En.Text.ToCharArray

Was This Post Helpful? 0
  • +
  • -

#7 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9208
  • View blog
  • Posts: 34,589
  • Joined: 12-June 08

Re: Easy encryption

Posted 05 June 2013 - 01:44 PM

mmm.. that seems to be where you are getting the character array, but where does that character array interact with CharsToAscii?
Was This Post Helpful? 1
  • +
  • -

#8 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: Easy encryption

Posted 05 June 2013 - 02:14 PM

View Postmodi123_1, on 05 June 2013 - 08:44 PM, said:

mmm.. that seems to be where you are getting the character array, but where does that character array interact with CharsToAscii?

On line 31
Dim intA() As Integer = CharsToAscii(charA)

Was This Post Helpful? 0
  • +
  • -

#9 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9208
  • View blog
  • Posts: 34,589
  • Joined: 12-June 08

Re: Easy encryption

Posted 05 June 2013 - 02:27 PM

Have you actually put a break point in En_TextChanged to see what is being set where?
Was This Post Helpful? 1
  • +
  • -

#10 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: Easy encryption

Posted 05 June 2013 - 02:41 PM

View Postmodi123_1, on 05 June 2013 - 09:27 PM, said:

Have you actually put a break point in En_TextChanged to see what is being set where?

I just did that, it was a good idea, look how weird this is:
Posted Image
Posted Image
Was This Post Helpful? 0
  • +
  • -

#11 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: Easy encryption

Posted 06 June 2013 - 01:09 PM

View PostRaminator, on 05 June 2013 - 09:41 PM, said:

View Postmodi123_1, on 05 June 2013 - 09:27 PM, said:

Have you actually put a break point in En_TextChanged to see what is being set where?

I just did that, it was a good idea, look how weird this is:
Posted Image
Posted Image

Ok, so, its pretty obvious that the problem here is when i make the -1 maths, but i can't figure out what i'm doing wrong, is it the array indexing?
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9208
  • View blog
  • Posts: 34,589
  • Joined: 12-June 08

Re: Easy encryption

Posted 06 June 2013 - 01:24 PM

I have no idea what you are trying to accomplish there.

Cutting all the bloat off your program

public sub someMethod()
        Dim en As New TextBox
        Dim Enres As New TextBox

        en.Text = "abc"

        'encrypt
        If (String.IsNullOrEmpty(en.Text)) Then
            Enres.Text = ""
        Else
            Dim charA() As Char = en.Text.ToCharArray
            Dim intA() As Integer = CharsToAscii(charA)
            Dim Restring As String
            For value As Integer = 0 To intA.Length - 1
                Dim resint(intA.Length - 1) As Integer
                resint(value) = intA(value) - 1
                If resint(value) = -1 Then
                    resint(value) = 255
                End If
                Restring = Restring & AsciiToChars(resint)
                Enres.Text = Restring
            Next
        End If

    End Sub

    Private Function CharsToAscii(ByVal chars As IEnumerable(Of Char)) As Integer()
        'converts a char to ASCII value
        Return (From c In chars Select Asc(c)).ToArray
    End Function
    Private Function AsciiToChars(ByVal nums As IEnumerable(Of Integer)) As Char()
        'converts ASCII value to char
        Return (From c In nums Select Chr(c)).ToArray
    End Function


I can turn

Quote

abc

into

Quote

` a b

Was This Post Helpful? 1
  • +
  • -

#13 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: Easy encryption

Posted 06 June 2013 - 03:38 PM

View Postmodi123_1, on 06 June 2013 - 08:24 PM, said:

I have no idea what you are trying to accomplish there.

Cutting all the bloat off your program

public sub someMethod()
        Dim en As New TextBox
        Dim Enres As New TextBox

        en.Text = "abc"

        'encrypt
        If (String.IsNullOrEmpty(en.Text)) Then
            Enres.Text = ""
        Else
            Dim charA() As Char = en.Text.ToCharArray
            Dim intA() As Integer = CharsToAscii(charA)
            Dim Restring As String
            For value As Integer = 0 To intA.Length - 1
                Dim resint(intA.Length - 1) As Integer
                resint(value) = intA(value) - 1
                If resint(value) = -1 Then
                    resint(value) = 255
                End If
                Restring = Restring & AsciiToChars(resint)
                Enres.Text = Restring
            Next
        End If

    End Sub

    Private Function CharsToAscii(ByVal chars As IEnumerable(Of Char)) As Integer()
        'converts a char to ASCII value
        Return (From c In chars Select Asc(c)).ToArray
    End Function
    Private Function AsciiToChars(ByVal nums As IEnumerable(Of Integer)) As Char()
        'converts ASCII value to char
        Return (From c In nums Select Chr(c)).ToArray
    End Function


I can turn

Quote

abc

into

Quote

` a b

Yes, thats the idea, the problem is the output for

Quote

abc
is being

Quote

`
rather then

Quote

`ab
,rather like this Posted Image
and i just don't know where this problem happens, i know its something around lines 34-40, but ,even with breakpoints, i can't find it's source.

This post has been edited by Raminator: 06 June 2013 - 03:44 PM

Was This Post Helpful? 0
  • +
  • -

#14 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9208
  • View blog
  • Posts: 34,589
  • Joined: 12-June 08

Re: Easy encryption

Posted 06 June 2013 - 05:01 PM

For Pete's sake... so I threw a bunch of textboxes in a form and tried to run this .. bad use of names there, but what evs.

As it is - your whole "En_TextChanged" is bunk. What the heck are you doing mixing arrays with single characters, and so on.

You try shoving an entire array into "AsciiToChars(resint)" and expect to some how have the system merge that into a string?


   Private Sub En_TextChanged(sender As Object, e As EventArgs) Handles En.TextChanged
        Dim charA() As Char = En.Text.ToCharArray
        Dim intA() As Integer = CharsToAscii(charA)
        Dim Restring As String = String.Empty
        Dim resint(intA.Length) As Integer

        For value As Integer = 0 To intA.Length - 1
            resint(value) = intA(value) - 1
            If resint(value) = -1 Then
                resint(value) = 255
            End If
        Next

        For i As Int32 = 0 To resint.Length - 1
            Restring &= Convert.ToChar(resint(i))
        Next

        Console.WriteLine(Restring)
        '    Enres.Text = Restring
    End Sub


You need to stop and logically think about what you are attempting to do with your shuffling around of arrays and so forth.
Was This Post Helpful? 1
  • +
  • -

#15 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: Easy encryption

Posted 06 June 2013 - 05:33 PM

I get it, i was using the same loop for things that could'nt be in the same loop, and forgeting to make the code actually form the final string. Thanks a lot

This post has been edited by AdamSpeight2008: 07 June 2013 - 06:46 AM
Reason for edit:: Removing pointless quote of post

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3