关于Guid的规律

根据代码看规律

如果是代码,可以看到Guid是非常有规律的

byte[] bytes;
bytes = new byte[16] { 
    0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0 
};
Console.WriteLine(new Guid(bytes)); // 00000000-0000-0000-0000-000000000000
bytes = new byte[16] { 
    255, 255, 255, 255, 255, 255, 255, 255, 
    255, 255, 255, 255, 255, 255, 255, 255 
};
Console.WriteLine(new Guid(bytes)); // ffffffff-ffff-ffff-ffff-ffffffffffff

bytes = new byte[16] { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Console.WriteLine(new Guid(bytes)); // 04030201-0000-0000-0000-000000000000
bytes = new byte[16] { 0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Console.WriteLine(new Guid(bytes)); // 00000000-0201-0000-0000-000000000000
bytes = new byte[16] { 0, 0, 0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0 };
Console.WriteLine(new Guid(bytes)); // 00000000-0000-0201-0000-000000000000
bytes = new byte[16] { 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0 };
Console.WriteLine(new Guid(bytes)); // 00000000-0000-0000-0102-000000000000
bytes = new byte[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6 };
Console.WriteLine(new Guid(bytes)); // 00000000-0000-0000-0000-010203040506

Guid与Int互相转换

一、未混淆

static Guid Int2Guid(int value)
{
    byte[] bytes = new byte[16];
    BitConverter.GetBytes(value).CopyTo(bytes, 1);
    var result = new Guid(bytes);
    return result;
}

static int Guid2Int(Guid value)
{
    byte[] b = value.ToByteArray();
    int bint = BitConverter.ToInt32(b, 1);
    return bint;
}

for (int i = 1; i < 10; i++)
{
    var guid = Int2Guid(i);
    var id = Guid2Int(guid);
    Console.WriteLine(guid);
    Console.WriteLine(id);
}

控制台打印的结果如下,可以看到非常有规律

00000100-0000-0000-0000-000000000000
1
00000200-0000-0000-0000-000000000000
2
00000300-0000-0000-0000-000000000000
3
00000400-0000-0000-0000-000000000000
4
00000500-0000-0000-0000-000000000000
5
00000600-0000-0000-0000-000000000000
6
00000700-0000-0000-0000-000000000000
7
00000800-0000-0000-0000-000000000000
8
00000900-0000-0000-0000-000000000000
9

二、增加混淆

static Guid Int2Guid(int value)
{
    var r = new Random(value);
    byte[] bytes = new byte[16];
    r.NextBytes(bytes);
    BitConverter.GetBytes(value).CopyTo(bytes, 1);
    var result = new Guid(bytes);
    return result;
}

static int Guid2Int(Guid value)
{
    byte[] b = value.ToByteArray();
    int bint = BitConverter.ToInt32(b, 1);
    return bint;
}

for (int i = 1; i < 10; i++)
{
    var guid = Int2Guid(i);
    var id = Guid2Int(guid);
    Console.WriteLine(guid);
    Console.WriteLine(id);
}

控制台打印的结果如下,虽然混淆,但是还能看到其规律

00000146-9700-a3e4-95cf-ff46699c73c4
1
00000271-b900-6fe3-7c38-98b7dd200bc1
2
0000039d-db00-3ae3-63a0-322750a4a3bd
3
000004c8-fd00-06e2-4a08-cc98c4273aba
4
000005f4-1e00-d1e2-3171-650938abd2b7
5
0000061f-4000-9de1-18d9-ff7aab2e6ab3
6
0000074b-6200-68e1-ff41-99ea1fb201b0
7
00000876-8400-34e0-e6aa-335b933599ad
8
000009a2-a600-ffdf-ce12-cccc06b931a9
9

二、混淆复杂化

static Guid Int2Guid(int value)
{
    var r = new Random(value);
    byte[] bytes = new byte[16];
    r.NextBytes(bytes);
    BitConverter.GetBytes(value).CopyTo(bytes, 1 + (bytes[0] % 6));
    var result = new Guid(bytes);
    return result;
}

static int Guid2Int(Guid value)
{
    byte[] bytes = value.ToByteArray();
    int bint = BitConverter.ToInt32(bytes, 1 + (bytes[0] % 6));
    return bint;
}

for (int i = 1; i < 10; i++)
{
    var guid = Int2Guid(i);
    var id = Guid2Int(guid);
    Console.WriteLine(guid);
    Console.WriteLine(id);
}

控制台打印的结果如下,很难看到规律

8286d046-0140-0000-00cf-ff46699c73c4
1
95c69371-b924-0002-0000-98b7dd200bc1
2
0003569d-0000-3ae3-63a0-322750a4a3bd
3
044519c8-0000-0600-4a08-cc98c4273aba
4
cf85ddf4-05ce-0000-0071-650938abd2b7
5
0006a01f-0000-9de1-18d9-ff7aab2e6ab3
6
f604634b-0007-0000-ff41-99ea1fb201b0
7
09442776-0878-0000-00aa-335b933599ad
8
000009a2-a600-ffdf-ce12-cccc06b931a9
9
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值