简体   繁体   English

如何解决“对于UInt32,值太大或太小”的C#

[英]How to fix “Value was either too large or too small for a UInt32” C#

I am trying to convert ActionScript3 code to C# that's like the main thing. 我正在尝试将ActionScript3代码转换为C#,就像最主要的事情一样。 However, with trying to convert one of the functions I got the error which is in the title when I was trying to convert a hexadecimal string to an int. 但是,尝试转换其中一个函数时,出现错误,当我尝试将十六进制字符串转换为int时,标题中出现了错误。

Basically, this code is supposed to take information for example user data and then do somethings and in the end return Base64 encoded text. 基本上,该代码应该获取诸如用户数据之类的信息,然后执行某些操作,最后返回Base64编码的文本。 The main error that I am aware of is at the part where "loc9 = Convert.ToInt32(loc8, 16);" 我知道的主要错误是在“ loc9 = Convert.ToInt32(loc8,16);”部分。 is as that is where I am getting the error said in the title. 这就是我得到标题中所述错误的地方。 I have tried researching similar issues others have had with something like this, but it just didn't seem the same and didn't really help me out. 我曾尝试研究其他人在类似问题上遇到的类似问题,但看起来似乎并不相同,也并没有真正帮助我。

(Btw I am sorry if this doesn't sound so clear, correct me or ask more questions if not understood) (顺便说一句,如果听起来不太清楚,我很抱歉,请改正我,或者如果不理解,请问更多问题)

Screenshot of error when called 调用时出现错误的屏幕截图

My C# Code: 我的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 Code: 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();
      }

What I am expecting in the end is to be able to call the function having the returned Base64 encoded data show in a MessageBox (using messagebox as a test) instead of any errors popping up. 最后,我期望能够调用将返回的Base64编码数据显示在MessageBox中(使用messagebox作为测试)的函数,而不是弹出任何错误。

PS - Besides the main problem I am having with this code, I also feel like the other functions I had converted aren't perfect or just might not be the same. PS-除了此代码存在的主要问题之外,我还觉得我转换后的其他函数并不完美,或者可能不一样。 So, if my main problem can be solved, if someone can also double check the other functions of my code make sure they are accurate that would be amazing and thanks in advance. 因此,如果我的主要问题可以解决,那么如果有人也可以再次检查我代码的其他功能,请确保它们准确无误,这是令人惊叹的,并在此先感谢。

Looking at this overall, it appears the AS3 code is attempting to convert the MD5.hash result into a Base64 encoded string in the worst way possible (I believe it can be done in one line.) 从总体MD5.hash ,似乎AS3代码正在尝试以最坏的方式将MD5.hash结果转换为Base64编码的字符串(我相信可以在一行中完成)。

So, instead of copying all the code to translate the hash to a hex string only to poorly translate it back to a binary array, just use the C# result which is already a binary array directly: 因此,与其复制所有代码以将哈希表转换为十六进制字符串,而仅是将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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM