【问题标题】:Encrypting in VB.Net and Decrypting in PHP在 VB.Net 中加密和在 PHP 中解密
【发布时间】:2018-09-27 12:53:56
【问题描述】:

我正在为一个用 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. 如果您有任何关于如何解决此问题的示例或任何可以帮助我进一步理解此问题的文章链接,我会 非常感谢。

谢谢。

【问题讨论】:

  • ".NET 到 PHP 加密和解密"...这不是一个真正有意义的短语。两端用于控制过程的语言实际上应该不是特别相关。重要的是在双方使用相同的加密方案来读取/写入数据。只要用于实现加密的库是正确且合规的,您就可以选择要使用的库,然后继续使用。方案的选择取决于您和您的业务需求/限制。
  • 您可以使用mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $string_to_decrypt, MCRYPT_MODE_CBC, $iv);。这里有例子:php.net/manual/en/function.mcrypt-decrypt.php
  • 将您的数据分成三部分 - salt、iv 和 ciphertex。将 hash_pbkdf2 与您的密码、盐和 1000 次迭代一起用作密钥。对于解密,您必须使用上面评论中提到的mcrypt_decrypt,因为openssl_decrypt 不支持256 位块。祝你好运!

标签: php vb.net encryption


【解决方案1】:

你可以使用(正如你已经说过的)钠!

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

【讨论】:

  • 是什么让你认为 OP 需要公钥加密?
猜你喜欢
  • 2021-09-02
  • 2010-11-19
  • 2013-02-12
  • 2019-06-26
  • 2015-02-05
  • 1970-01-01
  • 2013-02-06
  • 2012-05-23
  • 2019-10-02
相关资源
最近更新 更多