4

I am working on React-Native-OpenPGP for Encryption and Decryption. I want to take an image from my folder (local image fetch)/ Image url and convert that image into Uint8Array for encryption/Decryption . I am new to react native , Not able to find a better solution . Links can also help. Need a process to give an image path and convert it into Uint8Array.

Moreover also need a solution to convert an image file to binary data for Encryption/Decryption . Is it possible as openpgp provides two ways to do that one is through String and another is through Uint8Array data ?

4

1 回答 1

0

不知何故,花了一天的时间后,我设法通过b64-to-blob将图像文件转换为BLOB。这个附加的链接帮助我这样做。我是这样做的:

第 1 步:从 'react-native-image-base64' 导入 ImgToBase64;

第 2 步:您必须安装 npm i -S base-64(用于在 atob、btoa 中进行编码和解码)

第 3 步:从 'base-64' 导入 {decode as atob, encode as btoa}

第 4 步: var b64toBlob = require('b64-to-blob'); , var baseStringSample;

第 5 步:创建一个函数将图像转换为 base64

_convertImageToBaseSixFour() { 

    ImgToBase64.getBase64String('YOUR_IMAGE_PATH') // path to your image from local storage
  .then((base64String) => {
        baseStringSample = base64String,
        })
  .catch(err => Alert.alert('Error' + err));

}

第 6 步:

// ****** CONVERT BASE64 TO BLOB ******* //

  _imageToBlob(){

    var byteCharacters = atob(baseStringSample);
    var byteNumbers = new Array(byteCharacters.length);
for (var i = 0; i < byteCharacters.length; i++) {
    byteNumbers[i] = byteCharacters.charCodeAt(i);
    byteArray = new Uint8Array(byteNumbers);
    console.log("BYTEARRAY: " + byteArray);
}

} 

第7步: 然后生成密钥,通过openpgp库中的UInt8Array方法进行加密和解密

第 8 步: 将解密后的图像转换为 base64,然后将 base64 转换为 Image,在 Imageview 中显示图像。

于 2019-03-07T12:11:58.697 回答