2012-05-28 136 views
2

我需要將我的應用程序分爲應用程序和擴展。所以我舉了一個Google Play Downloader Library的例子(android-sdk \ extras \ google \ play_apk_expansion \ downloader_sample)。項目建立良好,但它崩潰在KeyFactory.generate公開硬編碼X.509證書

byte[] decodedKey = Base64.decode(encodedPublicKey); 
KeyFactory keyFactory = KeyFactory.getInstance(KEY_FACTORY_ALGORITHM); // "RSA" 
return keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey));<code> 

,對字符串keyFactory.generatePublic。 從SRC公司的\ com \谷歌\機器人\自動售貨機\許可證\ LicenseChecker.java (Android的SDK \演員\谷歌\ play_licensing \庫\ SRC) 錯誤:

E/AndroidRuntime(523): java.lang.IllegalArgumentException: java.security.spec.InvalidKeySpecException: java.lang.IllegalArgumentException: Bad sequence size: 3 

主要的樣子:

private static final String BASE64_PUBLIC_KEY = 
"MIIB5TCCAU6gAwIBAgIET45f9zANBgkqhkiG9w0BAQUFADA3MQswCQYDVQQGEwJVUzEQMA4GA1UE" + 
"ChMHQW5kcm9pZDEWMBQGA1UEAxMNQW5kcm9pZCBEZWJ1ZzAeFw0xMjA0MTgwNjMyMjNaFw00MjA0" + 
"MTEwNjMyMjNaMDcxCzAJBgNVBAYTAlVTMRAwDgYDVQQKEwdBbmRyb2lkMRYwFAYDVQQDEw1BbmRy" + 
"b2lkIERlYnVnMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDh2IN5HhCp1X+gLaga06VXr/MZ" + 
"JpkzhxMdg5yWyOkj50ZDCPywAh8LcNEih9XjYswAXwRHxZpUy9VFqgGcku33AAdHxyK7KK4ge7u5" + 
"a7KY11CJhxMUbOGezGldMUTwBA0ZSuObfW402I4Y4ciAsMrOnhZqSTI/tTdAWv6cPTiJQQIDAQAB" + 
"MA0GCSqGSIb3DQEBBQUAA4GBAAZ89R7OMtkQnGpE6s/crqUMysAXOaHktrr6mV/4VknoLHWJUsRg" + 
"iv34rAFpd1SDg0HS8HklIymcwFkrewwx9MzryYtZEdBjvo2EeTz5u8nxQNz9sqChaya0oSXB/VI8" + 
"nZBnoJ+n5Zbj7QfIgG7thrT4+n4pIDO83+E6zVW6RnIh"; 

如果我改變關鍵隨機字符串,我得到:

E/AndroidRuntime(478): java.lang.IllegalArgumentException: java.security.spec.InvalidKeySpecException: java.lang.ClassCastException: com.android.org.bouncycastle.asn1.DERApplicationSpecific cannot be cast to com.android.org.bouncycastle.asn1.ASN1Sequence 

我試圖生成密鑰規範,它不會導致錯誤,但塔噸的不是我所需要的:

RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger("12345678", 16), new BigInteger("11", 16)); 
KeyFactory keyFactory = KeyFactory.getInstance(KEY_FACTORY_ALGORITHM); 
return keyFactory.generatePublic(pubKeySpec); 

我重新檢查證書的生成和導出。看起來這是正確的。 密鑰對的產生:

keytool.exe -genkey -keyalg "RSA" -keysize 2048 -v -keystore key2.store -storepass <storepwd> -keypass <keypwd> 

,出口

keytool.exe -export -rfc -storepass <storepwd> -keystore key2.store -file key222.cer 

這是調試代碼。我將在Google Play中使用公開密鑰。 硬編碼x.509證書並在應用中使用它的正確方法是什麼?

回答

2

這不是一個有效的編碼公鑰,它似乎是一個Android調試證書。請注意,公共密鑰!=證書,儘管證書確實包含公鑰。還要注意,任何兩個隨機的BigInteger不構成一個有效的密鑰對(至少它們必須是素數)。要解析的一個實際的證書,你需要這樣的事:

FileInputStream fis = new FileInputStream(filename); 
BufferedInputStream bis = new BufferedInputStream(fis); 

CertificateFactory cf = CertificateFactory.getInstance("X.509"); 
Certificate cert = cf.generateCertificate(bis); 

究竟是你想做些什麼?

+0

哦,我明白了。你是完全正確的。謝謝! – valha

+0

非常感謝Nikolay Elenkov! –