Skip to main content
Udit_bhardwaj
Inspiring
May 23, 2014
Answered

how can i use VB's Encoding.UTF8.GetBytes() and System.Text.Encoding.Default.GetBytes() in actionscript Can Somebody help me ??

  • May 23, 2014
  • 3 replies
  • 3832 views

How can i Use  it in Action script . my VB code is following

Encoding.UTF8.GetBytes("String") and System.Text.Encoding.Default.GetBytes("char array")

Thank You . !

Udit Bhardwaj

This topic has been closed for replies.
Correct answer Udit_bhardwaj

I fond the way to convert it accordingly

Encoding.UTF8.GetBytes("String")   it is in ActionScript :

YourByteArray.writeMultiByte("String", "iso-8859-1");

And

System.Text.Encoding.Default.GetBytes("char array")

  YourByteArray.writeMultiByte("String","String");

Thank you for your suggestions moccamaximum

Thank You !

Udit Bhardwaj

3 replies

Udit_bhardwaj
Inspiring
May 30, 2014

  YourByteArray.writeMultiByte("String", "utf-8"); 
Thanks for telling me a few important things ,  i will make it Correct. In my hashing process it is working fine . ok i will try with what you have suggested Me ..

Thanks A ton @Neverbirth

Udit Bhardwaj

Udit_bhardwaj
Udit_bhardwajAuthorCorrect answer
Inspiring
May 30, 2014

I fond the way to convert it accordingly

Encoding.UTF8.GetBytes("String")   it is in ActionScript :

YourByteArray.writeMultiByte("String", "iso-8859-1");

And

System.Text.Encoding.Default.GetBytes("char array")

  YourByteArray.writeMultiByte("String","String");

Thank you for your suggestions moccamaximum

Thank You !

Udit Bhardwaj

Inspiring
May 30, 2014

That isn't right, Bear in mind that your code may be working right now, but it may break on some use cases. ISO-8859-1 is not UTF-8 at all, you should use YourByteArray.writeMultiByte("String", "utf-8");

About YourByteArray.writeMultiByte("String","String"); it works because String is not a valid charset, so it uses the default code page, which is what you want, but be really careful with using it, if the machines writing and reading the data are different, the default code page could be different, and the encoding/decoding part of the string will work, but some later data processing or displaying may break because some bytes equal to different chars in different code pages. I've worked on projects where developers were not aware of this, or either didn't care/think it was going to be a problem in the future, and at the end the code had either to be changed or force the end users to modify their system's default code page (which brought other problems or complaints).

Udit_bhardwaj
Inspiring
May 30, 2014

  YourByteArray.writeMultiByte("String", "utf-8"); 
Thanks for telling me a few important things ,  i will make it Correct. In my hashing process it is working fine . ok i will try with what you have suggested Me ..

Thanks A ton @Neverbirth

Udit Bhardwaj

Inspiring
May 23, 2014
Udit_bhardwaj
Inspiring
May 23, 2014

Thanks but there is some error . i want to hash my salted password . I am providing my VB code here Please Help me sir!

Public Sub GetHashString(ByVal Data As String, ByRef Hash As String, ByVal Salt As String)

            'see GetHashAndSaltString for more details

            Dim HashOut() As Byte

            HashOut = ComputeHash(Encoding.UTF8.GetBytes(Data), System.Text.Encoding.Default.GetBytes(Salt.ToString.ToCharArray))

            Hash = Convert.ToBase64String(HashOut)

        End Sub

and

definition Of compute hash is

    Private Function ComputeHash(ByVal Data() As Byte, ByVal Salt() As Byte) As Byte()

            ' Allocate memory to store both the Data and Salt together

            Dim DataAndSalt() As Byte = New Byte(((Data.Length + SaltLength)) - 1) {}

            ' Copy both the data and salt into the new array

            Array.Copy(Data, DataAndSalt, Data.Length)

            Array.Copy(Salt, 0, DataAndSalt, Data.Length, SaltLength)

            ' Calculate the hash

            ' Compute hash value of our plain text with appended salt.

            Return HashProvider.ComputeHash(DataAndSalt)

        End Function

i want to do exactly through ActionScript

Thanks a Ton in advance

Inspiring
May 23, 2014

It`s not much of a use to "translate" your code example to AS3 because it is not complete.

A general rule of thumb for key differneces between AS3 and VB

1.AS3 uses brackets instead of End commands

2. construcrtor arguments are writen like so:

  _data:ByteArray,_salt:ByteArray

3.return values are obligatory for functions

4.visibiliy keywords (private, public, protected) are only valid within packages

function computeHash(_data:ByteArray,_salt:ByteArray):ByteArray{

...

}

some links that could be helpful for you:

Base64Encoder - Adobe ActionScript® 3 (AS3 Flex) API Reference

Adobe AIR 1.5 * Example: Generating and using an encryption key