我也需要将数据库的一个主键(uint类型)编码为GUID。关于是否使用uint作为主键或使用GUID存在很多讨论,我不会在这里深入介绍,但我使用了uint作为主键,并在列中使用了GUID,我想将主键编码到GUID中,并从GUID中获取它。
下面的解决方案将24位主键(uint类型)编码为GUID,并将其解码。它对uint使用了DES加密。我知道问题想要一个32位int,这适用于24位uint,但可以通过连接两个GUID(一个具有最高的24位,另一个具有最低的24位)来实现32位。该算法已经进行了全面测试,并且可以用于int,但未测试负数。
GUID格式
GUID格式为32位数字,可以用连字符分隔,并包括大括号:00000000-0000-0000-0000-000000000000(D),其中8-4-4-12个字符,即4-2-2-6个字节,因为一个字节被编码成2个字符。还存在其他格式的GUID。
将3个字节的uint加密为8个字节的代码,并写入GUID中,从第5个字节(忽略括号和'-'分隔符的第10个字符)开始。让“z”表示编码的int,0表示任何其他十六进制字符。得到的编码GUID为:
00000000-zzzz-zzzz-zzzz-zzzz00000000
需要注意的是,此时生成的GUID可能不是全局唯一的。然而,由于GUID编码了您的主键,它将对于您的主键是唯一的,并且很难在没有加密密钥的情况下从GUID中提取uint值......尽管DES加密非常安全。
public void Test()
{
Guid NewG = Guid.NewGuid();
Guid EnryptedGuid = Utility.EncodeInt24InGUID(NewG, Num);
uint RestoredUint = Utility.DecodeInt24FromGUID(EnryptedGuid);
}
加密
public static Guid EncodeInt24InGUID(Guid guid, uint x)
{
if (x >= Math.Pow(2, 24))
throw new ArgumentOutOfRangeException("Unsigned integer is greater than 24bit");
string strGuid = guid.ToString();
strGuid = Utility.RemoveChars(strGuid, "-{}");
byte[] bytes = BitConverter.GetBytes(x);
var encryptedarray = Cypher.EncryptDES(bytes, Cypher.Key);
string EncryptedGuid = WriteBytesToString(strGuid, encryptedarray, 9);
Guid outg;
Guid.TryParse(EncryptedGuid, out outg);
return outg;
}
RemoveChars是从此处复制/改编而来:使用LINQ从字符串中删除字符
WriteBytesToString如下:
public static string WriteBytesToString(string Input, byte[] bytes, int start)
{
StringBuilder g = new StringBuilder(Input);
string temp;
int ByteNum = 0;
int CharPos = start;
int NumBytes = (int)bytes.LongLength;
for (int i = 0; i < NumBytes; i++)
{
temp = string.Format("{0:x2}", bytes[ByteNum++]);
g[CharPos++] = (temp.ToCharArray())[0];
g[CharPos++] = (temp.ToCharArray())[1];
}
return g.ToString();
}
解密
public static uint DecodeInt24FromGUID(Guid guid)
{
string strGuid = guid.ToString();
strGuid = Utility.RemoveChars(strGuid, "-{}");
byte[] EncryptedBytes = GetBytesFromString(strGuid, 9,8);
var decrypted = Cypher.DecryptDES(EncryptedBytes, Cypher.Key);
uint DecryptedUint = BitConverter.ToUInt32(decrypted, 0);
return DecryptedUint;
}
GetBytesFromString。请注意,字节从第9个索引开始,并且在调用函数中硬编码为8个字节。有关GUID格式的说明,请参见注释。
public static byte[] GetBytesFromString(string Input, int start, int NumBytes)
{
StringBuilder g = new StringBuilder(Input);
byte[] Bytes = new byte[NumBytes];
string temp;
int CharPos = start;
for (int i = 0; i < NumBytes; i++)
{
temp = g[CharPos++].ToString();
temp += g[CharPos++].ToString();
Bytes[i] = byte.Parse(temp, System.Globalization.NumberStyles.HexNumber);
}
return Bytes;
}
这个网络课程是从这里
https://www.codeproject.com/Articles/5719/Simple-encrypting-and-decrypting-data-in-C复制过来的,并经过改编以适用于DES加密。以下是完整的内容:
using System.IO;
using System.Security.Cryptography;
namespace UtilityLib
{
public static class Cypher
{
public static string Key = "Asd9847Fg85ihkn52s";
public static byte[] EncryptDES(byte[] clearData, byte[] Key, byte[] IV)
{
MemoryStream ms = new MemoryStream();
TripleDES alg = TripleDES.Create();
alg.Key = Key;
alg.IV = IV;
CryptoStream cs = new CryptoStream(ms,alg.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(clearData, 0, clearData.Length);
cs.Close();
byte[] encryptedData = ms.ToArray();
return encryptedData;
}
public static byte[] EncryptDES(byte[] clearData, string Password)
{
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,
0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
return EncryptDES(clearData, pdb.GetBytes(24), pdb.GetBytes(8));
}
public static byte[] DecryptDES(byte[] cipherData, byte[] Key, byte[] IV)
{
MemoryStream ms = new MemoryStream();
TripleDES alg = TripleDES.Create();
alg.Key = Key;
alg.IV = IV;
CryptoStream cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(cipherData, 0, cipherData.Length);
cs.Close();
byte[] decryptedData = ms.ToArray();
return decryptedData;
}
public static byte[] DecryptDES(byte[] cipherData, string Password)
{
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,
0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
return DecryptDES(cipherData, pdb.GetBytes(24), pdb.GetBytes(8));
}
}
}