views:

208

answers:

6

Can anyone please explain with the easy coding how to reverse this algorithm so that I get back the original text string?

Public Function CreateIntChecksum(ByVal s As String) As Integer
    Dim r As Integer = 0
    For i As Integer = 0 To (s.Length() - 1)
        Dim bchar As Byte = Convert.ToByte(s(i))
        r = bchar + ((r << 5) - r)
    Next
    Return r
End Function
+1  A: 

You simply can't reverse it. It's a checksum which is not reversible.

Darin Dimitrov
+2  A: 

how to reverse this algorithm so that I get back the original text string?

You can't, by design.
This is a (simple) checksum or hashing function.

Take a look at the size of the information going in and out: the function transforms a string s of arbitrary length into a 32-bit Integer. For each integer value there will be many input strings that will yield that result.


Edit: Apparently you want a shuffling algorithm. Maybe take a look at ROT13. Be advised this is not a (very) safe form of encryption.

Henk Holterman
I also need a formula based shuffling algorithm in c#. I was searching internet when I found this code. If anyone has such formula based shuffling which has reversing function too so that even if I shuffle a single text hundreds of times, it can be reversed.
Tush
+2  A: 

You can't. What you have here is a Checksum, which is a basic hashing function. The the whole point of a hashing function is that it is irreversible. Hashing functions map a set of inputs (usually infinite) to a smaller set of outputs - so multiple inputs can end up with the same output, and thus this makes reversing a hash impossible (assuming the hash is correctly done). This is why they are used to store passwords - there is no way to read a hash and go "Oh, that is password XYZ".

One way of trying to find out the original value is to use a Rainbow Table. This is merely a massive table of inputs and their equivalent hashed (or in this case checksummed) values. If you have the hashed value of your unknown string you can search for it in the table and get the set of possible inputs. This is not a way to reverse a hash function, which is impossible; it is merely a brute force guessing method. Note also that in this case (assuming the hashing function is not biased) there are an infinite number of strings that match each checksummed value, as a visual basic string can be of arbitrary length. This would make a rainbow table for this very impractical - you could cover the set of probably inputs for a hashing (as most normal users won't enter more than a 10 character code), but nothing stops the user using a 67 character code, or 109, or...

Read the wikipedia articles for Hash Functions, Checksums and Rainbow Tables.

Stephen
+1 Note however, that with rainbow tables, it may be possible to read a hash and say "the string XYZ will hash to this, and so will infinitely many other (but probably dissimilar) strings". RT are basically a solution by brute force.
Piskvor
This is not a hashing function. I picked it up to find out if Reversing is possible. This is a bit-wise encoding function which outputs an integer through this algorithm. It might be reversible?
Tush
@Pisk: but that will only get you a colliding string, OK if you want a (equivalent) password but it is very unlikely a rainbow table will give you the original text.
Henk Holterman
@Tush: You have a checksum (= simple hash) function. The answer is No.
Henk Holterman
How should it be possible to encode an arbitrary length string into 32 bits?
Eiko
@Eiko if you could bye-bye rar bye-bye zip. nearly every file on the computer would be 32 bits.
Wes
@Henk Holterman: That's what I was trying to say - you'll get something which could have been the original string, but there isn't a way to find out. Sorry if it wasn't clear enough. (For many intents and porpoises, this is sufficient; plus it's more likely that the RTs will be made from the [a-zA-Z0-9] space, so the colliding string has some propability of being meaningful)
Piskvor
64 Bits? Can anyone code for me?
Tush
@Eiko: Oh, you can *encode* it all right, see the OP's algorithm. The problem lies in the decoding - you'll have many strings (countably infinite, to be precise), of various lengths and content - and *all* fo them will correctly map to the same integer. There's the problem - you can not find out which of the inputs was encoded into the output.
Piskvor
@Tush - use a [Long](http://msdn.microsoft.com/en-us/library/y595sc15(v=VS.80\).aspx), which is 8 bytes (64 bits) instead of an Int, which is 32 bits?
Stephen
What is OP's algorithm, please provide sample in c# if possible. I require a reversible formula based shuffle algorithm which is able to shuffle, then reverse the text string. It would be nice of you to help me now. :-)
Tush
I am learning. Please provide a sample for 64 bits reversible algorithm in c#.
Tush
@Tush - That's both a rude question (questions of the type "Plz I can has code now?" are considered rude - we are here to **help**, not to write your code for you), and a different question from what you asked here. You should mark an answer for this question (choose the best given answer and click on the outline of the tick beside it), and then open a new question to ask about your shuffling and reversing of text strings in C#.
Stephen
@Tush: You are the OP (Original Poster) here.
Henk Holterman
@Tush: And you don't seem to get the answers: 64 bits or 128 bits wouldn't solve the irreversibility problem here.
Henk Holterman
@Wes -)
Eiko
@Eiko My comment was actually meant to support yours not contradict you.
Wes
It is all right :-) Nothing bad should matter.
Tush
Long String Text may or may not be crushed and compressed into hash kind of ascii "checksum" by using sophisticated mathematical formula/algo. To compress megabytes of asci text into a 128 or so bytes. When we are decompressing it, the last character is extracted first, then we just go on decompression using the formula and the sequential keys. This is the terra compression I was thinking about.
Tush
A: 

You can't. If nothing else, then simply because Integer contains only 32 bits of data and String can have any length.

svick
Do you agree that if I use 64 Bit Integer, will I get back any fixed length of string by reversing?
Tush
@Tush no. Unfortunatly not. What is it you want reversible encryption?
Wes
Yes. I am learning and weak in mathematics. I wish to have fixed string length = fixed reversible encryption algorithm.
Tush
@Tush: Hmm, that's quite a different question, IMO. Try asking for that in a new question - something like "I wish to have a reversible encryption algorithm for a fixed string length, which one is good for my situation" and describe your use case (for what you need the encryption, what you need to protect against, etc.). I'd say this should get you some answers.
Piskvor
@Piskvor +1@Tush consider this an upvote to asking a new question.
Wes
@Wes: The question seems to be already asked, as of 5 hours ago: http://stackoverflow.com/questions/3541378/dont-know-how-to-code-reversible-shuffle-algorhythm-by-using-key-anyone-can-hel
Piskvor
+3  A: 
KennyTM
If anyone got a simple reversible algorithm?
Tush
I am totally new to these codes. Please give a coding.
Tush
That really like this answer. +1 for being awesome.
mrduclaw
A: 

First of all the code as posted will throw an exception for anything but the shortest of strings.

The following code includes the OP's original code, plus a simple checksum, and a guess at how the checksum might be used.

Private Sub Button2_Click(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) Handles Button2.Click

    Dim buffer() As Byte

    Dim tstString As String = "Calculate a checksum for a given string"

    Dim chkSumFix As Integer = CreateIntChecksumFixed(tstString) 'get a checksum

    buffer = SendPacket(tstString, chkSumFix) 'create a byte buffer to send
    tstString = decodePacket(buffer)

    'do the same using the OP's original code
    Dim chkSum As Integer = CreateIntChecksum(tstString) 'error

    buffer = SendPacket(tstString, chkSum)
End Sub

'OP
Public Function CreateIntChecksum(ByVal s As String) As Integer
    Dim r As Integer = 0
    For i As Integer = 0 To (s.Length() - 1)
        Dim bchar As Byte = Convert.ToByte(s(i))
        r = bchar + ((r << 5) - r)
    Next
    Return r
End Function

'a very simple checksum
Public Function CreateIntChecksumFixed(ByVal s As String) As Integer
    Dim r As Integer = 0
    For i As Integer = 0 To (s.Length() - 1)
        Dim bchar As Byte = Convert.ToByte(s(i))
        r = (r And &HFFFF) + bchar
    Next
    Return r
End Function

Private Function SendPacket(ByVal aString As String, _
                            ByVal aChecksum As Integer) As Byte()
    'construct a packet to be sent
    'Packet format
    'returns a byte buffer
    'byte(0 -3) = length of original string - use BitConverter.ToInt32 to extract
    'byte(4-n) = original string.  n = (4 + string length) - 1
    'byte(n + 1, n + 2, n + 3, n + 4) = checksum

    Dim length As Integer
    Dim retV As New List(Of Byte)
    retV.AddRange(System.Text.Encoding.ASCII.GetBytes(aString)) 'add string to packet
    length = retV.Count 'get length - use this length in case a different encoding is used
    retV.AddRange(BitConverter.GetBytes(aChecksum)) 'add checksum
    retV.InsertRange(0, BitConverter.GetBytes(length)) 'insert length at start of packet

    Return retV.ToArray
End Function

Private Function decodePacket(ByVal buffer As Byte()) As String
    Dim sLen As Integer = BitConverter.ToInt32(buffer, 0)
    If sLen + 8 <> buffer.Length Then Throw New ArgumentOutOfRangeException

    Dim s As String = System.Text.Encoding.ASCII.GetChars(buffer, 4, sLen)

    Dim chksum As Integer = CreateIntChecksumFixed(s)

    Dim embeddedChecksum As Integer = BitConverter.ToInt32(buffer, sLen + 4)

    If chksum <> embeddedChecksum Then Throw New ArgumentException("Invalid checksum")
    Return s
End Function
dbasnett
And why exactly does a simple statement of fact deserve a down vote?
dbasnett
This leads to a new thought.. If the string is sufficient short enough, it actually *might* be reversible. :-) Not sure why it should throw an exception - do overflows generate exceptions in vb.net?
Eiko
As has been pointed out, the code appears to be used to generate a checksum (name of the function, code). I will edit my post with the code and intended purpose(guess).
dbasnett
Terra Compression. ;-)
Tush
There are bugs in the above coding. Multiple Invalid characters are added in the left and right corners of the reversed string thereby making the reversed string corrupted. The string gets reversed though. Please fix the bugs so that the code becomes useful.
Tush
I guess 64 bit UInt64 integer can hold 8 bytes of integer. So if you can provide the coded function for 64 Bit and not integer.
Tush
@tush - I am lost? The original string (assumed to be ascii) is not touched.
dbasnett
Added the code in place to decode the packet and check the checksum.
dbasnett
Thanks, now the code is working without corruption of output string upon reversing. I must ask if it is possible to encode the input ascii string into only one 64 bit integer, by using a formula/algo and a key/password? That I was asking for in here. Because if that is possible, string would just be crushed into the 64 bit integer. Thanks.
Tush