1

私はアンドロイドアプリを開発しています。残念ながら、互換性の問題があります。ソース コードはファームウェア 4.1 で動作しますが、ファームウェア 2.2 のデバイスではクラッシュします。エラーは、ObjectInputStream によって引き起こされた「ClassNotFoundException」のようです。

暗号化と復号化のための私のソースコード:

private Key getPublicKey() {
    try {
        InputStream fis = activity.getResources().openRawResource(R.raw.publick);
        ObjectInputStream ois = new ObjectInputStream(fis);
        Key RSApublicKey = (Key) ois.readObject();
        return RSApublicKey;
    }
    catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

private Key getPrivateKey() {
    try {
        InputStream fis = activity.getResources().openRawResource(R.raw.privatek);
        ObjectInputStream ois = new ObjectInputStream(fis);
        Key RSAprivateKey = (Key) ois.readObject();
        return RSAprivateKey;
    }
    catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    catch (ClassNotFoundException e) {
        Log.e("error", "Error: " + e);
        e.printStackTrace();
    }
    return null;
}

public byte[] encrypt(String data) {
    byte[] encrypted = null;
    try {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, getPublicKey());
        encrypted = cipher.doFinal(data.getBytes());
    }
    catch (IllegalBlockSizeException e) {
            e.printStackTrace();
    }
    catch (BadPaddingException e) {
        e.printStackTrace();
    }
    catch (InvalidKeyException e) {
        e.printStackTrace();
    }
    catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    catch (NoSuchPaddingException e) {
        e.printStackTrace();
    }
    catch (NoSuchProviderException e) {
        e.printStackTrace();
    }
    return encrypted;
}

public String decrypt(byte[] encrypted) {
    byte[] decrypted = null;
    try {
        Cipher cipher;
        cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, getPrivateKey());
        decrypted = cipher.doFinal(encrypted);
    }
    catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    catch (NoSuchPaddingException e) {
        e.printStackTrace();
    }
    catch (InvalidKeyException e) {
        e.printStackTrace();
    }
    catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    }
    catch (BadPaddingException e) {
        e.printStackTrace();
    }
    catch (NoSuchProviderException e) {
        e.printStackTrace();
    }
    return new String(decrypted);
}

logcat のエラー:

03-23 10:13:19.706: E/Error(427): Error: java.lang.ClassNotFoundException: com.android.org.bouncycastle.jce.provider.JCERSAPrivateCrtKey

どうすればAndroid 2.2で動作させることができますか?

4

2 に答える 2

0

Key.getEncoded()メソッドを使用してキーをシリアル化することをお勧めします。はKey.getFormat()、RSA 秘密鍵がデフォルトで PKCS#8 エンコーディングを使用する必要があることをすでに示唆しています。

その後、PKCS8EncodedKeySpecクラスと のインスタンスを使用して"RSA" KeyFactory、結果のバイト配列を逆シリアル化できます。

シリアル化されたキーは、さまざまな実装では正しく機能しない可能性がありPrivateKey(既にわかっているように)、PKCS#8 は明確に定義され、よく使用される標準です。

于 2013-03-23T16:16:22.033 に答える
0

4.1+ では動作するが 2.2 では動作しない場合は、2.2 以降に含まれている android のクラスだと思います。これをテストするには、このクラスを使用するプロジェクトを android 2.2 でコンパイルして、できるかどうかを確認します。その場合は、それを見つけてプロジェクトに含めてください。

于 2013-03-23T11:48:27.350 に答える