Java 使用openssl C++;/解密错误

Java 使用openssl C++;/解密错误,java,android,encryption,rsa,Java,Android,Encryption,Rsa,我使用以下方法在Android上生成rsa public/private: KeyPairGenerator kpg; KeyPair kp; PublicKey publicKey; PrivateKey privateKey; kpg = KeyPairGenerator.getInstance("RSA"); kpg.initialize(2048); kp = kpg.genKeyPair(); publicKey = kp.getPublic(); privateKey = kp.ge

我使用以下方法在Android上生成rsa public/private:

KeyPairGenerator kpg;
KeyPair kp;
PublicKey publicKey;
PrivateKey privateKey;
kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(2048);
kp = kpg.genKeyPair();
publicKey = kp.getPublic();
privateKey = kp.getPrivate();
然后我将公钥发送到Windows并使用OpenSSL库加密数据:

int resEncryption = RSA_public_encrypt(length, in, encrypted, rsaPkey, RSA_PKCS1_OAEP_PADDING);
然后我在Windows上将加密转换为十六进制:

stringstream ss;
for (int i = 0; i < resEncryption; i++)
    ss << std::hex << (int)encrypted[i] << std::setfill('0');

string hexEncrypted = ss.str();
但是我在CipherSpi.class中的engineDoFinal方法中得到一个错误:

protected byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen) throws IllegalBlockSizeException, BadPaddingException {
    if(input != null) {
        this.bOut.write(input, inputOffset, inputLen);
    }

    if(this.cipher instanceof RSABlindedEngine) {
        if(this.bOut.size() > this.cipher.getInputBlockSize() + 1) {
            throw new ArrayIndexOutOfBoundsException("too much data for RSA block");
        }
    } else if(this.bOut.size() > this.cipher.getInputBlockSize()) {
        throw new ArrayIndexOutOfBoundsException("too much data for RSA block");//Error here 
    }

    return this.getOutput();
} 

看来我用C++进行了十六进制转换,这解决了我的问题。现在我在Android上得到了预期长度的十六进制字符串

string asciiResEnc(reinterpret_cast<char*>(encrypted), resEncryption);
stringstream ss;
for(int i = 0; i < asciiResEnc.size(); i++)
    ss << std::hex << setw(2) << setfill('0') << (int) (unsigned char)asciiResEnc[i];
字符串asciiResEnc(重新解释(加密),重新加密);
细流ss;
对于(int i=0;i将十六进制字符转换为字节的方法不可靠,因此是不正确的。另外,没有像
String.toBytes()
这样的方法,所以你的代码不会被编译。@JamesKPolk true我编辑了我的问题(我想放的是getBytes)@JamesKPolk你会用什么把十六进制字符转换成字节?我认为保留这个问题没有任何意义,因为它对其他感兴趣的人没有任何用处“使用openssl C++在Windows上进行RSA加密”。公认的自我回答与此无关,因为该漏洞与二进制到十六进制转换有关,而不是与加密有关。我将投票结束这个问题,但不幸的是,我不能,因为它有一个积极的悬赏。
protected byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen) throws IllegalBlockSizeException, BadPaddingException {
    if(input != null) {
        this.bOut.write(input, inputOffset, inputLen);
    }

    if(this.cipher instanceof RSABlindedEngine) {
        if(this.bOut.size() > this.cipher.getInputBlockSize() + 1) {
            throw new ArrayIndexOutOfBoundsException("too much data for RSA block");
        }
    } else if(this.bOut.size() > this.cipher.getInputBlockSize()) {
        throw new ArrayIndexOutOfBoundsException("too much data for RSA block");//Error here 
    }

    return this.getOutput();
} 
string asciiResEnc(reinterpret_cast<char*>(encrypted), resEncryption);
stringstream ss;
for(int i = 0; i < asciiResEnc.size(); i++)
    ss << std::hex << setw(2) << setfill('0') << (int) (unsigned char)asciiResEnc[i];