title: ionic cordova encrypt base64 image
body: <p>I have this app where I take picture with cordova camera  then move it into file-system , save the imgpath for the image into sqlite db and display it, I'm trying to encrypt the imgPath for the image which is saved on ccordova.file.data directory , I tried cordova safe plugin <a href="https://github.com/disusered/cordova-safe" rel="nofollow noreferrer">https://github.com/disusered/cordova-safe</a> ,
  but I keep getting "Error with cryptographic operation".</p>
  
  <p>Any help would be appreciated </p>
  
  <pre><code>var db = null;
  angular.module('starter', ['ionic', 'ngCordova']) 
      .run(function($ionicPlatform, $cordovaSQLite) {    
          $ionicPlatform.ready(function() {      
              try {        
                  db = $cordovaSQLite.openDB({          
                      name: "my.db",
                                location: 'default'        
                  });        
                  $cordovaSQLite.execute(db,"CREATE TABLE IF NOT EXISTS imageTable " + "(id integer primary key, image text)");      
              } catch (e) {        
                  alert(e);      
              } finally {       }
  
              if (window.cordova &amp;&amp; window.cordova.plugins.Keyboard) {
  
                  cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
  
  
                  cordova.plugins.Keyboard.disableScroll(true);
              }
              if (window.StatusBar) {
                  StatusBar.styleDefault();
              }
          });
      })
  
  
  
      .controller('ImageCtrl', function($scope, $rootScope, $state, $stateParams, $cordovaDevice, $cordovaFile, $ionicActionSheet, $cordovaCamera, $cordovaFile, $cordovaDevice, $ionicPopup, $cordovaActionSheet, $cordovaSQLite, $interval) {
  
  
  
  
          var imagesP = [];
  
          //$scope.images = [];
  
  
          $scope.showAlert = function(title, msg) {
              var alertPopup = $ionicPopup.alert({
                  title: title,
                  template: msg
              });
          };
  
  
          $scope.loadImage = function() {
  
              var options = {
                  title: 'Select Receipts Image ',
                  buttonLabels: ['Gallery', 'Take photo', 'File System'],
                  addCancelButtonWithLabel: 'Cancel',
                  androidEnableCancelButton: true,
              };
              $cordovaActionSheet.show(options).then(function(btnIndex) {
                  var type = null;
                  if (btnIndex === 1) {
                      type = Camera.PictureSourceType.PHOTOLIBRARY;
                  } else if (btnIndex === 2) {
                      type = Camera.PictureSourceType.CAMERA;
                  }
                  if (type !== null) {
                      $scope.selectPicture(type);
                  }
              });
          }
  
  
  
  
          // Take image with the camera or from library and store it inside the app folder
          // Image will not be saved to users Library.
          $scope.selectPicture = function(sourceType) {
  
  
              var options = {
                  quality: 75,
                  destinationType: Camera.DestinationType.FILE_URI,
                  sourceType: sourceType,
                  allowEdit: true,
                  encodingType: Camera.EncodingType.JPEG,
                  correctOrientation: true,
                  targetWidth: 800,
                  targetHeight: 800,
                  popoverOptions: CameraPopoverOptions, // for IOS and IPAD
                  saveToPhotoAlbum: false
              };
  
              $cordovaCamera.getPicture(options).then(function(imagePath) {
                      // Grab the file name of the photo in the temporary directory
                      var currentName = imagePath.replace(/^.*[\\\/]/, '');
                      //  alert(currentName);
  
                      //Create a new name for the photo to avoid duplication
                      var d = new Date(),
                          n = d.getTime(),
                          newFileName = n + ".jpg";
                      //alert(newFileName);
                      // If you are trying to load image from the gallery on Android we need special treatment!
                      if ($cordovaDevice.getPlatform() == 'Android' &amp;&amp; sourceType === Camera.PictureSourceType.PHOTOLIBRARY) {
                          window.FilePath.resolveNativePath(imagePath, function(entry) {
                              window.resolveLocalFileSystemURL(entry, success, fail);
  
                              function fail(e) {
                                  console.error('Error: ', e);
                              }
  
                              function success(fileEntry) {
                                  var namePath = fileEntry.nativeURL.substr(0, fileEntry.nativeURL.lastIndexOf('/') + 1);
                                  // Only copy because of access rights
                                  $cordovaFile.copyFile(namePath, fileEntry.name, cordova.file.dataDirectory, newFileName).then(function(success) {
                                      //  $scope.image = newFileName;
                                      var imgPath = cordova.file.dataDirectory + newFileName;
  
                                      $scope.add(imgPath);
                                 $scope.encryptFile(imgPath);
  
  
  
  
                                  }, function(error) {
                                      $scope.showAlert('Error', error.exception);
                                  });
                                  //      alert( fileEntry.nativeURL);
  
                              };
                          });
                      } else {
                          var namePath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
                          // Move the file to permanent storage
                          $cordovaFile.moveFile(namePath, currentName, cordova.file.dataDirectory, newFileName).then(function(success) {
                              // $scope.image = newFileName;
                              //$scope.images.push(newFileName);
                                      var imgPath = cordova.file.dataDirectory + newFileName;
                              $scope.add(imgPath);
  
  
  
  
                          }, function(error) {
                              $scope.showAlert('Error', error.exception);
                          });
  
                      }
                  },
                  function(err) {
                      // Not always an error, maybe cancel was pressed...
                  })
          };
  
          $scope.add = function(path) {             
                  if (imagesP != null) {          
                      $cordovaSQLite.execute(db, "INSERT INTO imageTable (images) VALUES(?)", [path] );        
                  }        
                  alert("Inserted.");      
              },
              function(e) {        
                  alert(e);      
              };
  
  $scope.encryptFile = function(file){
  
  
              var safe = cordova.plugins.disusered.safe,
                  key = 'someKeyABC';
  
  
              function success(encryptedFile) {
                console.log('Encrypted file: ' + encryptedFile);
  
  
              }
  
              function error() {
                console.log('Error with cryptographic operation');
              }
  
              safe.encrypt(file, key, success, error);
              }
  
  
  
            
          $scope.ShowAllData = function() {     
              $scope.images = [];      
              $cordovaSQLite.execute(db,"SELECT images FROM imageTable").then(function(res) {          
                  if (res.rows.length &gt; 0) {            
                      for (var i = 0; i &lt; res.rows.length; i++) {              
                          $scope.images.push({                
                              id: res.rows.item(i).id,
                              image: res.rows.item(i).images
  
                                            
                          });            
                      }          
                  }        
              },         function(error) {          
                  alert(error);        
              }      );
  
               
          } 
  
                 
  </code></pre>