我正在創建一個跨平臺的Android/Windows應用程序。解密Android RSA:無效的密文異常
我用這個代碼來生成在Android的公鑰,我可以從一個從Windows的應用程序生成的測試公鑰使用:
String AppKeyPub = "MIGHAoGBAONcDWYnbGGOIG1wfHy8v54/2Ch2ZCewcM6TGGtnvHOa/53ekPlCYHXG5UDeaCUxPwPK" +
"Fx9qikj04nxF+tKl9GnV4RS+3kDQPkunlJ4pk52PiKVGaVpOWOli1Y31zJJZ9ufqLySEycJVuqiI" +
"Z9kektzkHdAIxNKlPDn4GQa2mjz/AgER";
try {
// PREP PUBLIC KEY
byte[] decoded = Base64.decode(AppKeyPub,0);
org.bouncycastle.asn1.pkcs.RSAPublicKey pkcs1PublicKey = org.bouncycastle.asn1.pkcs.RSAPublicKey.getInstance(decoded);
BigInteger modulus = pkcs1PublicKey.getModulus();
BigInteger publicExponent = pkcs1PublicKey.getPublicExponent();
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(modulus, publicExponent);
KeyFactory kf = KeyFactory.getInstance("RSA");
PublicKey publicKey = kf.generatePublic(keySpec);
我用這個代碼,然後加密測試消息:
byte[] input = "Hello from Android!".getBytes("UTF-8");
Cipher cipher = Cipher.getInstance("RSA", "BC");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
ctLength += cipher.doFinal(cipherText, ctLength);
String encodedData = Base64.encodeToString(cipherText, messageCount);
System.out.println(new String(encodedData));
System.out.println(ctLength);
這是由Android生成的加密測試消息:
fy1l1g/Tpxer4mR3bO6WQdfmi93I/YjpZZ DGvIiZ6UU/VZWhnmgmuU1zM6EqwppqQTMkfsKPk5kAWhSYH8 + tbyvgh/Cd48rTKJ39MCfnwCNZvSvNKETZbhgy5fVGL/Uisn16AOae0DI4gV4kubrGswhEFUpyp8seAPclKgHbGuQ =
問題是,當我嘗試在Windows的應用程序失敗,出現錯誤消息對消息進行解密:
RSA/OAEP-MGF1(SHA -1):無效的密文
我試過不同的Android BC算法組合,它們都給了我相同的結果。我也試過no_wrap no_padding等。誰能告訴我我做錯了什麼?感謝您的任何建議。
是Windows應用程序解密之前執行Base64編碼解碼? –
不,你不是。你提供了一個雙端問題的一端。假設你沒有正確加密它是壓倒性的。 – EJP