我正在尝试将ActionScript3代码转换为C#,这很重要。但是,在尝试转换其中一个函数时,我尝试将十六进制字符串转换为int时出现标题错误。
基本上,该代码应该获取诸如用户数据之类的信息,然后执行某些操作,最后返回Base64编码的文本。我知道的主要错误是在“ loc9 = Convert.ToInt32(loc8,16);”部分。这就是我得到标题中所述错误的地方。我曾尝试研究其他人在类似问题上遇到的类似问题,但看起来似乎并不相同,并没有真正帮助我。
(顺便说一句,如果听起来不太清楚,抱歉,请改正我,或者在不理解的情况下问更多问题)
Screenshot of error when called
我的C#代码:
private static string hasher(string input)
{
MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
byte[] hash = md5.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("x2"));
}
return sb.ToString();
}
public static string p(string param1)
{
var loc6 = "";
var loc7 = "";
var loc8 = "";
var loc9 = 0;
var loc2 = hasher(param1);
var loc4 = 0;
MemoryStream loc0 = new MemoryStream();
var loc3 = new byte[] { };
while(loc4 < loc2.Length * 2)
{
loc6 = loc2.Substring(loc4, loc4 + 1);
loc7 = loc2.Substring(loc4 + 1, loc4 + 2);
loc8 = "0x" + loc6 + loc7;
loc9 = Convert.ToInt32(loc8, 16);
new BinaryWriter(loc0).Write(loc9);
loc4 = loc4 + 2;
}
loc0.Position = 0;
loc3 = loc0.ToArray();
return Convert.ToBase64String(loc3, 0, 16);
}
public string calculateFromNewActorCreationData(string username, string password, byte[] small, byte[] full)
{
return calculateFromStrings(username, password, small, full);
}
public string calculateFromStrings(string param1, string param2, object param3, object param4)
{
var loc5 = param1 + param2 + fromByteArray(param3 as byte[]) + fromByteArray(param4 as byte[]) + p();
return p(loc5);
}
private string fromByteArray(byte[] param1)
{
var ms = new MemoryStream(param1);
List<byte> list2 = new List<byte>();
if (param1.Length <= 20)
return HexStringFromBytes(param1);
var loc3 = new byte[] { };
var loc4 = param1.Length / 20;
var loc5 = 0;
while (loc5 < 20)
{
ms.Position = loc4 * loc5;
list2.Add(new BinaryReader(ms).ReadByte());
loc5++;
}
loc3 = list2.ToArray();
return HexStringFromBytes(loc3);
}
private static string HexStringFromBytes(byte[] bytes)
{
var sb = new StringBuilder();
foreach (byte b in bytes)
{
var hex = b.ToString("x2");
sb.Append(hex);
}
return sb.ToString();
}
private string p()
{
MemoryStream stream = new MemoryStream();
new BinaryWriter(stream).Write(120);
new BinaryWriter(stream).Write(-38);
new BinaryWriter(stream).Write(99);
new BinaryWriter(stream).Write(16);
new BinaryWriter(stream).Write(32);
new BinaryWriter(stream).Write(51);
new BinaryWriter(stream).Write(41);
new BinaryWriter(stream).Write(-110);
new BinaryWriter(stream).Write(12);
new BinaryWriter(stream).Write(50);
new BinaryWriter(stream).Write(81);
new BinaryWriter(stream).Write(73);
new BinaryWriter(stream).Write(49);
new BinaryWriter(stream).Write(-56);
new BinaryWriter(stream).Write(13);
new BinaryWriter(stream).Write(48);
new BinaryWriter(stream).Write(54);
new BinaryWriter(stream).Write(54);
new BinaryWriter(stream).Write(14);
new BinaryWriter(stream).Write(48);
new BinaryWriter(stream).Write(46);
new BinaryWriter(stream).Write(2);
new BinaryWriter(stream).Write(0);
new BinaryWriter(stream).Write(45);
new BinaryWriter(stream).Write(-30);
new BinaryWriter(stream).Write(4);
new BinaryWriter(stream).Write(-16);
stream.Position = 0;
return Encoding.UTF8.GetString(stream.ToArray());
}
ActionScript3代码:
private static function p(param1:String) : String
{
var _loc6_:String = null;
var _loc7_:String = null;
var _loc8_:String = null;
var _loc9_:int = 0;
var _loc2_:String = MD5.hash(param1);
var _loc3_:ByteArray = new ByteArray();
var _loc4_:int = 0;
while(_loc4_ < _loc2_.length * 2)
{
_loc6_ = _loc2_.slice(_loc4_,_loc4_ + 1);
_loc7_ = _loc2_.slice(_loc4_ + 1,_loc4_ + 2);
_loc8_ = "0x" + _loc6_ + _loc7_;
_loc9_ = int(_loc8_);
_loc3_.writeByte(_loc9_);
_loc4_ = _loc4_ + 2;
}
_loc3_.position = 0;
var _loc5_:Base64Encoder = new Base64Encoder();
_loc5_.encodeBytes(_loc3_,0,16);
return _loc5_.toString();
}
public function calculateFromNewActorCreationData(param1:NewActorCreationData, param2:ByteArray, param3:ByteArray) : String
{
return this.calculateFromStrings(param1.ChosenActorName,param1.ChosenPassword,param2,param3);
}
public function calculateFromStrings(param1:String, param2:String, param3:Object, param4:Object) : String
{
var _loc5_:String = param1 + param2 + this.fromByteArray(param3) + this.fromByteArray(param4) + this.p();
return p(_loc5_);
}
private function fromByteArray(param1:Object) : String
{
if(param1 == null)
{
return "";
}
var _loc2_:int = 20;
if(param1.length <= _loc2_)
{
return Hex.fromArray(param1 as ByteArray);
}
var _loc3_:ByteArray = new ByteArray();
var _loc4_:int = param1.length / _loc2_;
var _loc5_:int = 0;
while(_loc5_ < _loc2_)
{
param1.position = _loc4_ * _loc5_;
_loc3_.writeByte(param1.readByte());
_loc5_++;
}
return Hex.fromArray(_loc3_);
}
private function p() : String
{
var _loc1_:ByteArray = new ByteArray();
_loc1_.writeByte(120);
_loc1_.writeByte(-38);
_loc1_.writeByte(99);
_loc1_.writeByte(16);
_loc1_.writeByte(12);
_loc1_.writeByte(51);
_loc1_.writeByte(41);
_loc1_.writeByte(-118);
_loc1_.writeByte(12);
_loc1_.writeByte(50);
_loc1_.writeByte(81);
_loc1_.writeByte(73);
_loc1_.writeByte(49);
_loc1_.writeByte(-56);
_loc1_.writeByte(13);
_loc1_.writeByte(48);
_loc1_.writeByte(54);
_loc1_.writeByte(54);
_loc1_.writeByte(14);
_loc1_.writeByte(48);
_loc1_.writeByte(46);
_loc1_.writeByte(2);
_loc1_.writeByte(0);
_loc1_.writeByte(45);
_loc1_.writeByte(-30);
_loc1_.writeByte(4);
_loc1_.writeByte(-16);
_loc1_.uncompress();
_loc1_.position = 0;
return _loc1_.readUTF();
}
最后,我希望能够调用将返回的Base64编码数据显示在MessageBox中(使用messagebox作为测试)的函数,而不是弹出任何错误。
P.S-除了这段代码的主要问题之外,我还觉得我转换的其他函数不是完美的,或者可能不一样。因此,如果我的主要问题可以解决,那么如果有人还可以再次检查我代码的其他功能,请确保它们准确无误,这是令人惊奇的,并在此先感谢。
答案 0 :(得分:2)
从总体上看,似乎AS3代码正在尝试以最坏的方式将MD5.hash
结果转换为Base64编码的字符串(我相信可以一行完成。)
因此,与其复制所有代码以将哈希转换为十六进制字符串,而仅是将C#结果直接转换为二进制数组,不如将其转换为十六进制字符串:
public static string p(string param1) {
byte[] loc3 = System.Security.Cryptography.MD5.Create().ComputeHash(System.Text.Encoding.ASCII.GetBytes(param1));
return Convert.ToBase64String(loc3, 0, 16);
}