Cordova加密数据目录文件

时间:2017-03-09 16:16:57

标签: javascript angularjs cordova ionic-framework

我使用cordova相机拍照或从Gallery中选择它,将图像移动到data-directory,在sqlite上保存路径然后显示它。 我一直在尝试加密数据目录中的图像,我正在使用https://github.com/brix/crypto-js,我可以加密图像文件名,但是当我尝试将加密文件移动到数据目录中时,它似乎没有工作。

任何帮助将不胜感激

         $scope.images = [];

$cordovaCamera.getPicture(options).then(function(imagePath) {

        var currentFile = imagePath.replace(/^.*[\\\/]/, '');

        var ciphertext = CryptoJS.AES.encrypt(currentFile, '123');
        console.log(ciphertext);
        encryptedImage = ciphertext.toString();
        if ($cordovaDevice.getPlatform() == 'Android' && sourceType === Camera.PictureSourceType.PHOTOLIBRARY) {
            console.log("FROM Gallery");   
            window.FilePath.resolveNativePath(imagePath, function(entry) { // request the native path for the image
                window.resolveLocalFileSystemURL(entry, success, fail); //request file system to copy or move image 
                function fail(e) {
                    console.error('Error: ', e);
                }

                function success(fileEntry) {
                    var namePath = fileEntry.nativeURL.substr(0, fileEntry.nativeURL.lastIndexOf('/') + 1);
                    $cordovaFile.moveFile(namePath, fileEntry.name, cordova.file.dataDirectory, currentFile).then(function(success) {
                        //  $scope.image = newFileName;
                        var imgPath = cordova.file.dataDirectory + currentFile;

                        $scope.add(imgPath);

                          
                        $scope.images.push({
                            image: imgPath

                                          
                        });
                        console.log("ADDED TO ARRAY");   


                    }, function(error) {
                        $scope.showAlert('Error', error.exception);
                    });

                };
            });
        } else {
            console.log("picture taken from camera");   
            var namePath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);



            // Move the file to permanent storage
            $cordovaFile.moveFile(namePath, encryptedImage, cordova.file.dataDirectory, encryptedImage).then(


                function(success) {


                    var bytes = CryptoJS.AES.decrypt(ciphertext.toString(), '123');
                    var decryptedImage = bytes.toString(CryptoJS.enc.Utf8);

                    console.log('the decrypted photo   ' + decryptedImage );

                    var imgPath = cordova.file.dataDirectory + decryptedImage ;




                      
                    $scope.images.push({
                        image: imgPath

                                      
                    });

                },

                function(error) {

                    $scope.showAlert("error occured: " + error.message);

                });

        }




    },
    function(err) {
        // Not always an error, maybe cancel was pressed...
    })
};

0 个答案:

没有答案