C#好像可以直接调用sql 的存储过程
sql 存储过程也可以直接调用类库函数
相同的明文相同的密钥相同的加密算法怎么得到的密文就不同了?
求mssql des算法 算出来 不一样
把代码贴出来吧.
mssql encyption des
CREATE SYMMETRIC KEY key1 WITH ALGORITHM=DES
ENCRYPTION BY PASSWORD='CMDKDCJC'
GO
OPEN SYMMETRIC KEY key1 DECRYPTION BY PASSWORD='CMDKDCJC'
DECLARE @str NVARCHAR(100),@Encrypted VARBINARY(MAX)
SET @str=N'aaaa'
SELECT @Encrypted=ENCRYPTBYKEY(KEY_GUID('key1'),@str)
SELEcT '加密'=@Encrypted ,'解密'=CAST(DECRYPTBYKEY(@Encrypted) AS NVARCHAR(100))
CLOSE SYMMETRIC KEY key1
GO
DROP SYMMETRIC KEY key1
GO
c# encyption des
//默认密钥向量
static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
/// <summary>
/// 自定义公共
/// </summary>
public static byte[] PuKeys = { 0x15, 0x1, 0x0, 0x78, 0x91, 0xAD, 0x1D, 0xCF };
/// <summary>
/// 默认密钥
/// </summary>
static string okey = "CMDKDCJC";
/// <summary>
/// DES加密字符串
/// </summary>
/// <param name="encryptString">待加密的字符串</param>
/// <param name="encryptKey">加密密钥,要求为8位</param>
public static string EncryptDES(string encryptString)
{
return EncryptDES(encryptString, okey);
}
/// <summary>
/// DES加密字符串
/// </summary>
/// <param name="encryptString">待加密的字符串</param>
/// <param name="encryptKey">加密密钥,要求为8位</param>
/// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
public static string EncryptDES(string encryptString, string encryptKey)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
byte[] rgbIV = Keys;
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey,
rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray());
}
catch
{
return encryptString;
}
}
/// <summary>
/// DES解密字符串
/// </summary>
/// <param name="decryptString">待解密的字符串</param>
/// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
/// <returns>解密成功返回解密后的字符串,失败返源串</returns>
public static string DecryptDES(string decryptString)
{
return DecryptDES(decryptString, okey);
}
/// <summary>
/// DES解密字符串
/// </summary>
/// <param name="decryptString">待解密的字符串</param>
/// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
/// <returns>解密成功返回解密后的字符串,失败返源串</returns>
public static string DecryptDES(string decryptString, string decryptKey)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
byte[] rgbIV = Keys;
byte[] inputByteArray = Convert.FromBase64String(decryptString);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey,
rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
}
catch
{
return decryptString;
}
}
@objectboy: 你是想在 sql 中加密,然后面C#中再解密么.?
或是用C#加密,然后用sql解密.?
如果是这样的话,应该是不行的.因为sql加密的时候创建了密钥,也就是文中的变量 key1
而你在C#中是得不到这个密钥的,因此两个加密结果应该是不同的,
@发粪图墙: 没有啊 我的意思是假如现在一个字符串是 "objectboy" 用sql的des算法加密出来的密文和用c#加密出来的密文不一样
我上面的代码是我的sql des算法 和c#des算法 是我用来测试的
@发粪图墙: 怎么才能让sql des 和c# des 算法加密同一个字符串得到相同的密文?????????? 求解
@objectboy: 应该不可能相同吧.?因为不是同一个密钥啊.
sql 使用的是他自己生成的一个密钥吧.?