用于Android的Facebook Conceal加密始终不可用

时间:2015-01-06 20:46:49

标签: android encryption

我无法在我的Android应用程序中使用Facebook Conceal库。 crypto似乎总是在crypto.isAvailable()上返回compile fileTree(dir: 'libs', include: ['*.jar', '*.zip']) 。 此外,我使用的是没有明确列出所包含的库的Android 1.0.2,但我已将其放在正确的 libs 文件夹中。

我在前一个链接中包含了Jar和Native Binaries。 .zip文件包含在构建中:

cryptox.so

构建似乎有效。我验证了本地库:conceal.socom.facebook.crypto包含在apk / libs中,而另一个包含classes.dex的jar二进制文件包含在import com.facebook.crypto.Crypto; import com.facebook.crypto.Entity; import com.facebook.crypto.keychain.SharedPrefsBackedKeyChain; import com.facebook.crypto.util.SystemNativeCryptoLibrary; public class ConcealActivity extends Activity implements View.OnClickListener { public static String filename = "data.txt"; public static String filepath = "concealedata"; public static String TAG = "com.zing.ConcealActivity"; File myInternalFile; Crypto crypto; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_conceal); Log.i(TAG, "created method."); ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext()); File directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE); myInternalFile = new File(directory, filename); crypto = new Crypto(new SharedPrefsBackedKeyChain(this), new SystemNativeCryptoLibrary()); Button cencrypt = (Button) findViewById(R.id.cencrypt); cencrypt.setOnClickListener(this); Button cdecrypt = (Button) findViewById(R.id.cdecrypt); cdecrypt.setOnClickListener(this); } @Override public void onClick(View v) { Log.i(TAG, "onclick method."); EditText myInputText = (EditText) findViewById(R.id.inputText); TextView responseText = (TextView) findViewById(R.id.responseText); String myData = ""; switch (v.getId()) { case R.id.cencrypt: encryptandsave(myInputText.getText().toString().getBytes()); myInputText.setText(""); responseText.setText(readFile(myInternalFile)); break; case R.id.cdecrypt: responseText.setText(readanddecrypt()); break; } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_conceal, 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(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } 中。

以下是活动代码:

// Encrypts the data and saves to directory
public void encryptandsave(byte[] plainTextBytes) {
    try {
    // Check for whether the crypto functionality is available
    // This might fail if android does not load libaries correctly.
        if (!crypto.isAvailable()) {
            Log.e(TAG, "Cipher unavailable.");
            return;
        }

        OutputStream fileStream = new BufferedOutputStream(new FileOutputStream(myInternalFile));
        OutputStream outputStream = crypto.getCipherOutputStream(fileStream, new Entity("Password"));
        outputStream.write(plainTextBytes);
        outputStream.close();
    } catch (Exception e) {
        Log.e(TAG, "EXCEPTION encryptandsave: " + e.getMessage());
    }
}

// decode encrypted file and returns Bitmap
private String readanddecrypt() {
    try {
        if (!crypto.isAvailable()) {
            Log.e(TAG, "Cipher unavailable.");
            return "";
        }
        FileInputStream fileStream = new FileInputStream(filename);
        InputStream inputStream = crypto.getCipherInputStream(fileStream, new Entity("Password"));
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        int read;
        byte[] buffer = new byte[1024];
        while ((read = inputStream.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        inputStream.close();
        return out.toString();
    } catch (Exception e) {
        Log.e(TAG, "EXCEPTION readanddecrypt: " + e.getMessage());
    }
    return null;
}

public String readFile(File internalFile) {
    StringBuilder sb = new StringBuilder();
    try {
        FileInputStream fis = new FileInputStream(internalFile);
        DataInputStream in = new DataInputStream(fis);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        while ((strLine = br.readLine()) != null) {
            sb.append(strLine);
        }
        in.close();
    } catch (IOException e) {
        Log.e(TAG, "EXCEPTION readFile: " + e.getMessage());
        return "";
    }
    return sb.toString();
}

//始终满足下面的第一个条件,这意味着未正确加载库。

com.facebook.crypto.util.SystemNativeCryptoLibrary.java

private static final ArrayList<String> LIBS = new ArrayList<String>() {{
  add("cryptox");
  add("conceal");
}};

}

我不确定库或代码是否存在问题。

在jar中,似乎代码失败了

for (String name : LIBS) {
  System.loadLibrary(name);

//在执行以下部分时抛出java.lang.UnsatisfiedLinkError异常:

{{1}}

2 个答案:

答案 0 :(得分:2)

Assaf's solution是有效的 以下是图书馆需要采用的结构 项目:
| --lib:
| - | --armeabi:
| - | - | - 。so文件。

请注意,下载的libs.zip将所有库文件压缩到libs根文件夹中,需要将其更改为lib才能将库正确捆绑在{{1}中}}

答案 1 :(得分:0)

您可以尝试将.so文件放到/ app / src / main / jniLibs /中。

app
+-src
  +-main
    +-jniLibs
      +-armeabi
      | +-libconceal.so
      +-armeabi-v7a
      | +-libconceal.so
      +-mips
      +-x86
        +-libconceal.so