我正在尝试从oracle加密数据,然后使用C#
对其进行解密到目前为止,我设法编写了一个代码来加密C#和Oracle中的数据,并得到了匹配的结果
我正在尝试解密C#中的数据,但我收到错误: “输入数据不是一个完整的块”
Oracle加密:
SELECT DBMS_CRYPTO.encrypt (RAWTOHEX ('Hello World!'),
6 + 256 + 12288,
RAWTOHEX ('4566456678997899'),
RAWTOHEX ('1234123456785678')) /*AES128 = 6, CHAINCBC = 256, PAD ZERO = 12288*/
FROM DUAL;
/
SELECT UTL_RAW.cast_to_varchar2(UTL_ENCODE.base64_encode('B6E88F68185584456FBF8FA799B10CA1'))
FROM DUAL;
在Oracle中执行第二个查询后的结果是:tuiPaBhVhEVvv4 + nmbEMoQ ==
加密/解密的C#类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Security.Cryptography;
using System.Diagnostics;
namespace ConsoleApplication3
{
class AesTest {
private const string AesIV = @"1234123456785678"; //@"d8zOcR9K9xqpl8Cd";//@"!QAZ2WSX#EDC4RFV";
private const string AesKey = @"4566456678997899"; //@"NDsVwQwRbwbuYDcX2PRGwNewMediaCod"; //@"5TGB&YHN7UJM(IK<";
static void Main(string[] args)
{
string src = "Hello World!";
string x = AesTest.EncryptUsingCBC(src);
System.Console.WriteLine("Text to Encrypt: " + src);
System.Console.WriteLine("CSharp: " + x);
string w = AesTest.DecryptUsingCBC(x);
System.Console.WriteLine("Decrypt CSharp: " + w);
System.Console.ReadLine();
}
public static byte[] EncryptToBytesUsingCBC(string toEncrypt) {
byte[] src = Encoding.UTF8.GetBytes(toEncrypt);
byte[] dest = new byte[src.Length];
using (var aes = new AesCryptoServiceProvider()) {
aes.BlockSize = 128;
aes.KeySize = 128;
aes.IV = Encoding.UTF8.GetBytes(AesIV);
aes.Key = Encoding.UTF8.GetBytes(AesKey);
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.Zeros;
// encryption
using (ICryptoTransform encrypt = aes.CreateEncryptor(aes.Key, aes.IV)) {
return encrypt.TransformFinalBlock(src, 0, src.Length);
}
}
}
public static string EncryptUsingCBC(string toEncrypt) {
return Convert.ToBase64String(EncryptToBytesUsingCBC(toEncrypt));
}
public static byte[] DecryptToBytesUsingCBC(string toDecrypt)
{
byte[] src = Encoding.UTF8.GetBytes(toDecrypt);
byte[] dest = new byte[src.Length];
using (var aes = new AesCryptoServiceProvider())
{
aes.BlockSize = 128;
aes.KeySize = 128;
aes.IV = Encoding.UTF8.GetBytes(AesIV);
aes.Key = Encoding.UTF8.GetBytes(AesKey);
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.Zeros;
// decryption
using (ICryptoTransform decrypt = aes.CreateDecryptor(aes.Key, aes.IV))
{
byte[] decryptedText = decrypt.TransformFinalBlock(src, 0, src.Length);
return decryptedText;
}
}
}
public static string DecryptUsingCBC(string toDecrypt)
{
return Convert.ToBase64String(DecryptToBytesUsingCBC(toDecrypt));
}
}
}
运行加密程序后的结果是:tuiPaBhVhEVvv4 + nmbEMoQ == 但是解密会抛出异常
提前致谢
答案 0 :(得分:1)
感谢Damien,我已经按照你的提示设法解决了问题,最终得到了代码工作+得到了相同的字符串我加密
这是执行所需编辑后的新代码
public static string DecryptToBytesUsingCBC(byte[] toDecrypt)
{
byte[] src = toDecrypt;
byte[] dest = new byte[src.Length];
using (var aes = new AesCryptoServiceProvider())
{
aes.BlockSize = 128;
aes.KeySize = 128;
aes.IV = Encoding.UTF8.GetBytes(AesIV);
aes.Key = Encoding.UTF8.GetBytes(AesKey);
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.Zeros;
// decryption
using (ICryptoTransform decrypt = aes.CreateDecryptor(aes.Key, aes.IV))
{
byte[] decryptedText = decrypt.TransformFinalBlock(src, 0, src.Length);
return Encoding.UTF8.GetString(decryptedText);
}
}
}
public static string DecryptUsingCBC(string toDecrypt)
{
return DecryptToBytesUsingCBC(Convert.FromBase64String(toDecrypt));
}
解密后的结果是:Hello World!