【发布时间】:2021-02-08 09:56:30
【问题描述】:
我正面临这个问题: 我正在使用带有 nhibernate 的 .net 核心应用程序。我可以在我的应用程序中使用 aes 加密一些列,然后在 db 中有一个函数来解密数据以查询报告吗?
这是我的加密 c# 代码
string EncryptData(string textData, string Encryptionkey)
{
RijndaelManaged objrij = new RijndaelManaged();
//set the mode for operation of the algorithm
objrij.Mode = CipherMode.CBC;
//set the padding mode used in the algorithm.
objrij.Padding = PaddingMode.PKCS7;
//set the size, in bits, for the secret key.
objrij.KeySize = 0x80;
//set the block size in bits for the cryptographic operation.
objrij.BlockSize = 0x80;
//set the symmetric key that is used for encryption & decryption.
byte[] passBytes = Encoding.UTF8.GetBytes(Encryptionkey);
//set the initialization vector (IV) for the symmetric algorithm
byte[] EncryptionkeyBytes = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
int len = passBytes.Length;
if (len > EncryptionkeyBytes.Length)
{
len = EncryptionkeyBytes.Length;
}
Array.Copy(passBytes, EncryptionkeyBytes, len);
objrij.Key = EncryptionkeyBytes;
objrij.IV = EncryptionkeyBytes;
//Creates a symmetric AES object with the current key and initialization vector IV.
ICryptoTransform objtransform = objrij.CreateEncryptor();
byte[] textDataByte = Encoding.UTF8.GetBytes(textData);
//Final transform the test string.
return Convert.ToBase64String(objtransform.TransformFinalBlock(textDataByte, 0, textDataByte.Length));
}
这是我在 oracle 中的解密函数
key VARCHAR2(64) := 'THE KEY';
encryption_type PLS_INTEGER := -- total encryption type
DBMS_CRYPTO.ENCRYPT_AES256
+ DBMS_CRYPTO.CHAIN_CBC
+ DBMS_CRYPTO.PAD_PKCS5;
DBMS_CRYPTO.DECRYPT(
src => data2decrypt,
typ => encryption_type,
key => key
)
【问题讨论】:
-
您的解密尝试未使用与原始加密相同的填充机制。我不认为
DBMS_CRYPTO支持 PKCS7 填充。你能改变你的 C# 程序来使用 PKCS5 吗? -
@MatthewMcPeak PKCS5 和 PKCS7 填充对于所有意图和目的都是相同的:crypto.stackexchange.com/a/9044/13022
-
您需要复制相同的方法。我没有看到您使用相同的方式将密码拉伸到 Oracle 中 16 字节的密钥大小。另外,我看不到您在哪里使用 IV,并且可能缺少各种编码步骤,具体取决于 Oracle 是否自动执行它们。
-
@ArtjomB。真的吗?我认为 PKCS5 本质上是 PKCS7 的一个子集。也就是说,PKCS5 是 PKCS7,但使用 8 字节块。但是 OP 似乎在他的加密代码中使用了 16 字节块(十六进制 0x80 位)。也许
objrij.BlockSize = 0x80;需要是objrij.BlockSize = 0x40;? -
@MatthewMcPeak 是的,但 Rijndael 仅针对 128、192 和 256 位的块大小定义。 64 位的块大小是不可能的,所以 PKCS5 也是不可能的,但是当块密码只有 64 位宽(例如 DES)时,它仍然适用于向后兼容。
标签: c# .net oracle encryption aes