2

私の問題に対する答えを見つけるためにスタックを見回しましたが、何もうまくいきませんでした。私が達成しようとしているのは、ASYNCtask でダウンロードされた XML ファイルを暗号化し、後で復号化することです。

私がすでにチェックしたこと:

・生成された鍵は暗号化時と復号時と同じで、Base64 で sharedpreferenced に保存されます。

-IV は、現時点ではテスト目的の静的変数であるため、同じです。

-Cipher は AES/CBC/PKCS5Padding に設定されています

-キーはAESに設定されています

エラーは、decryptXml() の行:
byte[] decrypted = cipher.doFinal(bytes);に表示されます。

私はすべてアイデアがなく、何もうまくいかないようです。私のコードで間違いを見つけることができることを願っています。手伝ってくれてありがとう!

コード:

生成キー()

    SharedPreferences sharedPreferences = context.getSharedPreferences(GENERATED_KEY, Context.MODE_PRIVATE);
    String keyStr = sharedPreferences.getString(GENERATED_KEY, null);
    if (keyStr == null) {
        final int outputKeyLength = 128;
        SecureRandom secureRandom = new SecureRandom();
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(outputKeyLength, secureRandom);
        SecretKey key = keyGenerator.generateKey();
        byte[] bytes = key.getEncoded();
        keyStr = Base64.encodeToString(bytes, Base64.DEFAULT);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(GENERATED_KEY, keyStr);
        editor.commit();
        return key.toString();
    }  else {
        return keyStr;
    }

XML 暗号化:

    connection = (HttpURLConnection) url.openConnection();
    connection.connect();
    SecretKey secretKey = getSecretKey(context);
    SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getEncoded(), "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    spec = generateIv(cipher.getBlockSize());
    cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, spec);

    input = connection.getInputStream();
    cis = new CipherInputStream(input, cipher);

    String FILEPATH = context.getFilesDir().getParentFile().getPath();
    File file = new File(FILEPATH, "/download/" + id + "/");
       if (!file.exists()) {
    file.mkdirs();
    }

    xmlFile = new File(FILEPATH + "/download/" + id + "/", "xmldata.xml");
    output = new FileOutputStream(xmlFile);
    cos = new CipherOutputStream(output, cipher);
    byte data[] = new byte[4096];
    int count;
    while ((count = cis.read(data)) != -1) {
       if (isCancelled()) throw new TaskCanceledException();
          cos.write(data, 0, count);
          progress = -1;
          publishProgress();
    }
    if (isCancelled()) throw new TaskCanceledException();

復号化:

public String decryptXml() {
    String data = null;
    File file = new File(context.getFilesDir().getParentFile().getPath() + "/download/" + id + "/xmldata.xml");
    int size = (int) file.length();
    byte[] bytes = new byte[size];
 try {
        SecretKeySpec secretKeySpec = new SecretKeySpec(getSecretKey(context).getEncoded(), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, DownloadBookAsyncTask.spec);
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        bis.read(bytes, 0, bytes.length);
        bis.close();
        byte[] decrypted = cipher.doFinal(bytes);
    }

getSecretKey():

public SecretKey getSecretKey(Context context){
    SharedPreferences sharedPreferences = context.getSharedPreferences(DashboardFragment.GENERATED_KEY, Context.MODE_PRIVATE);
    String stringKey = sharedPreferences.getString(DashboardFragment.GENERATED_KEY, null);
    byte[] encodedKey = Base64.decode(stringKey, Base64.DEFAULT);
    return new SecretKeySpec(encodedKey, 0, encodedKey.length, "AES");
}

編集

IVジェネレーター方式の追加

public AlgorithmParameterSpec generateIv(int size) throws NoSuchAlgorithmException {
    AlgorithmParameterSpec ivspec;
    byte[] iv = new byte[size];
    new SecureRandom().nextBytes(iv);
    ivspec = new IvParameterSpec(iv);
    return ivspec;
}
4

2 に答える 2

3

問題が見つかりました。コードが機能しなかった理由は、暗号化に CipherInputStream を使用したためであり、使用すべきではありませんでした。私はまた、今このように見える復号化方法全体をやり直しました:

  byte[] wholeFileByte = null;
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
    cipher.init(Cipher.DECRYPT_MODE, key, DownloadBookAsyncTask.ivspec);
    File file = new File(context.getFilesDir().getParentFile().getPath() + "/download/" + id + "/xmldata.xml");
    FileInputStream fis = new FileInputStream(file);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    CipherInputStream cis = new CipherInputStream(fis, cipher);
    byte data[] = new byte[4096];
    int count;
    while ((count = cis.read(data)) != -1) {
        bos.write(data, 0, count);
    }

    if(cis != null)
        cis.close();
    if(bos != null)
        bos.close();
    if(fis != null)
        fis.close();

    wholeFileByte = bos.toByteArray();
    String kk = new String(wholeFileByte, "UTF-8");

私が犯した他の間違いは、Cipherがすでに復号化を行っていたとしても、復号化にdoFinalを使用したことであり、それが私のエラーの原因だったと思います。

ありがとうございます

于 2013-12-19T11:09:10.013 に答える