0

次の Oracle PL/SQL コードを C# に変換する方法を教えてもらえますか?

declare 
   l_encrypted raw (100);
   p_key raw (100);
   p_plain raw(100);
   l_mode number; 
begin
  l_mode := dbms_crypto.ENCRYPT_DES + dbms_crypto.CHAIN_CBC + dbms_crypto.PAD_PKCS5;
  p_key := 'A217B5BEF1477D1A';
  p_plain := '07253075';
  l_encrypted := dbms_crypto.encrypt(UTL_I18N.STRING_TO_RAW(p_plain, 'AL32UTF8'), l_mode, p_key);
  dbms_output.put_line(l_encrypted);
     --outputs this value: E4624E16DB69451A14BE265CDCC5B0AB
end;

私のC#コードは次のとおりです。

        byte[] value = Encoding.UTF8.GetBytes("07253075");
        byte[] key = Encoding.UTF8.GetBytes("A217B5BEF1477D1A");
        DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider
        {
            Mode = CipherMode.CBC,
            Padding = PaddingMode.PKCS7,
        };
        MemoryStream memoryStream = new MemoryStream();
        CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(key, key), CryptoStreamMode.Write);
        cryptoStream.Write(value, 0, value.Length);
        cryptoStream.Close();
        Byte[] encrypted = memoryStream.ToArray();
        MessageBox.Show(string.Join(string.Empty, Array.ConvertAll(encrypted, b => b.ToString("X2"))));

ただし、鍵のサイズを訴える CryptographicException がスローされます。インターネットを検索したところ、DES のキー サイズは 8 バイトであることがわかりましたが、Oracle はどのようにテキストを暗号化したのでしょうか? また、Oracle と同じ出力が得られるようにコードを変更するにはどうすればよいでしょうか?

4

1 に答える 1

0

DES traditionally accepts a 64 bit key where only 56 bits are used to encrypt the message. You could try setting the KeySize property of the DESCryptoServiceProvider to 128 and see how it goes.

Are you sure the whole key is used? the doco for this states:

ENCRYPT_DES Data Encryption Standard. Block cipher. Uses key length of 56 bits.

I would assume that only the first 56 bits of the key are used (the first 7 characters). You could try running the encryption method in oracle with the first 7 characters and see if the output is the same compared to all 16.

If that's the case and you really want/need to to use 128 bit keys, you should move up to triple-DES or AES.

于 2013-04-30T07:24:22.683 に答える