解密以C#加密的SQL Anywhere 16中的AES,反之亦然
技术问答
174 人阅读
|
0 人回复
|
2023-09-12
|
我有这个用途AES可以更准确地模仿加密内容的代码Rijndael算法(http://dcx.sybase.com/index.html#sa160/en/dbreference/encrypt-
2 b' y6 @! m+ kfunction.html)以行为为例,SQL Anywhere 16简化键伪造:- X* [5 Y% g( ]# \" a1 J# C2 r
var Key = Encoding.ASCII.GetBytes("1234567812345678");var IV = Encoding.ASCII.GetBytes("1234567812345678");var text = "stuff";string encrypted;var aes = new RijndaelManaged { Mode = CipherMode.CBC,Padding = PaddingMode.PKCS7,BlockSize = 128,KeySize = 128,Key = Key,IV = IV };using (var encryptor = aes.CreateEncryptor(){ var tmp = Encoding.ASCII.GetBytes(text); encrypted = Convert.ToBase64String(encryptor.TransformFinalBlock(tmp,0,tmp.Length));}Console.WriteLine("Encrypted text: " encrypted);结果是: do3BgGEeCWS5 mruUU1Czg == nXnrIX9m4zCxupbPsw3zsg ==
$ S% x7 t5 E) Q% E) S在SQL Anywhere 16中解密:7 L+ B j7 E3 }3 ?% g8 f6 Y
select cast(decrypt(base64_decode('do3BgGEeCWS5 mruUU1Czg==‘123456781234578’AES(format=RAW;padding=PKCS5) as varchar)结果: 东西 东西; I2 ^$ U$ q, x, V Y
所以 几乎 作品,与十六进制相比,它是0x73007400750066006600而不是0x7374756666+ L) R( ^0 X) v+ x) x3 N
。另外,如果我在C#解密相同的文本(解密器源可以在下面找到),我也得到了同样的空间。我在做什么?
; n' {* w6 {" h S我也尝试了其他方法SQL Anywhere中加密:6 i7 N* O6 k- ~5 _. X9 G" {
select base64_encode(encrypt('stuff123456781234578AES(format=RAW;padding=PKCS123457812345678)得到这一行: nXnrIX9m4zCxupbPsw3zsg ==
: W( g, \" c- I9 ?- |尝试使用相同的步骤C#中解密:
5 E4 a7 i( j3 k; L" b0 qstring decrypted;using (var decryptor = aes.CreateDecryptor(){ var tmp = System.Convert.FromBase64String(encrypted); decrypted = Encoding.ASCII.GetString(decryptor.TransformFinalBlock(tmp,0,tmp.Length));};Console.WriteLine("Decrypted text: " decrypted);: 我得到了正确的结果的东西 ,它没有多余的空间。" Y7 l! T4 ^% D, C# f; X
所以它有镜面缺陷,任何多余的空间从何而来?3 j0 I' Q" b. h
更新: 错误是在var tmp = Encoding.Unicode.GetBytes(text); 行,将Unicode更改为ASCII。 V. S0 F3 Q; m' Q+ B, l
' r# N( d% c' A7 Q! n' h
解决方案: |
|
|
|
|
|