| 这有两个在线的AES加密网站,加密结果是一样的。 https://www.javainuse.com/aesgenerator 
 https://www.codeusingjava.com/tools/aes 
 ?Delphi XE10.x调用bouncy castle库,也可以得到相同的结果,Base64编码的字符串 BCklCa2IpgHiLT6k4DOELQ==unit AesUtil;
interface
uses bcprovext,
     System.SysUtils, System.Types, math,
     System.Classes, system.NetEncoding,
     Androidapi.Helpers,
     java.nio.BitConverter,
     Androidapi.JNIBridge,
     Androidapi.JNI.JavaTypes;
function AESEncryptCBC(const encrypted: Boolean; const AInput, AKey, AIV : String):string;
implementation
function AESEncryptCBC(const encrypted: Boolean; const AInput, AKey, AIV : String):string;
var
    blocksize    : integer;
    keyBytes, ivBytes,
    inputBytes,
    LJTemp,
    outputBytes,
    comparisonBytes : TJavaArray<Byte>;
    iv, vTemp     : TArray<Byte>;
    val,len       : integer;
    keyParam      : JKeyParameter;
    keyParamWithIV: JParametersWithIV;
    encryptedInput: JString;
    vBytes        : TBytes;
    engine : JAesEngine ;
    blockCipher: JCbcBlockCipher ;
    cipher: JPaddedBufferedBlockCipher ;
    function ByteToHex(InByte:byte):shortstring;
    const
       Digits:array[0..15] of char='0123456789ABCDEF';
    begin
      result:=digits[InByte shr 4]+digits[InByte and $0F];
    end;
begin
    try
        iv := TEncoding.ASCII.GetBytes(AIV);
        len := Length(iv);
        ivBytes := TJavaArray<Byte>.Create(len);
        //Move(iv, ivBytes, len);
        for val := 0 to len -1 do  ivBytes[val] := iv[val];
        engine := TJAesEngine.Create;
        blockCipher := TJCbcBlockCipher.JavaClass.init(TJBlockCipher.Wrap(engine)); //CBC
        cipher := TJPaddedBufferedBlockCipher.JavaClass.init(TJBlockCipher.Wrap(blockCipher));//, TJBlockCipherPadding.Wrap(TJPKCS7Padding.Create));
        blocksize := cipher.GetBlockSize();
        //TBitConverter.From<Int32>(iv, vTemp);
        //vTemp := TArray<Byte>.Create( byte( i  shr 24 ), byte( i  shr 16 ), byte( i  shr  8 ), byte(i));
        keyBytes      := TJavaArray<Byte>.Create(blocksize);
        vTemp := TEncoding.ASCII.GetBytes(Akey);
        len := Length(vTemp);
        //Move(vTemp, keyBytes, len);
        for val := 0 to len -1 do keyBytes[val] := vTemp[val];
        keyParam := TJKeyParameter.JavaClass.init(keyBytes);// Convert.FromBase64String(keyString));
        keyParamWithIV := TJParametersWithIV.JavaClass.init(TJCipherParameters.Wrap(keyParam), ivBytes, 0, 16);//16*8 = 128 bit
        // Encrypt
        if encrypted then
        begin
            //要加密的字符串,包括双字节
            vTemp := TEncoding.UTF8.GetBytes(Ainput);
            len := Length(vTemp);
            inputBytes := TJavaArray<Byte>.Create(len);
            for val := 0 to len -1 do
              inputBytes[val] := vTemp[val];
            cipher.Init(true, TJCipherParameters.Wrap(keyParamWithIV));
            len := cipher.GetOutputSize(inputBytes.Length);
            outputBytes := TJavaArray<Byte>.create(len);
            len := cipher.ProcessBytes(inputBytes, 0, inputBytes.Length, outputBytes,  0);
            cipher.DoFinal(outputBytes, len); //Do the final block
            SetLength(vBytes, outputBytes.Length);
            for val := 0 to Length(vBytes) -1 do
              vBytes[val] := outputBytes[val];
            encryptedInput := TJBase64.JavaClass.toBase64String(outputBytes);
            //TNetEncoding.Base64.Encode(vBytes);
            Result := JStringToString(encryptedInput);
        end
        else //Decrypt
        begin
           encryptedInput := StringToJString(Ainput);
           outputBytes := TJBase64.JavaClass.decode(encryptedInput);
           cipher.Init(false, TJCipherParameters.Wrap(keyParamWithIV));
           len := cipher.GetOutputSize(outputBytes.Length);
           comparisonBytes := TJavaArray<Byte>.create(len);
           len := cipher.ProcessBytes(outputBytes, 0, outputBytes.Length, comparisonBytes, 0);
           cipher.DoFinal(comparisonBytes, len); //Do the final block
           len := comparisonBytes.Length;
           SetLength(vBytes, len);
            for val := 0 to len -1 do
              vBytes[val] := comparisonBytes[val];
           Result := TEncoding.UTF8.GetString(vBytes);
        end;
    except on E: Exception do
       Result := e.Message;
    end;
end;
end.
 
 |