首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >.NET到Progress 4GL加密技术

.NET到Progress 4GL加密技术
EN

Stack Overflow用户
提问于 2013-04-30 21:36:26
回答 1查看 868关注 0票数 0

我有一个VB.NET应用程序,我正在尝试将加密信息发送到Progress 4GL 10.2B应用程序。我匹配了迭代计数、Salt、Private Key和Initialization Vector。我不明白为什么我们要生成不同的密钥。我在.NET中使用silverlight,rfc2898derivebytes类使用HMACSHA-1散列算法,正如进程使用SHA-1散列算法。我正在尝试如何将这两个系统映射为传输简单的字符串。

下面是VB.NET代码

代码语言:javascript
复制
enter codePublic Class OASISCryptography

Const _privateKey As String = "MEXMUELLER"

Public Shared Function GetSalt() As Byte()
    Dim salt() As Byte = New Byte(7) {}
    Dim s As New RNGCryptoServiceProvider
    s.GetBytes(salt)
    Return salt
End Function

Public Shared Function EncryptValue(ByVal data As String, ByVal salt() As Byte) As Byte()
    Using myAes As New AesManaged()
        Dim salt2() As Byte = System.Text.Encoding.UTF8.GetBytes("JosephsT")
        Dim Key As System.Security.Cryptography.Rfc2898DeriveBytes = New Rfc2898DeriveBytes(_privateKey, salt, 2000)
        myAes.Key = Key.GetBytes(128 / 8)
        myAes.IV = System.Text.Encoding.UTF8.GetBytes("HR$2pIjHR$2pIj12")
        Dim encrypted As Byte() = EncryptStringFunction(data, myAes.Key, myAes.IV)
        Return encrypted
    End Using
End Function

Public Shared Function DecryptValue(ByVal data() As Byte, ByVal salt() As Byte) As String
    Using myAes As New AesManaged()
        Dim salt2() As Byte = System.Text.Encoding.UTF8.GetBytes("JosephsT")
        Dim Key As System.Security.Cryptography.Rfc2898DeriveBytes = New Rfc2898DeriveBytes(_privateKey, salt2, 2000)
        myAes.Key = Key.GetBytes(128 / 8)
        Dim genKey As String = Nothing
        For Each g In myAes.Key
            genKey = genKey & g
        Next
        myAes.IV = System.Text.Encoding.UTF8.GetBytes("HR$2pIjHR$2pIj12")
        Dim decrypted As String = DecryptStringFunction(data, myAes.Key, myAes.IV)
        Return decrypted
    End Using
End Function

Public Shared Function EncryptStringFunction(ByVal data As String, ByVal Key() As Byte, ByVal IV() As Byte) As Byte()
    'check arguments
    If data Is Nothing OrElse data.Length <= 0 Then
        Throw New ArgumentNullException("data")
    End If
    If Key Is Nothing Or Key.Length <= 0 Then
        Throw New ArgumentNullException("Key")
    End If
    If IV Is Nothing Or IV.Length <= 0 Then
        Throw New ArgumentNullException("IV")
    End If
    Dim encrypted() As Byte
    Using aesALG As New AesManaged()
        aesALG.Key = Key
        aesALG.IV = IV
        ' Create a encryptor to perform the stream transform. 
        Dim encryptor As ICryptoTransform = aesALG.CreateEncryptor(aesALG.Key, aesALG.IV)
        ' Create the streams used for encryption. 
        Using msEncrypt As New MemoryStream
            Using csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
                Using swEncrypt As New StreamWriter(csEncrypt)
                    'Write all data to the stream.
                    swEncrypt.Write(data)
                End Using
            End Using
            encrypted = msEncrypt.ToArray
        End Using
    End Using
    Return encrypted
End Function

Public Shared Function DecryptStringFunction(ByVal encrytedData() As Byte, ByVal Key() As Byte, ByVal IV() As Byte) As String
    If encrytedData Is Nothing Or encrytedData.Length <= 0 Then
        Throw New ArgumentNullException("EncryptedData")
    End If
    If Key Is Nothing Or Key.Length <= 0 Then
        Throw New ArgumentNullException("Key")
    End If
    If IV Is Nothing Or IV.Length <= 0 Then
        Throw New ArgumentNullException("IV")
    End If
    Dim plainText As String = Nothing
    Using aesALG As New AesManaged()
        aesALG.Key = Key
        aesALG.IV = IV
        Dim decryptor As ICryptoTransform = aesALG.CreateDecryptor(aesALG.Key, aesALG.IV)
        Using msDecrypt As New MemoryStream(encrytedData)
            Using csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)
                Using srDecrypt As New StreamReader(csDecrypt, True)                     
                    plainText = srDecrypt.ReadToEnd
                End Using
            End Using
        End Using
    End Using
    Return plainText
End Function
End Class

下面是进度实现

代码语言:javascript
复制
SECURITY-POLICY:SYMMETRIC-ENCRYPTION-ALGORITHM = "AES_CBC_128".
SECURITY-POLICY:PBE-KEY-ROUNDS = 2000.
SECURITY-POLICY:PBE-HASH-ALGORITHM = "SHA-1".   
 /*SECURITY-POLICY:SYMMETRIC-ENCRYPTION-IV = ? /*mymemptr*/ /*IV*/. */

/* first, decrypt username */
SECURITY-POLICY:ENCRYPTION-SALT = ip-value3.
SECURITY-POLICY:SYMMETRIC-ENCRYPTION-KEY = GENERATE-PBE-KEY (password,ip-value3).
qadusername = get-string(DECRYPT (ip-value1),1).
qadpasswd = get-string(DECRYPT (ip-value2),1).
output to /tmp/decrypt.tst append.
put unformatted qadusername "-" qadpasswd skip .
output close.

qadusername = replace(qadusername,"SPD/","").
for first usr_mstr no-lock where
usr_mstr.usr_active and
usr_userid = qadusername and
usr_passwd = encode(qadpasswd) :
op-value1= true.
END.

/* second, encrypt a value and send back*/
/*
SECURITY-POLICY:ENCRYPTION-SALT = GENERATE-PBE-SALT.*/
salt-value = SECURITY-POLICY:ENCRYPTION-SALT.
/*
SECURITY-POLICY:SYMMETRIC-ENCRYPTION-KEY = GENERATE-PBE-KEY (password,ip-value3).
 */
op-value2 = ENCRYPT ("joe wears a tootoo and a dress!").
op-value3 = salt-value.
op-value4 = IV.
op-value5 = GENERATE-PBE-KEY (password).
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-02 02:59:50

我知道我是在回答我自己的问题,但如果有人有更好的方法,请让我知道……对于任何想要实现这一点的人,你都需要这段代码。我现在有静脉静电,但你通常不会这么做。您将动态地生成它并传递它

代码语言:javascript
复制
 Public Shared Function EncryptValue(ByVal data As String, ByVal salt() As Byte) As Byte()
    Using myAes As New AesManaged
        Dim key2 As PasswordDeriveBytes = New PasswordDeriveBytes(_privateKey, salt)
        key2.IterationCount = 2000
        key2.HashName = "SHA1"
        myAes.Padding = PaddingMode.PKCS7
        myAes.Mode = CipherMode.CBC
        myAes.KeySize = myAes.LegalKeySizes(0).MinSize
        myAes.BlockSize = myAes.LegalBlockSizes(0).MinSize
        myAes.IV = System.Text.Encoding.UTF8.GetBytes("HR$2pIjHR$2pIj12")
        myAes.Key = key2.GetBytes(16)
        Dim encrypted As Byte() = EncryptStringFunction(data, myAes.Key, myAes.IV)
        Return encrypted
    End Using
End Function

Public Shared Function DecryptValue(ByVal data() As Byte, ByVal salt() As Byte) As String
    Using myAes As New AesManaged
        Dim key2 As PasswordDeriveBytes = New PasswordDeriveBytes(_privateKey, salt)
        key2.IterationCount = 2000
        key2.HashName = "SHA1"            
        myAes.Padding = PaddingMode.PKCS7
        myAes.Mode = CipherMode.CBC
        myAes.KeySize = myAes.LegalKeySizes(0).MinSize
        myAes.BlockSize = myAes.LegalBlockSizes(0).MinSize
        myAes.IV = System.Text.Encoding.UTF8.GetBytes("HR$2pIjHR$2pIj12")
        myAes.Key = key2.GetBytes(16)
        Dim decrypted As String = DecryptStringFunction(data, myAes.Key, myAes.IV)
        Return decrypted
    End Using
End Function
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16301282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档