Jump to content
Sign in to follow this  
Innovative

_StringEncrypt is returning ""

Recommended Posts

Hi Jos, I'm in Belgium now :) This is not an excuse not having some Asian testing environment. :mad: On winxp you have to do no more than few clicks to have it. I'm working on an English winXP with Chinese input method enabled. The interface is still in English. I have no any problem to reproduce this issue. If you like, I'd like to tell you my language settings. I think you could also reproduce it with ease. Then the problem should be almost solved I guess, because you know how to do it. :D

Hi,skinnywhiteguy. I'd like to have a try for that. Could you please give me the link of which post you refer to?

Although I remember I tried almost all the testing cases in this thread, I'd like to help to figure this issue out.

In fact everyone of you could reproduce the issue if it's merely caused by the language settings on WinXP.

Open "regional and language options" in the control panel, on tab "regional options" change "standard and formats" to "Chinese(PRC)", leave the "location" on this tab untouched(I don't think this setting causes problem, I have "belgium" there). Then go to the next tab "languages", I have "install files for East Asian languages" checked , others unchecked. (This step may need the origional OS CD or internet download I don't remember.) Finally go to the last tab "advanced", select again "Chinese(PRC)" under "language for non-unicode program" , under "code page conversion tables", maKe sure code page 10001, 10002, 10003, 10008,20936,50227,50229,52936,54936,936,950 are checked(normally these are automatically checked if you have done the previous steps). restart. Now you have an international language testing system. And you should be able to reproduce the issue of this thread.

Note: even you keep the language setting as I said above, it will not bother you too much. Because most Asian languages include the western characters, no displaying problem. If you don't like them, just go to control panel again, switch the setting back to what you like. This time you do not need to install anything since you have everything installed already.

To be honest, I saw many softwares developed by western people didn't take care of Asian languages. Just recent years because of the prevailing of UTF encoding, things start being changed. But still not perfect. You can also see this problem in may web script projects. On many webserver in US, you have to manually change the script to use UTF8 to communicate with the database server, causing many annoying problems. You know what, this creates a huge market in Asian country like China for CHinese IT companies, meaning those US companies losing a lot of potential users, although they provide normally lower hosting fee and better services. I really wonder why they simply ignore this. :P So I suggest every software developer should not diminish the importance of internationalization if you really wish more people would use it.

I won't argue your last statement and fully agree, but I have given up even trying to replicate issues reported by a single person which on top of that required so many post just to do a simple debugging. I realize I am putting the OP on the spot a bit here, but that really is not my intent. The bigger message here is: If you want us to help you, you will need to help us with information as just provided by you (Ting). Try making our lives as easy as possible by giving short and working scripts to replicate issues and the steps to perform.

I will have a go with these instructions to see if I can get a test environment going and work a bit on the UDF.

No promises though about timing :P

Share this post


Link to post
Share on other sites

What about this solution ?

This doesn't work for ANSI version, but works on the test environment I setup following the specified specs:

#include <String.au3>
$String = "The quick brown fox jumps over the lazy dog."
$Key = "Password"
ConsoleWrite('@@ _StringEncrypt(1, $string, $key ) = ' & _StringEncrypt(1, $String, $Key) & @CRLF & '>Error code: ' & @error & @CRLF);### Debug Console
ConsoleWrite('@@ _StringEncrypt(0, _StringEncrypt(1, $string, $key ), $key ) = ' & _StringEncrypt(0, _StringEncrypt(1, $String, $Key), $Key) & @CRLF & '>Error code: ' & @error & @CRLF);### Debug Console
ConsoleWrite('@@ _StringEncryptW(1, $string, $key ) = ' & _StringEncryptW(1, $String, $Key) & @CRLF & '>Error code: ' & @error & @CRLF);### Debug Console
ConsoleWrite('@@ _StringEncryptW(0, _StringEncryptW(1, $string, $key ), $key ) = ' & _StringEncryptW(0, _StringEncryptW(1, $String, $Key), $Key) & @CRLF & '>Error code: ' & @error & @CRLF);### Debug Console

Func _StringEncryptW($i_Encrypt, $s_EncryptText, $s_EncryptPassword, $i_EncryptLevel = 1)
    If $i_Encrypt <> 0 And $i_Encrypt <> 1 Then
        SetError(1)
        Return ''
    ElseIf $s_EncryptText = '' Or $s_EncryptPassword = '' Then
        SetError(1)
        Return ''
    Else
        If Number($i_EncryptLevel) <= 0 Or Int($i_EncryptLevel) <> $i_EncryptLevel Then $i_EncryptLevel = 1
        Local $v_EncryptModified
        Local $i_EncryptCountH
        Local $i_EncryptCountG
        Local $v_EncryptSwap
        Local $av_EncryptBox[256][2]
        Local $i_EncryptCountA
        Local $i_EncryptCountB
        Local $i_EncryptCountC
        Local $i_EncryptCountD
        Local $i_EncryptCountE
        Local $v_EncryptCipher
        Local $v_EncryptCipherBy
        If $i_Encrypt = 1 Then
            For $i_EncryptCountF = 0 To $i_EncryptLevel Step 1
                $i_EncryptCountG = ''
                $i_EncryptCountH = ''
                $v_EncryptModified = ''
                For $i_EncryptCountG = 1 To StringLen($s_EncryptText)
                    If $i_EncryptCountH = StringLen($s_EncryptPassword) Then
                        $i_EncryptCountH = 1
                    Else
                        $i_EncryptCountH += 1
                    EndIf
                    $v_EncryptModified = $v_EncryptModified & ChrW(BitXOR(AscW(StringMid($s_EncryptText, $i_EncryptCountG, 1)), AscW(StringMid($s_EncryptPassword, $i_EncryptCountH, 1)), 255))
                Next
                $s_EncryptText = $v_EncryptModified
                $i_EncryptCountA = ''
                $i_EncryptCountB = 0
                $i_EncryptCountC = ''
                $i_EncryptCountD = ''
                $i_EncryptCountE = ''
                $v_EncryptCipherBy = ''
                $v_EncryptCipher = ''
                $v_EncryptSwap = ''
                $av_EncryptBox = ''
                Local $av_EncryptBox[256][2]
                For $i_EncryptCountA = 0 To 255
                    $av_EncryptBox[$i_EncryptCountA][1] = AscW(StringMid($s_EncryptPassword, Mod($i_EncryptCountA, StringLen($s_EncryptPassword)) + 1, 1))
                    $av_EncryptBox[$i_EncryptCountA][0] = $i_EncryptCountA
                Next
                For $i_EncryptCountA = 0 To 255
                    $i_EncryptCountB = Mod(($i_EncryptCountB + $av_EncryptBox[$i_EncryptCountA][0] + $av_EncryptBox[$i_EncryptCountA][1]), 256)
                    $v_EncryptSwap = $av_EncryptBox[$i_EncryptCountA][0]
                    $av_EncryptBox[$i_EncryptCountA][0] = $av_EncryptBox[$i_EncryptCountB][0]
                    $av_EncryptBox[$i_EncryptCountB][0] = $v_EncryptSwap
                Next
                For $i_EncryptCountA = 1 To StringLen($s_EncryptText)
                    $i_EncryptCountC = Mod(($i_EncryptCountC + 1), 256)
                    $i_EncryptCountD = Mod(($i_EncryptCountD + $av_EncryptBox[$i_EncryptCountC][0]), 256)
                    $i_EncryptCountE = $av_EncryptBox[Mod(($av_EncryptBox[$i_EncryptCountC][0] + $av_EncryptBox[$i_EncryptCountD][0]), 256)][0]
                    $v_EncryptCipherBy = BitXOR(AscW(StringMid($s_EncryptText, $i_EncryptCountA, 1)), $i_EncryptCountE)
                    $v_EncryptCipher &= Hex($v_EncryptCipherBy, 2)
                Next
                $s_EncryptText = $v_EncryptCipher
            Next
        Else
            For $i_EncryptCountF = 0 To $i_EncryptLevel Step 1
                $i_EncryptCountB = 0
                $i_EncryptCountC = ''
                $i_EncryptCountD = ''
                $i_EncryptCountE = ''
                $v_EncryptCipherBy = ''
                $v_EncryptCipher = ''
                $v_EncryptSwap = ''
                $av_EncryptBox = ''
                Local $av_EncryptBox[256][2]
                For $i_EncryptCountA = 0 To 255
                    $av_EncryptBox[$i_EncryptCountA][1] = AscW(StringMid($s_EncryptPassword, Mod($i_EncryptCountA, StringLen($s_EncryptPassword)) + 1, 1))
                    $av_EncryptBox[$i_EncryptCountA][0] = $i_EncryptCountA
                Next
                For $i_EncryptCountA = 0 To 255
                    $i_EncryptCountB = Mod(($i_EncryptCountB + $av_EncryptBox[$i_EncryptCountA][0] + $av_EncryptBox[$i_EncryptCountA][1]), 256)
                    $v_EncryptSwap = $av_EncryptBox[$i_EncryptCountA][0]
                    $av_EncryptBox[$i_EncryptCountA][0] = $av_EncryptBox[$i_EncryptCountB][0]
                    $av_EncryptBox[$i_EncryptCountB][0] = $v_EncryptSwap
                Next
                For $i_EncryptCountA = 1 To StringLen($s_EncryptText) Step 2
                    $i_EncryptCountC = Mod(($i_EncryptCountC + 1), 256)
                    $i_EncryptCountD = Mod(($i_EncryptCountD + $av_EncryptBox[$i_EncryptCountC][0]), 256)
                    $i_EncryptCountE = $av_EncryptBox[Mod(($av_EncryptBox[$i_EncryptCountC][0] + $av_EncryptBox[$i_EncryptCountD][0]), 256)][0]
                    $v_EncryptCipherBy = BitXOR(Dec(StringMid($s_EncryptText, $i_EncryptCountA, 2)), $i_EncryptCountE)
                    $v_EncryptCipher = $v_EncryptCipher & ChrW($v_EncryptCipherBy)
                Next
                $s_EncryptText = $v_EncryptCipher
                $i_EncryptCountG = ''
                $i_EncryptCountH = ''
                $v_EncryptModified = ''
                For $i_EncryptCountG = 1 To StringLen($s_EncryptText)
                    If $i_EncryptCountH = StringLen($s_EncryptPassword) Then
                        $i_EncryptCountH = 1
                    Else
                        $i_EncryptCountH += 1
                    EndIf
                    $v_EncryptModified &= ChrW(BitXOR(AscW(StringMid($s_EncryptText, $i_EncryptCountG, 1)), AscW(StringMid($s_EncryptPassword, $i_EncryptCountH, 1)), 255))
                Next
                $s_EncryptText = $v_EncryptModified
            Next
        EndIf
        Return $s_EncryptText
    EndIf
EndFunc ;==>_StringEncryptW

Jos

Edited by Jos

Share this post


Link to post
Share on other sites

What about this solution ?

This doesn't work for ANSI version, but works on the test environment I setup following the specified specs:

#include <String.au3>
$String = "The quick brown fox jumps over the lazy dog."
$Key = "Password"
ConsoleWrite('@@ _StringEncrypt(1, $string, $key ) = ' & _StringEncrypt(1, $String, $Key) & @CRLF & '>Error code: ' & @error & @CRLF);### Debug Console
ConsoleWrite('@@ _StringEncrypt(0, _StringEncrypt(1, $string, $key ), $key ) = ' & _StringEncrypt(0, _StringEncrypt(1, $String, $Key), $Key) & @CRLF & '>Error code: ' & @error & @CRLF);### Debug Console
ConsoleWrite('@@ _StringEncryptW(1, $string, $key ) = ' & _StringEncryptW(1, $String, $Key) & @CRLF & '>Error code: ' & @error & @CRLF);### Debug Console
ConsoleWrite('@@ _StringEncryptW(0, _StringEncryptW(1, $string, $key ), $key ) = ' & _StringEncryptW(0, _StringEncryptW(1, $String, $Key), $Key) & @CRLF & '>Error code: ' & @error & @CRLF);### Debug Console

Func _StringEncryptW($i_Encrypt, $s_EncryptText, $s_EncryptPassword, $i_EncryptLevel = 1)
    If $i_Encrypt <> 0 And $i_Encrypt <> 1 Then
        SetError(1)
        Return ''
    ElseIf $s_EncryptText = '' Or $s_EncryptPassword = '' Then
        SetError(1)
        Return ''
    Else
        If Number($i_EncryptLevel) <= 0 Or Int($i_EncryptLevel) <> $i_EncryptLevel Then $i_EncryptLevel = 1
        Local $v_EncryptModified
        Local $i_EncryptCountH
        Local $i_EncryptCountG
        Local $v_EncryptSwap
        Local $av_EncryptBox[256][2]
        Local $i_EncryptCountA
        Local $i_EncryptCountB
        Local $i_EncryptCountC
        Local $i_EncryptCountD
        Local $i_EncryptCountE
        Local $v_EncryptCipher
        Local $v_EncryptCipherBy
        If $i_Encrypt = 1 Then
            For $i_EncryptCountF = 0 To $i_EncryptLevel Step 1
                $i_EncryptCountG = ''
                $i_EncryptCountH = ''
                $v_EncryptModified = ''
                For $i_EncryptCountG = 1 To StringLen($s_EncryptText)
                    If $i_EncryptCountH = StringLen($s_EncryptPassword) Then
                        $i_EncryptCountH = 1
                    Else
                        $i_EncryptCountH += 1
                    EndIf
                    $v_EncryptModified = $v_EncryptModified & ChrW(BitXOR(AscW(StringMid($s_EncryptText, $i_EncryptCountG, 1)), AscW(StringMid($s_EncryptPassword, $i_EncryptCountH, 1)), 255))
                Next
                $s_EncryptText = $v_EncryptModified
                $i_EncryptCountA = ''
                $i_EncryptCountB = 0
                $i_EncryptCountC = ''
                $i_EncryptCountD = ''
                $i_EncryptCountE = ''
                $v_EncryptCipherBy = ''
                $v_EncryptCipher = ''
                $v_EncryptSwap = ''
                $av_EncryptBox = ''
                Local $av_EncryptBox[256][2]
                For $i_EncryptCountA = 0 To 255
                    $av_EncryptBox[$i_EncryptCountA][1] = AscW(StringMid($s_EncryptPassword, Mod($i_EncryptCountA, StringLen($s_EncryptPassword)) + 1, 1))
                    $av_EncryptBox[$i_EncryptCountA][0] = $i_EncryptCountA
                Next
                For $i_EncryptCountA = 0 To 255
                    $i_EncryptCountB = Mod(($i_EncryptCountB + $av_EncryptBox[$i_EncryptCountA][0] + $av_EncryptBox[$i_EncryptCountA][1]), 256)
                    $v_EncryptSwap = $av_EncryptBox[$i_EncryptCountA][0]
                    $av_EncryptBox[$i_EncryptCountA][0] = $av_EncryptBox[$i_EncryptCountB][0]
                    $av_EncryptBox[$i_EncryptCountB][0] = $v_EncryptSwap
                Next
                For $i_EncryptCountA = 1 To StringLen($s_EncryptText)
                    $i_EncryptCountC = Mod(($i_EncryptCountC + 1), 256)
                    $i_EncryptCountD = Mod(($i_EncryptCountD + $av_EncryptBox[$i_EncryptCountC][0]), 256)
                    $i_EncryptCountE = $av_EncryptBox[Mod(($av_EncryptBox[$i_EncryptCountC][0] + $av_EncryptBox[$i_EncryptCountD][0]), 256)][0]
                    $v_EncryptCipherBy = BitXOR(AscW(StringMid($s_EncryptText, $i_EncryptCountA, 1)), $i_EncryptCountE)
                    $v_EncryptCipher &= Hex($v_EncryptCipherBy, 2)
                Next
                $s_EncryptText = $v_EncryptCipher
            Next
        Else
            For $i_EncryptCountF = 0 To $i_EncryptLevel Step 1
                $i_EncryptCountB = 0
                $i_EncryptCountC = ''
                $i_EncryptCountD = ''
                $i_EncryptCountE = ''
                $v_EncryptCipherBy = ''
                $v_EncryptCipher = ''
                $v_EncryptSwap = ''
                $av_EncryptBox = ''
                Local $av_EncryptBox[256][2]
                For $i_EncryptCountA = 0 To 255
                    $av_EncryptBox[$i_EncryptCountA][1] = AscW(StringMid($s_EncryptPassword, Mod($i_EncryptCountA, StringLen($s_EncryptPassword)) + 1, 1))
                    $av_EncryptBox[$i_EncryptCountA][0] = $i_EncryptCountA
                Next
                For $i_EncryptCountA = 0 To 255
                    $i_EncryptCountB = Mod(($i_EncryptCountB + $av_EncryptBox[$i_EncryptCountA][0] + $av_EncryptBox[$i_EncryptCountA][1]), 256)
                    $v_EncryptSwap = $av_EncryptBox[$i_EncryptCountA][0]
                    $av_EncryptBox[$i_EncryptCountA][0] = $av_EncryptBox[$i_EncryptCountB][0]
                    $av_EncryptBox[$i_EncryptCountB][0] = $v_EncryptSwap
                Next
                For $i_EncryptCountA = 1 To StringLen($s_EncryptText) Step 2
                    $i_EncryptCountC = Mod(($i_EncryptCountC + 1), 256)
                    $i_EncryptCountD = Mod(($i_EncryptCountD + $av_EncryptBox[$i_EncryptCountC][0]), 256)
                    $i_EncryptCountE = $av_EncryptBox[Mod(($av_EncryptBox[$i_EncryptCountC][0] + $av_EncryptBox[$i_EncryptCountD][0]), 256)][0]
                    $v_EncryptCipherBy = BitXOR(Dec(StringMid($s_EncryptText, $i_EncryptCountA, 2)), $i_EncryptCountE)
                    $v_EncryptCipher = $v_EncryptCipher & ChrW($v_EncryptCipherBy)
                Next
                $s_EncryptText = $v_EncryptCipher
                $i_EncryptCountG = ''
                $i_EncryptCountH = ''
                $v_EncryptModified = ''
                For $i_EncryptCountG = 1 To StringLen($s_EncryptText)
                    If $i_EncryptCountH = StringLen($s_EncryptPassword) Then
                        $i_EncryptCountH = 1
                    Else
                        $i_EncryptCountH += 1
                    EndIf
                    $v_EncryptModified &= ChrW(BitXOR(AscW(StringMid($s_EncryptText, $i_EncryptCountG, 1)), AscW(StringMid($s_EncryptPassword, $i_EncryptCountH, 1)), 255))
                Next
                $s_EncryptText = $v_EncryptModified
            Next
        EndIf
        Return $s_EncryptText
    EndIf
EndFunc;==>_StringEncryptW

Jos

Thanks, Jos!

The function _StringEncryptW indeed does encryption on my computer, but doesn't give right decrypted string.

It has no problem if I copy&run your script. However, when I input some Asian characters, the decrypted string is garbage. Please have a look of the attached script (UTF8 encoded) and screen snapshot. See if you could get the same characters displayed on your screen.

stringencryptw.au3

post-23882-1214833987_thumb.png

Share this post


Link to post
Share on other sites

The post? It's about 12 posts up from this one. If you read the whole thread to learn about what was said, you'd have past right by it.

Sorry, SkinnyWhiteGuy. I tried the function you gave. For English words, no problem, it works. For Asian words, encryption works, but decrypted string is wrong. If you'd like to reproduce the error, please run the attached script. (UTF8 encoding)

temp_9.au3

Share this post


Link to post
Share on other sites

Ting, just tried it, and it works on my English Windows, all I had to do was add a line at the top to convince SciTE to use utf-8 to show it so I could be sure.

The function I had also uses flags, like what you asked PsaltyDS about, it's the 5th parameter. Try setting those around and see what changes with it.

Share this post


Link to post
Share on other sites

Thanks, Jos!

The function _StringEncryptW indeed does encryption on my computer, but doesn't give right decrypted string.

It has no problem if I copy&run your script. However, when I input some Asian characters, the decrypted string is garbage. Please have a look of the attached script (UTF8 encoded) and screen snapshot. See if you could get the same characters displayed on your screen.

mmm, yea, that would be a problem for the simple fact that those are "2 character" characters (UNICODE) which this function doesn't handle properly.

This UDF is not my design and its probably pretty difficult to modified to accommodate a mix of Ascii and Unicode characters.

Jos

Share this post


Link to post
Share on other sites

Ting, just tried it, and it works on my English Windows, all I had to do was add a line at the top to convince SciTE to use utf-8 to show it so I could be sure.

The function I had also uses flags, like what you asked PsaltyDS about, it's the 5th parameter. Try setting those around and see what changes with it.

Great! It works here too. I didn't notice the fifth argument. :)

As a summary:

if I encode the source au3 file (which contains Asian characters) in unicode such as utf8 and perform encryption/decryption with the function given by SkinnyWhiteGuy using the CORRESPONDING encoding flag, the problem discussed in this post is removed perfectly!

Thanks to the nice job by SkinnyWhiteGuy! I suggest this function should be added in the next release of AutoIt. I don't know if it's elegant enough, but it works.

(run the attached script you will see how it works.)

temp_9.au3

Edited by Ting

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...