/// <summary> /// Decodes and decrypts Base64-encoded encrypted data. /// </summary> /// <param name="data">The data to decode and decrypt.</param> /// <param name="result">The decoded and decrypted data.</param> /// <returns>The status of the function's execution.</returns> private static SecStatus Decrypt(string data, ref string result) { var status = SecStatus.Success; var decodedItem = new SecItem { Data = IntPtr.Zero, Length = 0 }; IntPtr decodedObject = IntPtr.Zero; result = string.Empty; try { decodedObject = NSSBase64_DecodeBuffer(IntPtr.Zero, IntPtr.Zero, data, data.Length); if (decodedObject == IntPtr.Zero) { status = SecStatus.Failure; } else { status = PK11SDR_Decrypt(decodedObject, ref decodedItem, IntPtr.Zero); if (status != SecStatus.Success) { throw new NsprException("Failed to decrypt data."); } try { result = Marshal.PtrToStringAnsi(decodedItem.Data, decodedItem.Length); } finally { SECITEM_FreeItem(ref decodedItem, false); } } } catch (Exception ex) { status = SecStatus.Failure; Logger.Error(ex, "Decryption failed."); } finally { if (decodedObject != IntPtr.Zero) { SECITEM_FreeItem(decodedObject, true); } if (decodedItem.Data != IntPtr.Zero) { Marshal.FreeHGlobal(decodedItem.Data); } } return(status); }
/// <summary> /// Decodes Base64-encoded data. /// </summary> /// <param name="data">The data to decode.</param> /// <param name="result">The decoded data.</param> /// <returns>The status of the function's execution.</returns> private static SecStatus Decode(string data, ref string result) { var status = SecStatus.Success; var decodedItem = new SecItem(); IntPtr decodedObject = IntPtr.Zero; result = string.Empty; try { decodedObject = NSSBase64_DecodeBuffer(IntPtr.Zero, IntPtr.Zero, data, data.Length); if (decodedObject == IntPtr.Zero) { status = SecStatus.Failure; } else { try { decodedItem = (SecItem)Marshal.PtrToStructure(decodedObject, typeof(SecItem)); result = Marshal.PtrToStringAnsi(decodedItem.Data, decodedItem.Length); } finally { SECITEM_FreeItem(decodedObject, true); } } } catch (Exception ex) { status = SecStatus.Failure; Logger.Error(ex, "Decoding failed."); } finally { if (decodedObject != IntPtr.Zero) { SECITEM_FreeItem(decodedObject, true); } if (decodedItem.Data != IntPtr.Zero) { Marshal.FreeHGlobal(decodedItem.Data); } } return(status); }