1

I want to convert a password which is stored in binary to normal ASCII form so that i can read it. I need a VBscript for that and script should also return this de-crypted password

Eg: Encrypted Binary password: 00110001 00110010 00110011 00110100

De-crypted Original password : 1234

I Tried this

'Binary contains the binary password
dim S
For I = 1 To LenB(Binary)
S = S & Chr(AscB(MidB(Binary, I, 1)))
Next
MSGBOX S

But the output is

0

How can achieve this. Please help!!

Anant Vaibhav
  • 325
  • 3
  • 6
  • 20
  • 1
    That binary is in no way "encrypted". Also, what does "stored in binary" even mean? You cannot store anything in binary. Or, expressed differently, *everything* is stored in binary in a computer. So, what is it? A byte array? Where does it come from? If it's a byte array, which character encoding does it represent? – Tomalak Oct 29 '14 at 07:43

4 Answers4

5

If you are dealing with a byte array, you must know the character encoding before you can convert it to string. Without that knowledge the bytes will be converted to the wrong characters.

The ADODB.Stream object can handle byte arrays. Here is a function that that does that:

Const adTypeBinary = 1
Const adTypeText = 2
Const adModeReadWrite = 3

Function BytesToString(bytes, charset)
    With CreateObject("ADODB.Stream")
        .Mode = adModeReadWrite
        .Type = adTypeBinary
        .Open
        .Write bytes
        .Position = 0
        .Type = adTypeText
        .Charset = charset
        BytesToString = .ReadText
    End With
End Function

And here is how to use it:

MsgBox BytesToString(binary, "Windows-1252")

For the sake of completeness, this is the reverse operation:

Function StringToBytes(str, charset)
    With CreateObject("ADODB.Stream")
        .Mode = adModeReadWrite
        .Type = adTypeText
        .Charset = charset
        .Open
        .WriteText str
        .Position = 0
        .Type = adTypeBinary
        StringToBytes = .Read
    End With
End Function

Since your input seems to be a string like "00110001 00110010 00110011 00110100", here is a function to convert that to a byte array, which you can then use with BytesToString() shown above:

Function BinaryStringToBytes(binaryStr)
    Dim b, n, i, l

    l = GetLocale
    SetLocale 1031

    With CreateObject("ADODB.Stream")
        .Mode = adModeReadWrite
        .Charset = "Windows-1252"
        .Type = adTypeText
        .Open
        For Each b In Split(binaryStr, " ")
            If Len(b) <> 8 Or Replace(Replace(b, "0", ""), "1", "") <> "" Then
                ' invalid procedure call or argument
                Err.Raise 5, "BinaryStringToBytes", _
                    "Only stings of 8-blocks of 0s and 1s, " & _
                    "separated by a single space are accepted."
            End If
            n = 0
            For i = 0 To 7
                n = n + Mid(b, 8 - i, 1) * 2^i
            Next
            .WriteText Chr(n)
        Next
        .Position = 0
        .Type = adTypeBinary
        BinaryStringToBytes = .Read
    End With

    SetLocale l
End Function

Usage

Dim input, output

input = "00110001 00110010 00110011 00110100"
output = BytesToString(BinaryStringToBytes(input), "Windows-1252")

MsgBox output  ' -> "1234"

And, more importantly, it can handle multi-byte encodings properly:

input = "00110001 00110010 00110011 00110100 11000011 10100100"
output = BytesToString(BinaryStringToBytes(input), "UTF-8")

MsgBox output ' -> "1234ä"
Tomalak
  • 332,285
  • 67
  • 532
  • 628
3

try this code ;)

the code :

function BinaryToString(bin)  
 dim next_char 
 dim result
 dim i
 dim ascii
 For i = 1 To Len(bin) + 18 Step 8
        next_char = Mid(bin, i, 8)
        ascii = BinaryToLong(next_char)
        result = result & Chr(ascii)
    Next 
    BinaryToString=result
end function  

Function BinaryToLong(binary_value)
Dim hex_result 
Dim nibble_num 
Dim nibble_value
Dim factor 
Dim bit 

    binary_value = UCase(Trim(binary_value))
    If Left(binary_value, 2) = "&B" Then
        binary_value = Mid(binary_value, 3)
    End If

    binary_value = Replace(binary_value, " ", "")
    binary_value = Right(String(32, "0") & binary_value, 32)

    For nibble_num = 7 To 0 Step -1
        factor = 1
        nibble_value = 0

        For bit = 3 To 0 Step -1
            If Mid(binary_value,1 + nibble_num * 4 + bit, 1) = "1"  Then
                nibble_value = nibble_value + factor
            End If
            factor = factor * 2
        Next 'bit

        hex_result = Hex(nibble_value) & hex_result
    Next 'nibble_num

    BinaryToLong = CLng("&H" & hex_result)
End Function

usage:

response.Write(BinaryToString("00110001001100100011001100110100"))

don't forget to take off " " blank spaces from the binary string

Gianluca Colombo
  • 717
  • 17
  • 38
  • Please don't post two answers. (You can post two answers, but post code *you wrote*, along with explanations, not code you found on the Internet. That's not an achievement.) – Tomalak Oct 29 '14 at 07:57
  • I tried to delete the first one but i couldn't and I don't know why – Gianluca Colombo Oct 29 '14 at 08:08
  • You should be able to delete your own posts (just like you can edit them). Did you see an error message when you tried to delete it? – Tomalak Oct 29 '14 at 08:14
  • It seems deleted but probably not if you can see it.. Anyway, what i can see is that: [link](http://www.magellano.us/qc/immagine.png) – Gianluca Colombo Oct 29 '14 at 08:27
  • 1
    Right, you can see your own deleted posts, so you can undelete them if you change your mind. Looks good. – Tomalak Oct 29 '14 at 08:39
1

If I'm right, all you're after is converting a binary number to decimal (eg 0100 -> 4)?

dim binary, n, s
binary= "00110001"

For s = 1 To Len(binary)
    n = n + (Mid(binary, Len(binary) - s + 1, 1) * (2 ^ (s - 1)))
Next 's

WScript.Echo binary & " = " & n

outputs

00110001 = 49

Converted from here: http://www.vb-helper.com/howto_decimal_to_binary.html

  • This answer is completely misleading. The "encryption" is not "adding 48 to each digit before converting to binary". That's called ASCII. The ASCII code for the character "1" is 49. So your conversation code is wrong and falls flat for characters outside of the ASCII range (everything beyond `01111111`, i.e. 127 in binary). @Anant You really, really should not use this answer. – Tomalak Oct 29 '14 at 10:06
  • @Tomalak I'll remove that last bit then – oracle certified professional Oct 29 '14 at 10:08
  • I'm sorry to say that does not make your answer correct, though. ;-) – Tomalak Oct 29 '14 at 10:08
  • @Tomalak You mean the OP needs to convert the result to an ascii character? – oracle certified professional Oct 29 '14 at 10:09
  • From how I read the question he has a password (i.e., a string) in binary (unfortunately he did not explain what "binary" means for him) and wants to convert that back to the original string. And your function does not really do that - it converts an 8-character-string consisting of 0s and 1s to a number. That number can be *interpreted as* a character code, but it's not clear in which code table to use to decode it. ASCII would work for a value range of 0-127, but with values 128-255 all bets are off. On top of that, multi-byte encodings like UTF-8 will not work *at all* with this code. – Tomalak Oct 29 '14 at 10:20
  • @Tomalak without knowing the charset details, I think that this is as far as I can asnwer the question, however. – oracle certified professional Oct 29 '14 at 10:23
  • Knowing the byte encoding is not necessary to write a function that works. Knowing that byte encoding is an issue that must be handled is. I've edited my answer with an approach that does the right thing. – Tomalak Oct 29 '14 at 11:53
-4

There are so many ways.

If it's a binary reg value then from Help (you did read it, didn't you)

The RegRead method returns values of the following five types.

Type  Description  In the Form of  
REG_SZ
 A string
 A string

REG_DWORD
 A number
 An integer

REG_BINARY
 A binary value
 A VBArray of integers

REG_EXPAND_SZ
 An expandable string (e.g., "%windir%\\calc.exe")
 A string

REG_MULTI_SZ
 An array of strings
 A VBArray of strings

If a string, split on space (gives you an array of strings). The least significant bit is 2^0, 2^1, ..., 2^7.


EDIT

The normal way, not the only way though, to store a password, is to dump it in the registry.

Reading it gives you an array, not a scalar variable. So ...

The second method, handles cases where it's stored in a file.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • Minus 1 because you are assuming the binary value is coming from the reg.whilst there is no indication whatsoever supporting such an assumption. Based on this wrong assumption, you are not answering the question. On a side, dumping a password As-Is in the registry is certainly NOT the normal way. You would at least encrypt it. – Jean-François Feb 09 '20 at 14:30