在VB.Net中加密并在PHP中解密

时间:2018-04-17 13:56:31

标签: php vb.net encryption

我正在为用VB.Net编写的程序创建一个PHP字符串解密器。我已经对.NET进行了PHP加密和解密方面的研究,我似乎无法找到明确的解决方案。

我是PHP新手,我的强项不在密码学中。看起来他们有很多不同的加密类。(mcryptOpensslSodium

以下是我为VB.Net应用程序提供的代码。

Public Shared Function DecryptString(ByVal cipherTextStringWithSaltAndIv As String, ByVal passPhrase As String) As String

    Dim cipherTextBytesWithSaltAndIv = Convert.FromBase64String(cipherTextStringWithSaltAndIv)
    Dim saltStringBytes = cipherTextBytesWithSaltAndIv.Take(Keysize / 8).ToArray()
    Dim ivStringBytes = cipherTextBytesWithSaltAndIv.Skip(Keysize / 8).Take(Keysize / 8).ToArray()
    Dim cipherTextBytes = cipherTextBytesWithSaltAndIv.Skip((Keysize / 8) * 2).Take(cipherTextBytesWithSaltAndIv.Length - ((Keysize / 8) * 2)).ToArray()

    Dim key As New Rfc2898DeriveBytes(passPhrase, saltStringBytes, 1000)
    Dim keyBytes = key.GetBytes(Keysize / 8)

    Using symmetricKey As New RijndaelManaged()
        symmetricKey.BlockSize = 256
        symmetricKey.Mode = CipherMode.CBC
        symmetricKey.Padding = PaddingMode.ISO10126

        Using decryptor = symmetricKey.CreateDecryptor(keyBytes, ivStringBytes)
            Using memoryStream As New MemoryStream(cipherTextBytes)
                Using cryptoStream As New CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)
                    Dim plainTextBytes As Byte() = New Byte(cipherTextBytes.Length - 1) {}
                    Dim decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length)

                    memoryStream.Close()
                    cryptoStream.Close()

                    Return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount)
                End Using
            End Using
        End Using
    End Using
End Function

这是我无法在PHP中复制的功能。

所以我正在寻找具体的内容。

  
      
  1. 哪个班级/分机?我是否应该使用接收加密字符串并解密它以获得与此VB.Net函数相同的结果。
  2.   
  3. 如果您有任何关于如何解决此问题的示例或任何有助于我进一步理解此问题的文章的链接,我会   非常感激。
  4.   

感谢。

1 个答案:

答案 0 :(得分:0)

你可以使用(如你所说)钠!

Sodium for .NET

另外,您应该查看Public-key authenticated encryption的工作原理。这就是你需要的。该系统提供相互认证。但是,典型的用例是保护预先知道其公钥的服务器与匿名连接的客户端之间的通信。

代码是用C#编写的,但翻译它并不是什么大不了的事。 Die lib本身在vb.net中没有问题

编辑:我为你翻译了这个例子:

Private Sub GetStarted()
    Const MESSAGE As String = "hello bob"
    Dim vbnet As Sodium.KeyPair = Sodium.PublicKeyBox.GenerateKeyPair()
    Dim php As Sodium.KeyPair = Sodium.PublicKeyBox.GenerateKeyPair()
    Dim nonce As Byte() = Sodium.PublicKeyBox.GenerateNonce()
    'vbnet encrypts A message for php
    Dim encrypted As Byte() = Sodium.PublicKeyBox.Create(MESSAGE, nonce, vbnet.PrivateKey, php.PublicKey)
    'php decrypt the message
    Dim decrypted As Byte() = Sodium.PublicKeyBox.Open(encrypted, nonce, php.PrivateKey, vbnet.PublicKey)
    'make the bytes readable
    Dim msg As String = System.Convert.ToBase64String(decrypted)
    decrypted = Convert.FromBase64String(msg)
    msg = System.Text.Encoding.UTF8.GetString(decrypted)
    MsgBox(msg)
End Sub

对于php,您应该检查the PHP String Encryption Example with Libsodium in this answer

相关问题