如何将ActionScript中的字符串ByteArray编码为VB或C#
我想在如何将ActionScript中的字符串ByteArray编码为VB或C#,c#,vb.net,actionscript-3,apache-flex,sha256,C#,Vb.net,Actionscript 3,Apache Flex,Sha256,我想在ActionScript中使用mx.utils.SHA256或SHA256基于算法的密码对我的SQLite本地数据库哈希密码进行hash密码。这样我就可以将插入的密码与存储的数据库相匹配HashedPassword。为此,我也在使用盐 我想用ActionScript做我在VBcode中做的同样的事情 如何在ActionScript中从VB.NET更改以下内容? Encoding.UTF8.GetBytes("String") 字符串Salt-类型参数 System.Text.Enco
ActionScript
中使用mx.utils.SHA256或SHA256
基于算法的密码对我的SQLite
本地数据库哈希密码进行hash密码。这样我就可以将插入的密码与存储的数据库相匹配HashedPassword
。为此,我也在使用盐
我想用ActionScript
做我在VB
code中做的同样的事情
如何在ActionScript
中从VB.NET
更改以下内容?
Encoding.UTF8.GetBytes("String")
字符串Salt
-类型参数
System.Text.Encoding.Default.GetBytes(Salt.ToString.ToCharArray))
Convert.ToBase64String(HashOut)
字节散列
-类型参数
System.Text.Encoding.Default.GetBytes(Salt.ToString.ToCharArray))
Convert.ToBase64String(HashOut)
Array.Copy()
method根据指定的长度将一个字节数组复制到另一个字节数组:
Array.Copy(Data, DataAndSalt, Data.Length) // concatenation of Arrays in context of `ActionScript`
过程相当简单,但是Actionscript的SHA256
类的文档非常缺乏,您需要做的是:
将腌制的字符串写入ByteArray
调用SHA256.computeDigest()
例如:
我根据自己的需求创建了整个代码,这是在VB中完成的,现在两者都产生了相同的结果
ActionScript中的VB代码是Encoding.UTF8.GetBytes(“字符串”)
yourByteArray.writeMultiByte(“字符串”,“iso-8859-1”)代码>
ActionScript中的VB代码是System.Text.Encoding.Default.GetBytes(Salt.ToString.toCharray))
字节Salt.writeMultiByte(Salt,Salt)代码>
它是用于字节数组的串联,这是在 操作脚本由Array.Copy(Data,DataAndSalt,Data.Length)
var-DataAndSalt:ByteArray=newbytearray()代码>
DataAndSalt.writeBytes(数据)代码>
DataAndSalt.writeBytes(Salt)代码>
Data和Salt ByteArray将同时具有ByteArray now Data和Salt
数据是ByteArray,您可以通过连接多个字节数组。writeBytes(您的ByteArray)
- <代码>转换。TOBASE64字符串(散列)由以下功能完成
private static const BASE64_CHARS:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; public static function encodeByteArray(data:ByteArray):String { // Initialise output var output:String = ""; // Create data and output buffers var dataBuffer:Array; var outputBuffer:Array = new Array(4); // Rewind ByteArray data.position = 0; // while there are still bytes to be processed while (data.bytesAvailable > 0) { // Create new data buffer and populate next 3 bytes from data dataBuffer = new Array(); for (var i:uint = 0; i < 3 && data.bytesAvailable > 0; i++) { dataBuffer[i] = data.readUnsignedByte(); } // Convert to data buffer Base64 character positions and // store in output buffer outputBuffer[0] = (dataBuffer[0] & 0xfc) >> 2; outputBuffer[1] = ((dataBuffer[0] & 0x03) << 4) | ((dataBuffer[1]) >> 4); outputBuffer[2] = ((dataBuffer[1] & 0x0f) << 2) | ((dataBuffer[2]) >> 6); outputBuffer[3] = dataBuffer[2] & 0x3f; // If data buffer was short (i.e not 3 characters) then set // end character indexes in data buffer to index of '=' symbol. // This is necessary because Base64 data is always a multiple of // 4 bytes and is basses with '=' symbols. for (var j:uint = dataBuffer.length; j < 3; j++) { outputBuffer[j + 1] = 64; } // Loop through output buffer and add Base64 characters to // encoded data string for each character. for (var k:uint = 0; k < outputBuffer.length; k++) { output += BASE64_CHARS.charAt(outputBuffer[k]); } } // Return encoded data return output; }
private static const BASE64_CHARS:String=“abcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxyzo123456789+/=”; 公共静态函数encodeByteArray(数据:ByteArray):字符串{ //初始化输出 var输出:String=“”; //创建数据和输出缓冲区 var-dataBuffer:数组; var outputBuffer:Array=新数组(4); //按顺序倒带 data.position=0; //但仍有字节需要处理 而(data.bytesavable>0){ //创建新的数据缓冲区并从数据中填充接下来的3个字节 dataBuffer=新数组(); 对于(变量i:uint=0;i<3&&data.bytesavable>0;i++){ dataBuffer[i]=data.readUnsignedByte(); } //转换为数据缓冲区Base64字符位置和 //存储在输出缓冲区中 输出缓冲区[0]=(数据缓冲区[0]&0xfc)>>2; outputBuffer[1]=(数据缓冲区[0]&0x03]>4); outputBuffer[2]=(数据缓冲区[1]&0x0f)>6); outputBuffer[3]=数据缓冲区[2]&0x3f; //如果数据缓冲区短(即不是3个字符),则设置 //将数据缓冲区中的字符索引结束为“=”符号的索引。 //这是必要的,因为Base64数据始终是 //4个字节,并带有“=”符号。 对于(变量j:uint=dataBuffer.length;j<3;j++){ outputBuffer[j+1]=64; } //循环输出缓冲区并将Base64字符添加到 //每个字符的编码数据字符串。 for(变量k:uint=0;k
Udit Bhardwaj请注意,您不应该使用
编码。默认值
用于任何本应可移植的内容…语言说明:SHA是散列,而不是加密。此外,在调用SHA256.computeDigest()
之前,我必须说:bytes.position=0代码>或生成的哈希不正确。遍历mx.utils.SHA256
中的代码,它似乎试图从ByteArray
读回字节,在使用writeUTFBytes()
将字符串写入后,将其读取位置设置为数组的末尾。所以它仍然有效,只是生成了错误的散列。(无论如何,使用FLEX Builder 3。)