2015-01-06 39 views
2

我無法在我的android應用程序中使用Facebook Conceal庫。 crypto似乎總是在crypto.isAvailable()上返回true。 此外,我使用的Android 1.0.2並未明確列出包含的庫,但我已將其放入正確的庫文件夾Facebook隱藏crypto for android始終無法使用

我在前面的鏈接中包含Jar和本機二進制文件。構建中包含的.zip文件爲:

compile fileTree(dir: 'libs', include: ['*.jar', '*.zip']) 

構建似乎工作。我驗證了本地庫:cryptox.soconceal.so包含在apk/libs中,而包含com.facebook.crypto等包的其他jar二進制包含在classes.dex之內。

以下是活動代碼:

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"); 
}}; 

失敗//它拋出在執行這一塊低於java.lang.UnsatisfiedLinkError中的例外:

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

回答

2

Assaf's solution是一個工作。
下面是圖書館需要去的結構。
項目:
| --lib:
| - | --armeabi:
| - | - | - 這樣的文件。

注意,下載的libs.zip有所有libs根文件夾拉鍊的庫文件,需要爲了得到在.apk

0

你可以試着將文件放入。所以正確地捆綁在一起的庫改爲lib/app/src/main/jniLibs /相應地。

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