RSA Cypher VisualBasic到Java

RSA Cypher VisualBasic到Java,java,vb.net,encryption,rsa,Java,Vb.net,Encryption,Rsa,我在Java中加密一些字符串时遇到问题。我需要以与VisualBasic代码相同的方式对其进行加密: Public Function Encrypt(ByRef EncryptionKeyPair As KeyPair, ByVal PlainText As String) As String //Use Public Key to encrypt m_objRSA.FromXmlString(EncryptionKeyPair.PublicKey.Key)

我在Java中加密一些字符串时遇到问题。我需要以与VisualBasic代码相同的方式对其进行加密:

 Public Function Encrypt(ByRef EncryptionKeyPair As KeyPair, ByVal PlainText As String) As String
        //Use Public Key to encrypt
        m_objRSA.FromXmlString(EncryptionKeyPair.PublicKey.Key)

        //Get Modulus Size and compare it to length of PlainText
        // If Length of PlainText > (Modulus Size - 11), then PlainText will need to be broken into segments of size (Modulus Size - 11)
        //Each of these segments will be encrypted separately
        //    and will return encrypted strings equal to the Modulus Size (with at least 11 bytes of padding)
        //When decrypting, if the EncryptedText string > Modulus size, it will be split into segments of size equal to Modulus Size
        //Each of these EncryptedText segments will be decrypted individually with the resulting PlainText segments re-assembled.

        Dim intBlockSize As Integer = GetModulusSize(EncryptionKeyPair.PublicKey.Key) - 11
        Dim strEncryptedText As String = ""

        While Len(PlainText) > 0
            If Len(PlainText) > intBlockSize Then
                strEncryptedText = strEncryptedText & EncryptBlock(Left(PlainText, intBlockSize))
                PlainText = Right(PlainText, Len(PlainText) - intBlockSize)
            Else
                strEncryptedText = strEncryptedText & EncryptBlock(PlainText)
                PlainText = ""
            End If
        End While

        Return strEncryptedText
    End Function


Private Function EncryptBlock(ByRef TheRSAProvider As RSACryptoServiceProvider, ByVal strIn As String) As String
        Return ByteArrayAsString(TheRSAProvider.Encrypt(StringAsByteArray(strIn), False))
    End Function

 Private Function GetModulusSize(ByVal intKeySize As Integer) As Integer
        //KeySize is in Bits - so divide by 8 to get # of bytes
        Return intKeySize / 8
    End Function
我已经在互联网上搜索过了,但没有找到类似的东西。 我有来自模数和指数的公钥,我正在这样做:

byte[] expBytes = Base64.decode(exponent.trim());
byte[] modBytes = Base64.decode(modulus.trim());

BigInteger modules = new BigInteger(1, modBytes);
BigInteger exponents = new BigInteger(1, expBytes);

KeyFactory factory = KeyFactory.getInstance("RSA");
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(modules, exponents);
PublicKey pubKey = factory.generatePublic(pubSpec);
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] encrypted =cipher.doFinal(field.getBytes("UTF-16LE")); 
String string = new String(encrypted);
结果是不正确的,因为我没有做任何关于模数大小-11的事情。你能给我解释一下用Java怎么做吗


谢谢。

模数大小不是问题。问题更可能是您希望生成相同的值。它们不是,甚至不在VB代码或Java代码中(运行代码片段两次!)。RSA PKCS#1 v1.5填充包含随机数,确保加密始终会产生不同的值。顺便说一下,OAEP填充也是如此


请注意,您可能希望了解OAEP模式和混合密码系统,而不是您现在正在做的事情。这样你会更安全,你可以处理任何大小的数据,当然密文的数量会更大

我知道它会产生不同的价值。问题是我将Java加密字符串发送到VisualBasicWeb服务,但它无法对其进行解密。解密遵循模数大小规则。我刚刚给出了一个加密示例,你需要解密代码吗?不一定,但你应该更新你的问题,因为“结果不正确”列出了有史以来最糟糕的错误描述。我认为标题和描述都很清楚。我所需要的就是用Java实现VisualBasic加密。对不起,我没有看到任何其他错误描述。我想您可以自己与
field.getBytes(“UTF-16LE”).length进行比较?或者,如果超过RSA PKCS#1 v1.5的最大大小,只需捕获发生的异常。