在PLSQL中实施3DES加密/解密以处理由VB.net加密的值

问题描述 投票:0回答:1

我们被要求使用PLSQL解密数据流,而PLSQL已由不同平台上的VB.net进程加密。

VB代码如下:

Public Function TruncateHash(ByVal key As String,ByVal length As Integer) As Byte()

            Dim sha1 As New SHA1CryptoServiceProvider

            ' Hash the key.
            Dim keyBytes() As Byte =  System.Text.Encoding.Unicode.GetBytes(key)
            Dim hash() As Byte = sha1.ComputeHash(keyBytes)

            ' Truncate or pad the hash.
            ReDim Preserve hash(length - 1)
            Return hash
        End Function

      Imports System.Security.Cryptography

        Public Function test_encrypt(ByVal plaintext As String, ByVal key As String, ByVal seed As String) As String

            Dim TripleDes As New TripleDESCryptoServiceProvider
            TripleDes.Key = TruncateHash(key, TripleDes.KeySize \ 8)
            TripleDes.IV = TruncateHash(seed, TripleDes.BlockSize \ 8)

            ' Convert the plaintext string to a byte array.
            Dim plaintextBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(plaintext)

            ' Create the stream.
            Dim ms As New System.IO.MemoryStream
            ' Create the encoder to write to the stream.
            Dim encStream As New CryptoStream(ms, TripleDes.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write)

            ' Use the crypto stream to write the byte array to the stream.
            encStream.Write(plaintextBytes, 0, plaintextBytes.Length)
            encStream.FlushFinalBlock()

            ' Convert the encrypted stream to a printable string.
            Return Convert.ToBase64String(ms.ToArray)

        End Function

        Public Function TEST_decrypt(ByVal encryptedtext As String, ByVal key As String, ByVal seed As String) As String

            Dim TripleDes As New TripleDESCryptoServiceProvider
            TripleDes.Key = TruncateHash(key, TripleDes.KeySize \ 8)
            TripleDes.IV = TruncateHash(seed, TripleDes.BlockSize \ 8)


            ' Convert the encrypted text string to a byte array.
            Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedtext)

            ' Create the stream.
            Dim ms As New System.IO.MemoryStream
            ' Create the decoder to write to the stream.
            Dim decStream As New CryptoStream(ms, TripleDes.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write)

            ' Use the crypto stream to write the byte array to the stream.
            decStream.Write(encryptedBytes, 0, encryptedBytes.Length)
            decStream.FlushFinalBlock()

            ' Convert the plaintext stream to a string.
            Return System.Text.Encoding.Unicode.GetString(ms.ToArray)
        End Function

这似乎主要是从MSDN article中拉出来的。

如何使用Oracle 11g企业版在PLSQL中实现这种类型的加密/解密?我已经使用下面的代码刺中了它,但是我得到的值却完全不同。我不知道如何解释TruncateHash函数的padding / truncation元素。

DECLARE 

g_value VARCHAR2 (30 BYTE) DEFAULT 'hello world';
g_key   VARCHAR2 (30 BYTE) DEFAULT 'password';
g_seed  VARCHAR2 (30 BYTE) DEFAULT 'seed';

l_enc   VARCHAR2 (2000 BYTE) DEFAULT NULL;

    FUNCTION get_hash (p_in     VARCHAR2)
    RETURN RAW
    AS
    l_hash  RAW(2000);
    BEGIN

    l_hash  :=  dbms_crypto.hash(utl_raw.cast_to_raw(p_in), dbms_crypto.hash_sh1);

    RETURN l_hash;

    END get_hash;  

FUNCTION get_enc_val (
     p_in_val      IN   VARCHAR2,
       p_key         IN   VARCHAR2,
       p_iv          IN   VARCHAR2 := NULL
    )
       RETURN VARCHAR2
    IS
       l_enc_val    RAW (4000);
       l_enc_algo   PLS_INTEGER;
      l_in         RAW (4000);
      l_iv         RAW (4000);
      l_key        RAW (4000);
      l_ret        VARCHAR2 (4000);
   BEGIN
      l_enc_algo := dbms_crypto.encrypt_3des_2key;
      l_in := utl_raw.cast_to_raw(p_in_val);
      l_iv := get_hash (p_iv);
      l_key := get_hash (p_key);
      l_enc_val :=
         dbms_crypto.encrypt (src      => l_in,
                              KEY      => l_key,
                              iv       => l_iv,
                              typ      =>   l_enc_algo
                                          + dbms_crypto.chain_cbc
                                          + dbms_crypto.pad_pkcs5
                             );
      l_ret := RAWTOHEX (l_enc_val);
      RETURN l_ret;
   END;


BEGIN

l_enc := get_enc_val(g_value, g_key, g_seed);

dbms_output.put_line(utl_raw.cast_to_varchar2(l_enc));
END;

“ hello world”的预期加密值是“ S3UPpm51c919L + C + PqkuxS7avQt5IjXV”。

vb.net encryption plsql oracle11g 3des
1个回答
0
投票

有这个运气吗...?我一直在努力寻找解决方案(在Oracle中加密,在.Net中解密)。如果您找到解决方案,请分享,谢谢!

© www.soinside.com 2019 - 2024. All rights reserved.