各位高手,你们好,我现在要对一个decimal类型的数值进行加密和解密,加密代码如下:
public static byte[] EncryptString(Decimal sInputString, int id)
{
byte[] data = Encoding.UTF8.GetBytes(sInputString.ToString());
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = GetKey(id);
DES.IV = GetKey(id);
ICryptoTransform desencrypt = DES.CreateEncryptor();
int itmp = data.Length;
byte[] result = desencrypt.TransformFinalBlock(data, 0, itmp);
return result;
}
解密代码如下:
public static Decimal DecryptString(byte[] dInput, int id)
{
Decimal dValue = 0.00m;
try
{
byte[] data = dInput;
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = GetKey(id);
DES.IV = GetKey(id);
ICryptoTransform desencrypt = DES.CreateDecryptor();
byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
dValue = Decimal.Parse(Encoding.UTF8.GetString(result));
}
catch
{ }
return dValue;
}
但是我要求中间加密出来的要以Decimal类型的方式存入数据库,解密的时候是要用加密过的Decimal数值为参数进行解密。我用了BitConverter.GetBytes()进行转换。这时候数值不超过8位还好。如果超过8位。用这个方法就会有问题。不知道各位有什么办法帮我解决一下吗?在byte[]与decimal类型进行转换。长度最好有16位以上。谢谢各位,我的MSN是:[email protected]欢迎大家加我,指点我一下。先谢过了。
public static byte[] EncryptString(Decimal sInputString, int id)
{
byte[] data = Encoding.UTF8.GetBytes(sInputString.ToString());
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = GetKey(id);
DES.IV = GetKey(id);
ICryptoTransform desencrypt = DES.CreateEncryptor();
int itmp = data.Length;
byte[] result = desencrypt.TransformFinalBlock(data, 0, itmp);
return result;
}
解密代码如下:
public static Decimal DecryptString(byte[] dInput, int id)
{
Decimal dValue = 0.00m;
try
{
byte[] data = dInput;
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = GetKey(id);
DES.IV = GetKey(id);
ICryptoTransform desencrypt = DES.CreateDecryptor();
byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
dValue = Decimal.Parse(Encoding.UTF8.GetString(result));
}
catch
{ }
return dValue;
}
但是我要求中间加密出来的要以Decimal类型的方式存入数据库,解密的时候是要用加密过的Decimal数值为参数进行解密。我用了BitConverter.GetBytes()进行转换。这时候数值不超过8位还好。如果超过8位。用这个方法就会有问题。不知道各位有什么办法帮我解决一下吗?在byte[]与decimal类型进行转换。长度最好有16位以上。谢谢各位,我的MSN是:[email protected]欢迎大家加我,指点我一下。先谢过了。
加密
public static byte[] Decrypt (byte[] bKey, byte[] bStr)
{
byte[] bIV = new byte[8] {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, desc.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write);
cStream.Write(bStr, 0, bStr.Length);
cStream.FlushFinalBlock();
return mStream.ToArray();
}解密
public static byte[] Encrypt (byte[] bKey, byte[] bStr)
{
byte[] bIV = new byte[8] {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, desc.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write);
cStream.Write(bStr, 0, bStr.Length);
cStream.FlushFinalBlock();
return mStream.ToArray();
}
反正强烈建议修改保存的数据类型。