我正在尝试解密一些加密的客户端文本,并且很难实现这一点。
我在客户端使用Standford Javascript Crypto Library(sjcl)生成一个json字符串,我可以发送到服务器,就像这样...
sjcl.encrypt("my_key", "something, something");
并生成这个json字符串......
"{"iv":"tfyF7oMXjauOkRnwaffU3A==","v":1,"iter":10000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"bq5G63+hk0U=","ct":"KahF3hQ3FZZ2l7APxLB4nRvw03Xt/qD04AChoA=="}"
现在我收到了这个json字符串服务器端,并试图这样做......
dynamic data = JObject.Parse(json);
var key = Encoding.ASCII.GetBytes("my_key");
var iv = Convert.FromBase64String(data.iv.Value);
byte[] cypher = Convert.FromBase64String(data.ct.Value);
string plainText = null;
using (AesCryptoServiceProvider provider = new AesCryptoServiceProvider())
{
provider.Key = key;
provider.IV = iv;
ICryptoTransform decryptor = provider.CreateDecryptor(provider.Key, provider.IV);
using (MemoryStream ms = new MemoryStream(cypher))
{
using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
{
using (StreamReader sr = new StreamReader(cs))
{
plainText = sr.ReadToEnd();
}
}
}
}
return plainText;
上面的服务器端代码在plainText = sr.ReadToEnd;
崩溃并抛出“输入数据不是完整的块”。我知道AES要求cypherText的块大小为128,并且由于文本大小不同,我如何保证它的块大小为128并且不会搞砸预期值?这里需要设置填充吗?
另外,AESCryptoServiceProvider中“ccm”的相应模式是什么?做“iter”&为了正确解密,“盐”需要发挥作用吗?
提前致谢!
的Jaime