1

以下代码在我的笔记本电脑、服务器和 Android v2.2 上的 JVM 上按预期解密字符串。但是,如果我在 Android 2.3 上使用完全相同的代码,则会得到不正确的输出。我认为这可能是由于其中一个函数的默认值不同,但我很确定我没有使用任何默认值......

    private void test() {

        try {
            String stringMessage="GEQRpAPA577ks/QveudNkk7H9DjItKGLDYW6xhH1YJGabCVzrkejkBh6S+APwEXxB84UV/q0sO5rqkgXWONJQ8CoMTfqXtUkAAwkYHSc86eGewkM8WpctA0AyNVFonOxDCXm84Uq8JRMzqskSH5VXHmMxvHIvpFgdhmt9Ir0cKWzoLsuvgfY9hfypfEyBXGZcoptQeKhsZxRGIlxbXhrFl/LqhC+F6vYtZ/j5pv2LUP38wh2rTCKnAQ+xvC+7wn5SVzt/Wbr/q7GjCoJuU9uFHQSS49KQDt+BzJL2XNwAMmdbC+XHYkEBBWxVSS+0hdSQxoaKVZZJk4hTnHwQlBAkw==";
            //Convert String back to Byte[] and decrpt
            byte[] byteMessage = Base64.decodeBase64(stringMessage.getBytes("UTF-8"));
            System.out.println("ENCRYPTED MESSAGE byte Length: "+byteMessage.length);

            String decryptedMsg = decryptString(byteMessage, loadCASPrivateKey());
            System.out.println(decryptedMsg);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
    }

    private static String decryptString(byte[] message, Key privateKey) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException, NoSuchProviderException {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

        Cipher cipher = Cipher.getInstance("RSA/None/NoPadding","BC");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);

        byte[] cipherData = cipher.doFinal(message);
        return new String(cipherData, "UTF-8");
    }

    private PrivateKey loadCASPrivateKey() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
        InputStream is = getClass().getResourceAsStream( "/keys/app-private.key" );
        if (is == null) {
            System.out.println("NULL");
        }
        byte[] encodedPrivateKey = new byte[(int) 1216];
        is.read(encodedPrivateKey);
        is.close();

        // Generate KeyPair.
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");

        PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey);
        PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);

        return privateKey;

    }

输出:

04-28 15:00:05.969: I/System.out(677): ���;��bW�Q<�tz.�c4Ă핆�Q�"��6����"bW �k!� </p>

4

0 回答 0