我想编写一个WCF代码,其中我具有加密和解密C#中任何值的功能 并在我的JavaScript代码中使用它来加密我的查询字符串参数
代码如下:
private string Encrypt(string clearText)
{
string EncryptionKey = "MAKV2SPBNI99212";
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
}
clearText = Convert.ToBase64String(ms.ToArray());
}
}
return clearText;
}
我正在这样的JavaScript代码中调用此Web服务:
function Encrypt(para)
{
var resultEncrypt = "";
$.ajax({
type: "GET",
url:"http://servername/site/_layouts/15/SPED/ED.svc/Call("+para+")",
data: "{'clearText':'clearText'}",
async : false,
contentType: "application/json",
datatype: "json",
success: function(data) {
resultEncrypt = data.SampleServiceCallResult ;
}
});
return resultEncrypt;
}
如果我尝试对纯文本或数字进行加密。它可以正常工作。但是,如果我的字符串像“ 07/89 / HP8 / 9090”。它不起作用。
请帮助