关于加密:密码盒3 AES 128加密PLSQL dbms_crypto解密

Lockbox 3 AES 128 Encrypt PLSQL dbms_crypto Decrypt

我在获取Delphi和Oracle之间返回的相同加密值时遇到麻烦,将不胜感激。
不幸的是,我不确定这可能是填充引起的差异。

密码箱3
东京德尔斐RAD Studio 10.2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//uses uTPLb_Constants,uTPLb_BaseNonVisualComponent, uTPLb_Codec, uTPLb_CryptographicLibrary, IdHashMessageDigest, idHash
var cipher64, CipherText : string;
  plainText: utf8string;
  keyText: utf8string;
  FLibrary: TCryptographicLibrary;
  FCodec: TCodec;
  bytes, cipher: TBytes;
  workHash : TIdHashMessageDigest5;
  Result : String;
BEGIN
 plainText := 'test-data';
 keyText := 'test_key';

 try
   workHash := TIdHashMessageDigest5.CREATE;
   Result   := workHash.HashStringAsHex(keyText);
 finally
   FreeAndNil(workHash);
 END;
  memoOutput.Lines.Add('plaintext = ' + plainText);
  memoOutput.Lines.Add('key hash = ' + Result);

  FLibrary := TCryptographicLibrary.CREATE(Self);
  try
    FCodec := TCodec.CREATE(Self);
    try
     FCodec.CryptoLibrary := FLibrary;
     FCodec.StreamCipherId := BlockCipher_ProgId;
     FCodec.BlockCipherId := Format(AES_ProgId, [128]);
     FCodec.ChainModeId := ECB_ProgId;
     FCodec.password := Result;
     FCodec.EncryptString( plainText, CipherText, Tencoding.UTF8 );
     FCodec.Burn;

   finally
     FCodec.Free;
   END;
 finally
   FLibrary.Free;
 END;

结果:

1
2
key hash = 8C32D1183251DF9828F929B935AE0419   MD5 Hash OF text"test_key"
ciphertext = FJRXv9zMbypUmYnzzEHLnA==        Base64 Result FROM Lockbox

Oracle XE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    DECLARE
      raw_key RAW(200);
      encryption_type NUMBER;
      encrypted_result VARCHAR2(4000);
      decrypted_result VARCHAR2(4000);
    BEGIN
      raw_key := DBMS_CRYPTO.Hash (UTL_I18N.STRING_TO_RAW ('test_key', 'AL32UTF8'), DBMS_CRYPTO.HASH_MD5);

      -- Initialize the encrypted result
      encryption_type:= DBMS_CRYPTO.ENCRYPT_AES128 + DBMS_CRYPTO.CHAIN_ECB + DBMS_CRYPTO.PAD_PKCS5;

      -- Then the data is being encrypted with AES:
      encrypted_result := DBMS_CRYPTO.ENCRYPT(UTL_I18N.STRING_TO_RAW('test-data', 'AL32UTF8'), encryption_type, raw_key);

      decrypted_result := DBMS_CRYPTO.DECRYPT(encrypted_result, encryption_type, raw_key);


      DBMS_OUTPUT.put_line(raw_key);

      DBMS_OUTPUT.put_line(encrypted_result);
      DBMS_OUTPUT.put_line(UTL_RAW.CAST_TO_VARCHAR2 (decrypted_result));
      DBMS_OUTPUT.put_line(UTL_RAW.cast_to_varchar2(UTL_ENCODE.BASE64_ENCODE(encrypted_result)));

    END;

结果:

1
2
3
 Key Hash : 8C32D1183251DF9828F929B935AE0419
 Encrypt : 8FCA326C25C8908446D28884394F2E22   Hex VALUE returned
 Base 64 : j8oybCXIkIRG0oiEOU8uIg==

是的,Lockbox正在使用Ciphertext窃取进行填充。您的Oracle代码正在使用PKCS5填充。