簡體   English   中英

C#等效於Oracle函數的AES加密

[英]C# equivalent of Oracle function for AES encryption

我正在尋找與以下代碼等效的C#代碼

CREATE OR REPLACE
  FUNCTION aes_encrypt(
      plaintext IN VARCHAR2,
      cryptokey    IN VARCHAR2) -- key is expected to be 32 bytes
    RETURN VARCHAR2
  IS
    v_varchar2 VARCHAR2(4000) := NULL; -- stores the encrypted data that will be returned
  BEGIN
    IF (cryptokey IS NULL OR LENGTH(cryptokey) <> 32) THEN
      RAISE_APPLICATION_ERROR(-20001,'cryptokey must not be null and must be 32 bytes');
    END IF;
    IF (plaintext IS NOT NULL) THEN
      v_varchar2     := rawtohex (
        DBMS_CRYPTO.ENCRYPT (
          src => UTL_I18N.STRING_TO_RAW (plaintext),
          typ => DBMS_CRYPTO.ENCRYPT_AES256 + DBMS_CRYPTO.CHAIN_CBC + DBMS_CRYPTO.PAD_PKCS5,
          KEY => UTL_I18N.STRING_TO_RAW (cryptokey)
    ));
    END IF;
    RETURN v_varchar2;
  END aes_encrypt;

我顯然已經在System.Security命名空間中找到了東西(例如https://msdn.microsoft.com/en-us/library/system.security.cryptography.aes%28v=vs.110%29.aspx ),但是我不確定如何制作IV和鑰匙。

IV為您的加密過程的開始增加了隨機性,並且密鑰保護了加密的數據。

using (var aes = new AesManaged())
{
    var iv = aes.IV;   // Gets the initialization vector (IV) to use for the symmetric algorithm.
    var key = aes.Key; // Gets or sets the secret key used for the symmetric algorithm. Set the Key if you don't like the generated one.
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM