我正在尝试使用隐藏的facebook库来加密和解密某些音频文件。 因此加密阶段没有问题,但随后的解密阶段始终以以下错误结束:
com.facebook.crypto.cipher.NativeGCMCipherException: decryptFinal
at com.facebook.crypto.cipher.NativeGCMCipher.decryptFinal(NativeGCMCipher.java:108)
at com.facebook.crypto.streams.NativeGCMCipherInputStream.ensureTagValid(NativeGCMCipherInputStream.java:126)
at com.facebook.crypto.streams.NativeGCMCipherInputStream.read(NativeGCMCipherInputStream.java:91)
at com.facebook.crypto.streams.NativeGCMCipherInputStream.read(NativeGCMCipherInputStream.java:76)
这是我的代码:
public class Main extends Activity {
private Button btnEncrypt, btnDecrypt, btnPlay, btnStop;
private final String mediaPath = Environment.getExternalStorageDirectory().getPath();
private File file;
private String media;
private NativeCryptoLibrary mNativeCryptoLibrary;
private Crypto crypto;
private keyChain key;
private Entity entity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnEncrypt = (Button) findViewById(R.id.btnEncrypt);
btnDecrypt = (Button) findViewById(R.id.btnDecrypt);
mNativeCryptoLibrary = new SystemNativeCryptoLibrary();
key = new keyChain();
crypto = new Crypto(key, mNativeCryptoLibrary);
if (!crypto.isAvailable()) {
Log.e("ERROR: ", "crypto not available!");
return;
}
media = mediaPath+"/example.mp3";
file = new File(media);
entity = new Entity("TEST");
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
btnEncrypt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
OutputStream fileStream, outputStream;
try {
final byte[] encrypt = new byte[(int) file.length()];
fileStream = new BufferedOutputStream(new FileOutputStream(media));
// encrypt
outputStream = crypto.getCipherOutputStream(fileStream, entity);
outputStream.write(encrypt);
outputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CryptoInitializationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (KeyChainException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnDecrypt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FileInputStream fileStream;
OutputStream out;
try {
fileStream = new FileInputStream(media);
InputStream inputStream = crypto.getCipherInputStream(fileStream, entity);
out = new FileOutputStream (Environment.getExternalStorageDirectory().getPath()+"/decrypt.mp3");
int read;
byte[] buffer = new byte[1024];
while ((read = inputStream.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
inputStream.close();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CryptoInitializationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (KeyChainException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
我发现这篇文章Problems with facebooks conceal library但我仍无法解决我的问题。