未找到Android GAE KeyFactory SHA1withRSA实现

时间:2014-11-25 17:47:01

标签: java google-app-engine cryptography rsa public-key-encryption

我正在尝试使用Android 4.4.2手机来处理发布密钥并验证其签名。我使用的是Google App Engine。请参阅下面的系统错误和代码。 Tx为你提供帮助。

11-25 18:30:00.271: W/System.err(11042): java.security.NoSuchAlgorithmException: KeyFactory SHA1withRSA implementation not found
11-25 18:30:00.271: W/System.err(11042):    at org.apache.harmony.security.fortress.Engine.notFound(Engine.java:177)
11-25 18:30:00.271: W/System.err(11042):    at org.apache.harmony.security.fortress.Engine.getInstance(Engine.java:151)
11-25 18:30:00.271: W/System.err(11042):    at java.security.KeyFactory.getInstance(KeyFactory.java:81)
11-25 18:30:00.271: W/System.err(11042):    at com.sample.activities.MainActivity$Task.onPostExecute(MainActivity.java:1304)
11-25 18:30:00.276: W/System.err(11042):    at com.sample.activities.MainActivity$Task.onPostExecute(MainActivity.java:1)
11-25 18:30:00.276: W/System.err(11042):    at android.os.AsyncTask.finish(AsyncTask.java:632)
11-25 18:30:00.276: W/System.err(11042):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
11-25 18:30:00.276: W/System.err(11042):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
11-25 18:30:00.276: W/System.err(11042):    at android.os.Handler.dispatchMessage(Handler.java:102)
11-25 18:30:00.276: W/System.err(11042):    at android.os.Looper.loop(Looper.java:146)
11-25 18:30:00.276: W/System.err(11042):    at android.app.ActivityThread.main(ActivityThread.java:5602)
11-25 18:30:00.276: W/System.err(11042):    at java.lang.reflect.Method.invokeNative(Native Method)
11-25 18:30:00.276: W/System.err(11042):    at java.lang.reflect.Method.invoke(Method.java:515)
11-25 18:30:00.276: W/System.err(11042):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
11-25 18:30:00.276: W/System.err(11042):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
11-25 18:30:00.276: W/System.err(11042):    at dalvik.system.NativeStart.main(Native Method)

代码:

try {

                X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(
                        serverPubKey);
                // Now you need a KeyFactory object to do the conversion. That
                // object must be one that works with RSA keys.

                KeyFactory keyFactory = KeyFactory.getInstance("SHA1withRSA");
                // Finally, you can use the KeyFactory object to generate a
                // PublicKey from the key specification.

                PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);

                Signature sig = Signature.getInstance("RSA");
                sig.initVerify(pubKey);
                boolean verifies = sig.verify(dataSignature);

                System.out.println("signature verifies: " + verifies);

        } catch (InvalidKeyException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SignatureException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 

1 个答案:

答案 0 :(得分:0)

仅使用KeyFactory而不是"RSA"来试用"SHA1withRSA"。哈希是签名算法的一部分,但密钥不是特定于签名算法的。

编辑:这是Signature算法的另一种方式,所以也许你只是改变了这两个算法。