/// <summary> /// decrypts an image /// </summary> /// <param name="image">the image that will be dencrypted</param> /// <param name="roIs">the Region of Interests</param> /// <param name="password">the password used for decryption</param> /// <param name="cryptoFlags">determines which encryption mode will be applied</param> /// <param name="errorCode">if an error occurs this value will be set is not 0</param> /// <returns>returns an decrypted image</returns> public static Image DecryptJPG(Image image, IList <RoI> roIs, string password, CryptoFlags cryptoFlags, out EnumErrorCode errorCode) { // create JpegOperation from business logic IBusinessLogic businessLogic = BLFactory.CreateBusinessLogic(); IJpegOperation jpegOperation = businessLogic.CreateJpegOperationBL(); // convert Image to byte array byte[] in_jpegToDecrypt = jpegOperation.ConvertImageToByteArray(image); Int32 in_size = in_jpegToDecrypt.Length; // convert RoIs to the correct format int counter = 0; Int32[] in_roiArray = new Int32[roIs.Count() * 4]; foreach (RoI roi in roIs) { in_roiArray[(counter * 4) + 0] = roIs[counter].X; in_roiArray[(counter * 4) + 1] = roIs[counter].Y; in_roiArray[(counter * 4) + 2] = roIs[counter].Width; in_roiArray[(counter * 4) + 3] = roIs[counter].Height; counter++; } Int32 in_roiArraySize = roIs.Count() * 4; // create MD5 hash of the password MD5 md5 = new MD5CryptoServiceProvider(); byte[] textToHash = Encoding.Default.GetBytes(password); byte[] in_decryptionKey = md5.ComputeHash(textToHash); Int32 in_decryptionKeySize = in_decryptionKey.Length; // convert crypto flags byte in_cryptoFlags = cryptoFlags.ToByte(); // get memory for the decrypted image byte[] out_jpegDecrypted = new byte[in_size * 2]; byte[] out_statistics = new byte[in_size]; IntPtr out_jpegDecryptedSize = new IntPtr(); int[] ia2 = new int[1]; GCHandle gch1 = GCHandle.Alloc(ia2); out_jpegDecryptedSize = Marshal.UnsafeAddrOfPinnedArrayElement(ia2, 0); // create a pointer for the error code (will be set from C-code) int[] ia = new int[1]; IntPtr out_errorCode = new IntPtr(); GCHandle gch = GCHandle.Alloc(ia); out_errorCode = Marshal.UnsafeAddrOfPinnedArrayElement(ia, 0); // try to decrypt the image try { decJpeg( in_jpegToDecrypt, in_size, in_roiArray, in_roiArraySize, in_decryptionKey, in_decryptionKeySize, in_cryptoFlags, out_jpegDecrypted, ref out_jpegDecryptedSize, ref out_errorCode ); // get the error code errorCode = ConvertIntToErrorCode(out_errorCode.ToInt32()); // truncate byte array out_jpegDecrypted = out_jpegDecrypted.Take(out_jpegDecryptedSize.ToInt32()).ToArray(); } catch { throw new EncryptionException("Error while decrypting!"); } return(jpegOperation.ConvertByteArrayToImage(out_jpegDecrypted)); }