【问题标题】:C# equivalent of Oracle function for AES encryption用于 AES 加密的 C# 等效的 Oracle 函数
【发布时间】:2015-03-11 21:00:47
【问题描述】:

我正在寻找与以下等效的 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 和密钥。

【问题讨论】:

    标签: c# oracle encryption aes


    【解决方案1】:

    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.
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-23
      • 2015-12-29
      • 2015-12-07
      • 2014-03-24
      • 2021-08-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多