代码之家  ›  专栏  ›  技术社区  ›  WynDiesel

将int转换为加扰guid并返回

  •  0
  • WynDiesel  · 技术社区  · 8 年前

    我正在寻找一种将int转换为guid的方法,但不希望下一个int是连续的。例如,我有这个:

    static void Main(string[] args)
    {
       var a = Int2Guid(1);
       var b = Int2Guid(2);
    }
    
    public static Guid Int2Guid(int value)
    {
        byte[] bytes = new byte[16];
        BitConverter.GetBytes(value).CopyTo(bytes, 0);
        return new Guid(bytes);
    }
    
    public static int Guid2Int(Guid value)
    {
        byte[] b = value.ToByteArray();
        int bint = BitConverter.ToInt32(b, 0);
        return bint;
    }
    

    这是可行的,但我不希望这些数字是,或者看起来是连续的。从上面的例子,我得到 a = {00000001-0000-0000-0000-000000000000} ,但是 b = {00000002-0000-0000-0000-000000000000} 是的。我想要一些像 {48204b95-9cbb-4295-a6b1-cf05ebda9d0d} 是的。

    有什么建议吗?

    3 回复  |  直到 8 年前
        1
  •  1
  •   Barr J    8 年前

    有一个很好的解决方案使用aes ecb模式来加密guid,可以工作16个字节(您在示例中提供了:

     public class EncryptGUI
        {
            private Aes aes;
    
            public EncryptGUI (byte[] key)
            {
                aes = Aes.Create ();
                aes.Mode = CipherMode.ECB;
                aes.Padding = PaddingMode.None;
                aes.Key = key;
            }
    
            public String encryptUID (byte[] guid)
            {
                ICryptoTransform aesDecryptor = aes.CreateDecryptor ();
                byte[] result = aesDecryptor.TransformFinalBlock (guid, 0, guid.Length);
                return ToHex (result);
            }
    
            public static string ToHex (byte[] data)
            {
                StringBuilder hex = new StringBuilder (data.Length * 2);
                foreach (byte b in data)
                    hex.AppendFormat ("{0:x2}", b);
                return hex.ToString ();
            }
    
            public static void Main (string[] args)
            {
                byte[] key = new byte[16];
                EncryptGUI main = new EncryptGUI (key);
    
                byte[] guid = new byte[16];
                Console.Out.WriteLine (main.encryptUID (guid));
            }
        }
    

    注意,ecb不使用iv,这意味着您可以区分不同的guid(因为每个guid将映射到一个密文)。但是密文应该与使用的分组密码和密钥的安全性完全相同。

        2
  •  2
  •   lindexi    8 年前

    对于guid需要4个int或16个字节,您应该输入4个int或16个字节。

    我做的 Int2Guid 输入4个整数。

        public static Guid Int2Guid(int value, int value1, int value2, int value3)
        {
            byte[] bytes = new byte[16];
            BitConverter.GetBytes(value).CopyTo(bytes, 0);
            BitConverter.GetBytes(value1).CopyTo(bytes, 4);
            BitConverter.GetBytes(value2).CopyTo(bytes, 8);
            BitConverter.GetBytes(value3).CopyTo(bytes, 12);
            return new Guid(bytes);
        }
    

    我做了 Guid2Int 输出int数组。

        public static int[] Guid2Int(Guid value)
        {
            byte[] b = value.ToByteArray();
            int bint = BitConverter.ToInt32(b, 0);
            var bint1 = BitConverter.ToInt32(b, 4);
            var bint2 = BitConverter.ToInt32(b, 8);
            var bint3 = BitConverter.ToInt32(b, 12);
            return new[] {bint, bint1, bint2, bint3};
        }
    

    当我给 Int2Guid公司 并将其设置为 Guid2int公司 然后它可以回到原点。

        static void Main(string[] args)
        {
            var guid = Guid.NewGuid();
            var foo = Guid2Int(guid);
    
            var a = Int2Guid(foo[0], foo[1], foo[2], foo[3]);
    
            Console.WriteLine(a == guid); //true
        }
    

    我不知道你是否需要它。

        3
  •  -1
  •   pixelbadger    8 年前

    通过使用md5对整数进行哈希运算,并将哈希用作guid的源,可以实现此目的:

    int max = 10;
    for (int i = 1; i < max + 1; i++)
    {
        MD5 md5 = MD5.Create();
        Byte[] intBytes = BitConverter.GetBytes(i);
        Byte[] hash = md5.ComputeHash(intBytes);
        Console.WriteLine(new Guid(hash));
    }
    

    注意,这不是一种加密安全的方法。