用于AES加密的C#相当于Oracle功能

时间:2015-03-11 21:00:47

标签: c# oracle encryption aes

我正在寻找与以下相同的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和Key的内容。

1 个答案:

答案 0 :(得分:0)

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