Facebook Conceal - 图像加密和解密

2023-12-14

我正在尝试使用 Facebook Conceal Library 加密和解密图像。这是我第一次使用它,因此如果它微不足道,请耐心等待。我查看了有关 SO 的其他问题,以找出异常的原因,但我无法使其正常工作。

这是我到目前为止所做的......

集成:我使用的是 Eclipse,因此从以下位置下载了 crypto.jar 和 libs.ziphere并将 jar 文件添加到 libs 文件夹,将 .so 文件添加到 libs 文件夹内的相应文件夹。

我的场景:

我必须从相机捕获图像,加密并将其存储在手机内存中。解密它并在图像视图中显示它。在稍后阶段,我还需要从内存中解密该图像并通过网络发送它。

所以,我的代码如下......

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (Const.DEBUGGING) {
        Log.d(Const.DEBUG, "RequestCode: " + requestCode + "\nResultCode:"
                + resultCode);
    }

    int tag = getRecordCount();
    tag++;

    if (requestCode == KTP_PICTURE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {

            ENCRYPTEDFILENAME = tag + "_" + KTP_TAG + ".png";

            saveFile((Bitmap) data.getExtras().get("data"), requestCode);
            Bitmap decryptedImage = decodeFile(ENCRYPTEDFILENAME);
            mImgBtnKtppicture.setImageBitmap(decryptedImage);

        } else if (resultCode == RESULT_CANCELED) {
            if (Const.DEBUGGING)
                Log.d(Const.DEBUG, "KTP_RESULT CANCELED");
        } else {

        }
    }

    if (requestCode == PROFILE_PICTURE_REQUEST_CODE) {

        if (resultCode == RESULT_OK) {

            ENCRYPTEDFILENAME = tag + "_" + PROFILE_TAG + ".png";

            saveFile((Bitmap) data.getExtras().get("data"), requestCode);
            Bitmap decryptedImage = decodeFile(ENCRYPTEDFILENAME);
            mImgBtnPicture.setImageBitmap(decryptedImage);

        } else if (resultCode == RESULT_CANCELED) {
            if (Const.DEBUGGING)
                Log.d(Const.DEBUG, "PICTURE_RESULT CANCELED");
        } else {

        }

    }
}

保存存档():

public void saveFile(Bitmap photo, int code) {

    try {
        ContextWrapper cw = new ContextWrapper(getApplicationContext());
        File directory = cw.getDir(DIRECTORY, Context.MODE_PRIVATE);
        File mypath = new File(directory, ENCRYPTEDFILENAME);

        if (code == KTP_PICTURE_REQUEST_CODE) {
            mKtppicture = Uri.fromFile(mypath).toString();

            if (Const.DEBUGGING)
                Log.d(Const.DEBUG, "KTP Picture Path: " + mKtppicture);
        } else if (code == PROFILE_PICTURE_REQUEST_CODE) {
            mPicture = Uri.fromFile(mypath).toString();

            if (Const.DEBUGGING)
                Log.d(Const.DEBUG, "Profile Picture Path: " + mPicture);
        }


        Crypto crypto = new Crypto(
                  new SharedPrefsBackedKeyChain(this),
                  new SystemNativeCryptoLibrary());


        if (!crypto.isAvailable()) {
              return;
            }

        OutputStream fileStream = new BufferedOutputStream(
                  new FileOutputStream(mypath));

        OutputStream outputStream = crypto.getCipherOutputStream(
                  fileStream, new Entity("Password"));

        ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
        objectOutputStream.write(bitmapToBytes(photo));

        objectOutputStream.close(); //Line with exception
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

位图转字节():

private byte[] bitmapToBytes(Bitmap photo) {

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();

    return byteArray;
}

解码文件():

private Bitmap decodeFile(String filename) {

    Crypto crypto = new Crypto(
              new SharedPrefsBackedKeyChain(this),
              new SystemNativeCryptoLibrary());

    ContextWrapper cw = new ContextWrapper(getApplicationContext());
    File directory = cw.getDir(DIRECTORY, Context.MODE_PRIVATE);
    File file = new File(directory, filename);

    try{
        FileInputStream fileStream = new FileInputStream(file);
        InputStream inputStream = crypto.getCipherInputStream(
                  fileStream,
                  new Entity("Password"));
        ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
        Bitmap bitmap = bytesToBitmap((byte[])objectInputStream.readObject());

        return bitmap;
    }catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

字节转位图():

private Bitmap bytesToBitmap(byte[] bytes) {

    Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    return bitmap;
}

当我尝试保存图像时,我收到 UnsupportedOperationExceptionobjectOutputStream.close();在保存文件()中

Logcat 跟踪:

10-01 16:55:34.529: W/System.err(31291): java.lang.UnsupportedOperationException
10-01 16:55:34.529: W/System.err(31291):    at com.facebook.crypto.streams.NativeGCMCipherOutputStream.write(NativeGCMCipherOutputStream.java:93)
10-01 16:55:34.529: W/System.err(31291):    at java.io.DataOutputStream.writeByte(DataOutputStream.java:144)
10-01 16:55:34.529: W/System.err(31291):    at java.io.ObjectOutputStream.drain(ObjectOutputStream.java:394)
10-01 16:55:34.529: W/System.err(31291):    at java.io.ObjectOutputStream.flush(ObjectOutputStream.java:461)
10-01 16:55:34.529: W/System.err(31291):    at java.io.ObjectOutputStream.close(ObjectOutputStream.java:337)
10-01 16:55:34.529: W/System.err(31291):    at com.xx.xxx.RegistrationActivity.saveFile(RegistrationActivity.java:761)
10-01 16:55:34.529: W/System.err(31291):    at com.xx.xxx.RegistrationActivity.onActivityResult(RegistrationActivity.java:639)
10-01 16:55:34.529: W/System.err(31291):    at android.app.Activity.dispatchActivityResult(Activity.java:5423)
10-01 16:55:34.529: W/System.err(31291):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3347)
10-01 16:55:34.529: W/System.err(31291):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3394)
10-01 16:55:34.529: W/System.err(31291):    at android.app.ActivityThread.access$1300(ActivityThread.java:135)
10-01 16:55:34.529: W/System.err(31291):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
10-01 16:55:34.529: W/System.err(31291):    at android.os.Handler.dispatchMessage(Handler.java:102)
10-01 16:55:34.529: W/System.err(31291):    at android.os.Looper.loop(Looper.java:136)
10-01 16:55:34.529: W/System.err(31291):    at android.app.ActivityThread.main(ActivityThread.java:5001)
10-01 16:55:34.529: W/System.err(31291):    at java.lang.reflect.Method.invokeNative(Native Method)
10-01 16:55:34.529: W/System.err(31291):    at java.lang.reflect.Method.invoke(Method.java:515)
10-01 16:55:34.529: W/System.err(31291):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-01 16:55:34.529: W/System.err(31291):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-01 16:55:34.529: W/System.err(31291):    at dalvik.system.NativeStart.main(Native Method)
10-01 16:55:34.529: W/System.err(31291): java.io.IOException: Unexpected crypto version -1
10-01 16:55:34.529: W/System.err(31291):    at com.facebook.crypto.util.Assertions.checkArgumentForIO(Assertions.java:29)
10-01 16:55:34.539: W/System.err(31291):    at com.facebook.crypto.CipherHelper.getCipherInputStream(CipherHelper.java:52)
10-01 16:55:34.539: W/System.err(31291):    at com.facebook.crypto.Crypto.getCipherInputStream(Crypto.java:83)
10-01 16:55:34.539: W/System.err(31291):    at com.xx.xxx.RegistrationActivity.decodeFile(RegistrationActivity.java:821)
10-01 16:55:34.539: W/System.err(31291):    at com.xx.xxx.RegistrationActivity.onActivityResult(RegistrationActivity.java:640)
10-01 16:55:34.539: W/System.err(31291):    at android.app.Activity.dispatchActivityResult(Activity.java:5423)
10-01 16:55:34.539: W/System.err(31291):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3347)
10-01 16:55:34.539: W/System.err(31291):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3394)
10-01 16:55:34.539: W/System.err(31291):    at android.app.ActivityThread.access$1300(ActivityThread.java:135)
10-01 16:55:34.539: W/System.err(31291):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
10-01 16:55:34.539: W/System.err(31291):    at android.os.Handler.dispatchMessage(Handler.java:102)
10-01 16:55:34.539: W/System.err(31291):    at android.os.Looper.loop(Looper.java:136)
10-01 16:55:34.539: W/System.err(31291):    at android.app.ActivityThread.main(ActivityThread.java:5001)
10-01 16:55:34.539: W/System.err(31291):    at java.lang.reflect.Method.invokeNative(Native Method)
10-01 16:55:34.539: W/System.err(31291):    at java.lang.reflect.Method.invoke(Method.java:515)
10-01 16:55:34.539: W/System.err(31291):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-01 16:55:34.549: W/System.err(31291):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-01 16:55:34.549: W/System.err(31291):    at dalvik.system.NativeStart.main(Native Method)
10-01 16:55:34.549: D/BAT(31291): onResume called

感谢您的帮助...


这是我解决问题的方法..加密和解密现在工作正常。

// Encrypts the image and saves to directory

public void encodeAndSaveFile(Bitmap photo, int code) {

    try {
        ContextWrapper cw = new ContextWrapper(getApplicationContext());
        File directory = cw.getDir(DIRECTORY, Context.MODE_PRIVATE);
        File mypath = new File(directory, ENCRYPTEDFILENAME);

        Crypto crypto = new Crypto(new SharedPrefsBackedKeyChain(this),
                new SystemNativeCryptoLibrary());

        if (!crypto.isAvailable()) {
            return;
        }

        OutputStream fileStream = new BufferedOutputStream(
                new FileOutputStream(mypath));
        OutputStream outputStream = crypto.getCipherOutputStream(
                fileStream, new Entity("Password"));
        outputStream.write(bitmapToBytes(photo));
        outputStream.close();
    } catch (UnsupportedOperationException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

// convert Bitmap to bytes
private byte[] bitmapToBytes(Bitmap photo) {

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();
    return byteArray;
}

// convert bytes to Bitmap
private Bitmap bytesToBitmap(byte[] bytes) {

    Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

    return bitmap;
}

// decode encrypted file and returns Bitmap
private Bitmap decodeFile(String filename) {

    Crypto crypto = new Crypto(new SharedPrefsBackedKeyChain(this),
            new SystemNativeCryptoLibrary());

    ContextWrapper cw = new ContextWrapper(getApplicationContext());
    File directory = cw.getDir(DIRECTORY, Context.MODE_PRIVATE);
    File file = new File(directory, filename);

    try {
        FileInputStream fileStream = new FileInputStream(file);
        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();

        Bitmap bitmap = bytesToBitmap(out.toByteArray());
        return bitmap;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

这是我在从相机返回后在 onActivityResult() 中调用encodeAndSaveFile() 和decodeFile() 的方法。

encodeAndSaveFile((Bitmap) data.getExtras().get("data"),
                        requestCode);
Bitmap decryptedImage = decodeFile(ENCRYPTEDFILENAME);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Facebook Conceal - 图像加密和解密 的相关文章

随机推荐

  • Django + React:如何连接它们进行部署?

    我正在运行一个应用程序Django DRF CELERY REDIS ReactJs REDUX JWT 并且我很难连接backend与frontend用于部署 我用过create react app生成 React 代码 和npm run
  • 在 PL/SQL 函数的 EXECUTE IMMEDIATE 中使用 UDT 变量

    我正在使用 Oracle 11g 在 PL SQL 上构建一个函数 我试图在 EXECUTE IMMEDIATE 语句中使用表变量 但它不起作用 如您所见 ERROR at line 1 ORA 00904 CENTER OBJECTS i
  • Java TreeMap 重复键

    我想我可能发现了 Java 中的一个错误 我有一个 TreeMap 其中使用自定义比较器 但是 当我将 键 值 放在已经存在的键上时 它似乎不会覆盖该键 从而创建重复的键 我想我已经验证了这一点 因为我尝试过 System out prin
  • 根据类型参数选择了错误的特征

    我有一个二元特征Resolve pub trait Resolve
  • 如何设置每天早上 8:00 响铃

    我正在尝试设置每天早上 8 00 触发的闹钟 我知道如何创建闹钟 但如何将其设置为每天上午 8 00 启动 am setRepeating 您可以使用日历并将其设置为您想要的适当时间 那么你会做cal getTimeInMillis 并将其
  • 隐藏透明标题下的滚动内容

    好吧 我已经疯狂地寻找答案了 这很简单 我知道 我的页面顶部有一个固定的透明标题 当我滚动时 我希望正文内容在其下方滚动 但不能通过标题 div 看到 我看过类似的帖子 但对我的案子没有任何帮助 感谢任何可能有帮助的提示或线索 谢谢 下面的
  • 安装face_recognition时出错 错误文本:收集face_recognition

    我想在 python 上启动人脸识别项目 我安装了 bython 并安装了 pipelinev 此后 当我安装 pipenv installface recognition shell 时 在安装后会出现错误 黑色先生 PGk31eo C
  • PySide2 Qthread 崩溃

    由于 Qtcore Signal 我想使用 PySide2 Qtcore Qthread 但最终出现此错误 进程已完成 退出代码为 1073740791 from PySide2 QtCore import QThread class Th
  • eval SyntaxError:python 中的语法无效[重复]

    这个问题在这里已经有答案了 我想分配 x0 123 x1 123 x2 123 x3 123 x4 123 x5 123 x6 123 x7 123 x8 123 x9 123 我写代码是为了表达我可以得到一个字符串的输出123当输入x1
  • ViewModel和Model之间的MVVM设计模式关系[重复]

    这个问题在这里已经有答案了 根据MSDN上的图片 似乎所有数据和业务逻辑都应该位于模型内部 其中视图模型应该具有模型的一组重复属性以用于显示目的 View 应该绑定到 ViewModel 内的重复属性 而不是直接绑定到 Models 内的属
  • 如何将文件从一个git分支复制到另一个git分支并另存为不同的文件?

    我想将文件从一个 git 分支复制到另一个分支 但将其另存为不同的文件名 我知道我可以这样做以使用相同的名称复制它 有没有办法在结帐时重命名 git checkout otherbranch myfile txt git show othe
  • 使用以逗号分隔的类似数组的字符串执行存储过程[重复]

    这个问题在这里已经有答案了 可能的重复 使用逗号分隔参数帮助进行 SQL 搜索查询 我想编写一个对表执行选择的存储过程 并且需要一个类型的输入变量varchar max 我想发送一堆由 分隔的值 作为输入参数 例如 Jack Jane Jo
  • 将 EC2 安全组限制为 Elastic Beanstalk 实例

    我将 MongoDB 部署在 EC2 实例中 良好且稳定 我 希望 很快就会使用 Docker 启动我的 Elastic Beanstalk 负载平衡 Web 应用程序 但是 我觉得我的数据库对 dockerize 或 bestalk iz
  • 将 WPF 控件的大小调整为精确的百分比

    在 WPF 中 我想将控件宽度设置为父控件的 97 ActualWidth财产 我怎样才能做到这一点 您可以使用网格面板 例如
  • PHP 简单 HTML DOM 解析器

    我刚开始使用PHP 简单 HTML DOM 解析器 现在我试图提取所有被 a 包围的元素 b 标签包括 b 来自现有的 HTML 文档 这适用于 foreach html gt find b as q echo q 我怎样才能实现只显示被包
  • UWP 从 TreeView 控件获取选定节点

    如何从 a 中获取当前突出显示 选定的节点TreeView控制 根据文档here应该可以迭代控件的SelectedNodes财产但它总是空的 EDIT 事实证明这是 XAML 的一个实际错误 已跟踪here 在修复之前 接受的答案可以作为解
  • 如何更改传递给函数的结构体的值

    嗨 朋友们 我正在练习结构 我有这两个函数 其中一个返回结构 然后将其复制到 main 中的本地结构 我的第二个函数通过输入不同的实体来更改这些本地结构成员 现在我在调用每个函数后打印了结果 令我惊讶的是我注意到两个函数之后的打印结果是相同
  • excel:如何识别包含从关键字列表中获取的文本关键字的行

    我有一列 称为 A 数据 其中每个单元格包含一长串单词 例如 COLUMN A HORNBACH BAUMARKT ETOY ETOY ALIGRO CHAVANNES PR DIPL ING FUST AG ETO ETOY AGIP S
  • 从绿色到红色取决于百分比

    我有一个民意调查系统 我希望这次民意调查的答案是彩色的 例如 如果为 10 则为红色 如果为 40 则为黄色 如果为 80 则为绿色 所以我希望我的 javascript 代码使用 rgb 颜色根据给定的百分比生成颜色 function h
  • Facebook Conceal - 图像加密和解密

    我正在尝试使用 Facebook Conceal Library 加密和解密图像 这是我第一次使用它 因此如果它微不足道 请耐心等待 我查看了有关 SO 的其他问题 以找出异常的原因 但我无法使其正常工作 这是我到目前为止所做的 集成 我使