我在理解這里發(fā)生了什么時遇到了一些麻煩。我是加密領(lǐng)域的新手,并試圖在通過 HTTP(以 JSON 字符串發(fā)送)傳輸用戶名和密碼之前對其進行加密,并在另一端對其進行解密。我有一個類作為 AES 的簡單實現(xiàn)(使用硬編碼密鑰/iv):public class SimpleAES{ private byte[] key = { 32, 128, 16, 11, 28, 36, 45, 15, 214, 184, 17, 244, 27, 142, 252, 119, 111, 84, 125, 244, 123, 93, 126, 39, 44, 76, 87, 118, 231, 136, 43, 109 }; private byte[] vector = { 246, 164, 231, 211, 32, 8, 64, 128, 211, 221, 132, 242, 122, 123, 129, 254 }; private ICryptoTransform encryptor, decryptor; private UTF8Encoding encoder; public SimpleAES() { //return; RijndaelManaged rm = new RijndaelManaged(); encryptor = rm.CreateEncryptor(key, vector); decryptor = rm.CreateDecryptor(key, vector); encoder = new UTF8Encoding(); } public string Encrypt(string unencrypted) { //return unencrypted; return Convert.ToBase64String(Encrypt(encoder.GetBytes(unencrypted))); } public string Decrypt(string encrypted) { //return encrypted; return encoder.GetString(Decrypt(Convert.FromBase64String(encrypted))); } public byte[] Encrypt(byte[] buffer) { return Transform(buffer, encryptor); } public byte[] Decrypt(byte[] buffer) { return Transform(buffer, decryptor); } protected byte[] Transform(byte[] buffer, ICryptoTransform transform) { MemoryStream stream = new MemoryStream(); using( CryptoStream cs = new CryptoStream(stream, transform, CryptoStreamMode.Write) ) { cs.Write(buffer, 0, buffer.Length); } return stream.ToArray(); }}這會產(chǎn)生正確的加密。SimpleAES在 ASP.NET Core Web API 上的每次加密之間創(chuàng)建一個新對象,什么都不做,并且值保持完全相同。有誰知道為什么這會在 Xamarin.iOS 上以這種方式表現(xiàn)?也許是垃圾收集問題?代碼中是否存在特定于 Xamarin.iOS 的問題?我只是不知所措,不想使用這個解決方案。
1 回答

尚方寶劍之說
TA貢獻1788條經(jīng)驗 獲得超4個贊
的實現(xiàn)ICryptoTransform
在您調(diào)用后不一定可重用TransformFinalBlock
(CryptoStream
在關(guān)閉時自動發(fā)生)。CanReuseTransform
在創(chuàng)建另一個CryptoStream
具有相同轉(zhuǎn)換的屬性之前,您應該檢查該屬性。
除了,正如剛剛提到的另一條評論,在某些版本的框架中存在一個錯誤,其中某些實現(xiàn)不會返回 的正確值CanReuseTransform
。為了安全起見,您可能希望每次都重新創(chuàng)建對象。
- 1 回答
- 0 關(guān)注
- 607 瀏覽
添加回答
舉報
0/150
提交
取消