PHPSecLib RSA密钥和VB.NET

时间:2013-10-23 02:14:32

标签: php vb.net encryption rsa phpseclib

简短说明:

我需要在PHP中生成RSA密钥对,然后才能在VB.NET中使用它来加密/解密文件/字符串。

我的文件加密/解密VB.NET:http://jandrozd.eu/RSAFileEncryption.zip

基于此:http://www.tma.dk/rsa/

使用.NET生成的密钥,而不是使用PHPSecLib生成的密钥。

详细信息:

我正在为一家公司创建内部信息系统。这个IS基于VB.NET桌面客户端和PHP服务器。由于客户安全要求,我们在存储文件或字符串时被迫使用RSA加密。它还需要在转移时/之前进行加密。

我在PHP中寻找生成RSA密钥对的代码。我找到了phpseclib,它可以很好地生成它。我在服务器上使用它:

$rsa = new Crypt_RSA();

$rsa->setEncryptionMode(CRYPT_RSA_PUBLIC_FORMAT_PKCS1);
$rsa->setPrivateKeyFormat(CRYPT_RSA_PRIVATE_FORMAT_XML);
$rsa->setPublicKeyFormat(CRYPT_RSA_PUBLIC_FORMAT_XML);

//define('CRYPT_RSA_EXPONENT', 65537);
//define('CRYPT_RSA_SMALLEST_PRIME', 64); // makes it so multi-prime RSA is used
extract($rsa->createKey(512)); // == $rsa->createKey(1024) where 1024 is the key size

我还存储了生成密钥的文本文件。当我尝试在VB.NET中使用这些密钥加密和解密时,问题就出现了。 使用phpseclib生成的密钥加密似乎在VB.NET中运行良好,但是我无法解密VB.NET中的文件,带有“错误数据错误”的字符串基础文件解密vb代码

Public Sub EncryptFile(ByVal strFilePath As String, ByVal strNewPath As String, ByVal strPublicKey As String)
        Try
            If File.Exists(strFilePath) Then
                Dim bts() As Byte = My.Computer.FileSystem.ReadAllBytes(strFilePath)

                Dim EncryptedMessage As RSAResult = RSA.Encrypt(bts, strPublicKey)

                My.Computer.FileSystem.WriteAllBytes(strNewPath, EncryptedMessage.AsBytes, False)
                If EnableMessageBoxes Then MsgBox("Encryption complete", MsgBoxStyle.Information)
            Else
                If EnableMessageBoxes Then MsgBox("File to encrypt does not exists", MsgBoxStyle.Exclamation)
            End If
        Catch ex As Exception
            If EnableMessageBoxes Then MsgBox("Encryption error: " & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Public Sub DecryptFile(ByVal strFilePath As String, ByVal strNewPath As String, ByVal strPrivateKey As String)
        Try
            If File.Exists(strFilePath) Then
                Dim bts() As Byte = My.Computer.FileSystem.ReadAllBytes(strFilePath)

                Dim DecryptedMessage As RSAResult = RSA.Decrypt(bts, strPrivateKey)

                My.Computer.FileSystem.WriteAllBytes(strNewPath, DecryptedMessage.AsBytes, False)
                If EnableMessageBoxes Then MsgBox("Decryption complete", MsgBoxStyle.Information)
            Else
                If EnableMessageBoxes Then MsgBox("File to encrypt does not exists", MsgBoxStyle.Exclamation)
            End If
        Catch ex As Exception
            If EnableMessageBoxes Then MsgBox("Decryption error: " & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

VB.NET RSA类

Imports System.Security.Cryptography
Imports System.Text
Public Class RSA
    Public Shared Function Encrypt(ByVal Data As String, ByVal Publickey As String) As RSAResult
        Try
            Dim ByteConverter As New UnicodeEncoding()
            Return Encrypt(ByteConverter.GetBytes(Data), Publickey)
        Catch ex As Exception
            Throw New Exception("Encrypt(String): " & ex.Message, ex)
        End Try
    End Function

    Public Shared Function Encrypt(ByVal Data() As Byte, ByVal Publickey As String) As RSAResult
        Try
            Dim RSA As System.Security.Cryptography.RSACryptoServiceProvider = New System.Security.Cryptography.RSACryptoServiceProvider()
            RSA.FromXmlString(Publickey)
            Return New RSAResult(RSAEncrypt(Data, RSA.ExportParameters(False), False))
        Catch ex As Exception
            Throw New Exception("Encrypt(Bytes): " & ex.Message, ex)
        End Try
    End Function

    Public Shared Function Decrypt(ByVal Data() As Byte, ByVal Privatekey As String) As RSAResult
        Try
            Dim RSA As System.Security.Cryptography.RSACryptoServiceProvider = New System.Security.Cryptography.RSACryptoServiceProvider()
            RSA.FromXmlString(Privatekey)
            Dim Result As New RSAResult(RSADecrypt(Data, RSA.ExportParameters(True), False))
            Return Result
        Catch ex As Exception
            Throw New Exception("Decrypt(): " & ex.Message, ex)
        End Try
    End Function

    Private Shared Function RSAEncrypt(ByVal DataToEncrypt() As Byte, ByVal RSAKeyInfo As RSAParameters, ByVal DoOAEPPadding As Boolean) As Byte()
        Try
            Dim encryptedData() As Byte
            Using RSA As New RSACryptoServiceProvider
                RSA.ImportParameters(RSAKeyInfo)
                encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding)
            End Using
            Return encryptedData
        Catch e As CryptographicException
            Throw New Exception("RSAEncrypt(): " & e.Message, e)
        End Try
    End Function

    Private Shared Function RSADecrypt(ByVal DataToDecrypt() As Byte, ByVal RSAKeyInfo As RSAParameters, ByVal DoOAEPPadding As Boolean) As Byte()
        Try
            Dim decryptedData() As Byte
            Using RSA As New RSACryptoServiceProvider
                RSA.ImportParameters(RSAKeyInfo)
                decryptedData = RSA.Decrypt(DataToDecrypt, DoOAEPPadding)
            End Using
            Return decryptedData
        Catch e As CryptographicException
            Throw New Exception("RSADecrypt(): " & e.Message, e)
        End Try
    End Function

RSAResult类:

Imports System.Text
Public Class RSAResult
    Private _Data() As Byte
    Public Sub New(ByVal Data() As Byte)
        _Data = Data
    End Sub
    Public ReadOnly Property AsBytes() As Byte()
        Get
            Return _Data
        End Get
    End Property
    Public ReadOnly Property AsString() As String
        Get
            Dim ByteConverter As New UnicodeEncoding()
            Return ByteConverter.GetString(_Data)
        End Get
    End Property
    Public ReadOnly Property AsBase64String() As String
        Get
            Return Convert.ToBase64String(_Data)
        End Get
    End Property
End Class

示例键geenerated:

个人:

<RSAKeyValue>
  <Modulus>AN3mGF2XtuTBC7jaBhPrVNYyheYX4HuCkhRpRXOCcKVOseahQBokzn555hIW0fK3kamVLOkSvSF6hP8rt1PC/Qs=</Modulus>
  <Exponent>AQAB</Exponent>
  <P>APeyh3iQRVNleEHNEx8cVCxYMIA7pCiSdYDGWJwf9meZ</P>
  <Q>AOVWMMpSkGlHtHe8aIgu9xw1lUiJ6VZIxVckPyj8oOBD</Q>
  <DP>AM20C+FKHuilSfuLbafWhOjWzGCSJ0AycTbigdAWkzFx</DP>
  <DQ>AMCTd7TtT8aYJ7rDwyNYDLjrZcfbxsxlnxBlp4PLX2vx</DQ>
  <InverseQ>AJ2/aZW5UR/pkQeA6BMKdhqqTWa5mjzMlnOga6zPlRdN</InverseQ>
  <D>AShwPBzMkYkIXKCWo4f4211MIZVCUKKvzHd4K1Ak8lfiTH4oxB8fgq4aa2QT5ufDwedlKGJLnuY6Kv9SpkMOIQ==</D>
</RSAKeyValue>

公开:

<RSAKeyValue>
  <Modulus>AN3mGF2XtuTBC7jaBhPrVNYyheYX4HuCkhRpRXOCcKVOseahQBokzn555hIW0fK3kamVLOkSvSF6hP8rt1PC/Qs=</Modulus>
  <Exponent>AQAB</Exponent>
</RSAKeyValue>

0 个答案:

没有答案