/// <summary> /// 使用指定算法解密,通过回调返回解密后的数据. /// </summary> /// <param name="options"></param> /// command.arguments: /// - 1 sKey 密钥 /// - 2 sourceData 需要解密的数据 /// - 3 jsOptions 需要解密的数据可选参数 /// - 1 CryptAlgorithm 加密算法,默认des /// - 2 EncodeDataType 数据编码格式,默认base64 /// - 3 kKeyForEncodeKeyType 密钥编码格式,默认16进制 public void decrypt(string options) { string sKey = string.Empty; string sourceDataStr = string.Empty; string[] args = JSON.JsonHelper.Deserialize<string[]>(options); sKey = args[0]; sourceDataStr = args[1]; SecurityOptions option = new SecurityOptions(); if (args.Length > 2 && null != args[2]) { option = JSON.JsonHelper.Deserialize<SecurityOptions>(args[2]); } if (null == option) { option = new SecurityOptions(); } //check if (string.IsNullOrEmpty(sKey) || sKey.Length < 8 || string.IsNullOrEmpty(sourceDataStr)) { XLog.WriteError("Input data invalid!"); DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR)); return; } try { var keyData = StringToData(sKey, option.EncodeKeyType); keyData = MakeKey(keyData, option.CryptAlgorithm); var sourceData = StringToData(sourceDataStr, option.EncodeDataType); byte[] result = Decrypt(keyData, sourceData, option.CryptAlgorithm); DispatchCommandResult(new PluginResult(PluginResult.Status.OK, Encoding.UTF8.GetString(result, 0, result.Length))); } catch (Exception ex) { if (ex is FormatException || ex is ArgumentNullException || ex is ArgumentException || ex is ArgumentOutOfRangeException || ex is NotSupportedException) { XLog.WriteError("decrypt data error with Exception : " + ex.Message); DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR)); } } }
private void DoFileCrypt(bool encrypt, string options) { string sKey = string.Empty; string sourceFilePath = string.Empty; string targetFilePath = string.Empty; string[] args = JSON.JsonHelper.Deserialize<string[]>(options); sKey = args[0]; sourceFilePath = args[1]; targetFilePath = args[2]; SecurityOptions option = new SecurityOptions(); if (args.Length > 3 && null != args[3]) { option = JSON.JsonHelper.Deserialize<SecurityOptions>(args[3]); } if (null == option) { option = new SecurityOptions(); } //check if (string.IsNullOrEmpty(sKey) || sKey.Length < 8) { XLog.WriteError("Input data invalid!"); DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR)); return; } string appWorkSpace = this.app.GetWorkSpace(); sourceFilePath = XUtils.ResolvePath(appWorkSpace, sourceFilePath); string targetPath = targetFilePath; targetFilePath = XUtils.ResolvePath(appWorkSpace, targetFilePath); //path_error if ( string.IsNullOrEmpty(sourceFilePath) || string.IsNullOrEmpty(targetFilePath) || appWorkSpace.Equals(sourceFilePath) || appWorkSpace.Equals(targetFilePath)) { XLog.WriteError("Input data invalid path error!"); DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, SecurityError.PATH_ERR)); return; } //资源文件不存在 using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication()) { if (!isoFile.FileExists(sourceFilePath)) { XLog.WriteError(sourceFilePath + ": not_found_error!"); DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, SecurityError.FILE_NOT_FOUND_ERR)); return; } } try { var keyData = StringToData(sKey, option.EncodeKeyType); keyData = MakeKey(keyData, option.CryptAlgorithm); using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication()) { using (Stream sourceStream = isoFile.OpenFile(sourceFilePath, FileMode.Open, FileAccess.Read)) { using (FileStream fileStream = new IsolatedStorageFileStream(targetFilePath, FileMode.OpenOrCreate, isoFile)) { using (BinaryWriter writer = new BinaryWriter(fileStream)) { int streamLength = (int)sourceStream.Length; byte[] fileData = new byte[streamLength]; sourceStream.Read(fileData, 0, streamLength); byte[] result = null; if (encrypt) { result = Encrypt(keyData, fileData, option.CryptAlgorithm); } else { result = Decrypt(keyData, fileData, option.CryptAlgorithm); } writer.Write(result); } } } } //返回加密后文件路径 DispatchCommandResult(new PluginResult(PluginResult.Status.OK, targetPath)); } catch (Exception ex) { if (ex is FormatException || ex is ArgumentNullException || ex is ArgumentException || ex is ArgumentOutOfRangeException || ex is NotSupportedException || ex is IsolatedStorageException) { string operation = encrypt ? "encrypt" : "decrypt"; XLog.WriteError("DoFileCrypt " + operation + " error with Exception : " + ex.Message); DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, SecurityError.OPERATION_ERR)); return; } throw ex; } }