1

i am having problem with a piece of code in vb.net.In this the flow of control goes to the else condition even though the value in if is true.

I had used this as input: WD-WXK0AC9T7316 code:

Private Function EncryptHDKey(ByVal Hd_no As String) As String

       Dim EncryptedString As String = ""

        For Each ch As Char In Hd_no
            If ((ch >= "0" And ch <= "9") Or (ch >= "a" And ch <= "z") Or (ch >= "A" And ch <= "Z")) Then
                If (Convert.ToChar((Convert.ToInt16(ch) + 4)) > "9" And ch <= "9") Then
                    EncryptedString = EncryptedString + Convert.ToChar((Convert.ToInt16(ch) + 3) - Convert.ToInt16("9"))
                Else
                    If (Convert.ToChar((Convert.ToInt16(ch) + 4)) > "z" And ch <= "z") Then
                        EncryptedString = EncryptedString + Convert.ToChar(Microsoft.VisualBasic.Asc("a") + (Convert.ToInt16(ch) + 3) - Microsoft.VisualBasic.Asc("z"))
                    Else
                        If (Convert.ToChar(Convert.ToInt16(ch) + 4) > "Z" And ch <= "Z") Then
                            EncryptedString = EncryptedString + Convert.ToChar(Microsoft.VisualBasic.Asc("A") + (Convert.ToInt16(ch) + 3) - Microsoft.VisualBasic.Asc("Z"))
                        Else
                            EncryptedString = EncryptedString + Convert.ToChar(Convert.ToInt16(ch) + 4)
                        End If
                    End If
                End If
            Else
                EncryptedString = EncryptedString + ch
            End If
        Next
        Return (EncryptedString)
    End Function

I don't have screenshot uploading privileges yet, so I have uploaded screenshot at this location. www(dot)freeimagehosting(dot)net/y6ea3

user2617874
  • 91
  • 1
  • 2
  • 12
  • 4
    Please, post your code. – varocarbas Nov 20 '13 at 09:52
  • Which If condition is giving trouble? – Brian Hooper Nov 20 '13 at 10:10
  • the third one If (Convert.ToChar(Convert.ToInt16(ch) + 4) > "Z" And ch <= "Z") Then – user2617874 Nov 20 '13 at 10:11
  • 1
    Its just not the right way, you cannot compare char with arithmetic sign, or if so, VS will probably check for its ascii. For example `(ch >= "a" = False) when ch = "W"c` – Nadeem_MK Nov 20 '13 at 10:14
  • when i check the value of this in the watch tab of vs2010 it evaluates it to true. – user2617874 Nov 20 '13 at 10:23
  • The arithmetical comparisons between char type variables you are performing do not make too much sense. You are bringing letters (small and big caps) and numbers together. Ideally, you should use functions like Asc and Chr to convert Char types into the ASCII equivalent; although VB.NET has some support even without doing that (-> correction of my previous statement). – varocarbas Nov 20 '13 at 10:23
  • And why it will not be true? What inputs are you considering? – varocarbas Nov 20 '13 at 10:27
  • i had run this in the watch tab Convert.ToChar(Convert.ToInt16(ch) + 4) > "Z" And ch <= "Z" – user2617874 Nov 20 '13 at 10:29
  • ?! What are the inputs and why you think that the behaviour of VB.NET is not right? – varocarbas Nov 20 '13 at 10:30
  • Let me put it in a different way: any character (letter or number) passes through the first condition and thus `If (Convert.ToChar((Convert.ToInt16(ch) + 4)) > "9" And ch <= "9") Then` is used also for letters (delivering unexpectable results) and `If (Convert.ToChar((Convert.ToInt16(ch) + 4)) > "z" And ch <= "z") Then` is used with both numbers and big-caps letters (which follow a different ordering than small caps ones). In summary: this condition is more or less like your question: pointless (no offense)... – varocarbas Nov 20 '13 at 10:43
  • I have added a URL to the screenshot. It is only option I am getting with my current privileges. Please advice. – user2617874 Nov 20 '13 at 10:44
  • ... if you are not sure about anything, just intend to avoid the high VB.NET flexibility (it is pretty "dangerous" for not too knowledgeable people). The expected operations for characters (as you see them "a" or "z" or "9") are equal or different. Any arithmetical operation is performed on account of their ASCII codes (scale ordering all the characters by bringing letters (small and big caps), symbols and numbers together) and thus you should use the corresponding functions to avoid misunderstandings Asc and Chr. VB.NET allows you to do "b"c >= "a"c, but this is just a shortcut to ASCII codes – varocarbas Nov 20 '13 at 10:47
  • Can you please indicate the exact character provoking this situation (don't expect us to analyse all the characters in this string) such that we can try to reproduce this problem (in any case, you should bear in mind that what is written via comments; this code does not make too much sense and it is unclearly structured). – varocarbas Nov 20 '13 at 10:51
  • this is a code to keygen for which i dont have a documentation.. – user2617874 Nov 20 '13 at 10:53
  • A reference to difference ASCII/Unicode to let my ideas above clear enough: http://stackoverflow.com/questions/10361579/are-unicode-and-ascii-characters-the-same – varocarbas Nov 20 '13 at 10:56
  • I tested these conditions and I am not able to reproduce what you show (not in VS 2010); the condition is true and the code behaves as expected (does NOT go to else). In any case, other than giving an opportunity to clarify things (which should be more or less clear, on the other hand), I don't see the point of this code and thus of wasting a single second on analysing it. – varocarbas Nov 20 '13 at 11:00
  • thank you for giving your time,but it is still behaving in the same manner, i am unable to post a screen in this question please have a look at this www(dot)freeimagehosting(dot)net/y6ea3 – user2617874 Nov 20 '13 at 11:07
  • As said, I saw your picture. But I have tested your conditions ch = "W"c with the condition you are complaining about and I cannot replicate what you show in your picture (with VS 2010). Also bear in mind that it is extremely unlikely (virtually impossible) that the code does not analyses a condition properly (and for this input, this condition has to be true); it might be the case that the immediate window, perhaps under very specific conditions (using "old VB code"), might deliver a wrong result; but you are claiming right the contrary! (immediate window right and condition wrong)... – varocarbas Nov 20 '13 at 11:11
  • ... what sounds impossible to me. When you think that a condition is skipped, the most likely scenario is that your analysis is wrong; when you provide graphical evidence of such a phenomenon... I don't know what I can say, other than: conditions are one of the basic structures of a programming language and not being 100% accurate would have lots of implications in the reliability of the given language. You have got quite a lot for what you have given out (originally, nothing), just intend to learn from this, start this code from scratch (writing it properly) and let's stop wasting time :) – varocarbas Nov 20 '13 at 11:13
  • thank you, are right i have converted this from an old vb project using vs2010. so if is there an update that can solve my problem this? – user2617874 Nov 20 '13 at 11:14
  • Please, don't misunderstand my statement. What I said was: I cannot reproduce your picture with VS 2010, exactly under your conditions. Also, even before trying, what you claim (i.e., immediate window being right and code acting wrongly) is virtually impossible; there is virtually no chance that a condition is not accounted for PERFECTLY in a given programming language (and, logically, in VB.NET). The immediate window (what you show in your picture) MIGHT be wrong (under very specific conditions, which do NOT happen in your code), but the execution will always be right. – varocarbas Nov 20 '13 at 11:17
  • It works well for me also. You should debug a bit more. Look at the condition more carefully not just ch. example, look at: asc(ch), asc("Z"), Convert.ToInt16(ch) + 4 – the_lotus Nov 20 '13 at 16:33

1 Answers1

0

It may be a long shot, But since you said that you are converting an old project in VS2010, It might be worth a try.

Maybe the problem is not with the source code, but with the environment itself. It might be possible that the character encoding for the older project is different that what is set in VS2010. I am saying this because default encoding in VS2005 was different that what is set in VS2010. In cryptology, these small things like project metadata also play their role which many tends to overlook, thinking that something is wrong with their source code.

So check that the project environment configuration is same. Specially Encoding (as pointed by @varocarbas). Play with it for a while.

Abhinav Kulshreshtha
  • 2,147
  • 1
  • 23
  • 38