public static void Consume(this SecureString ss, Action <byte[]> method) { IntPtr bstr = IntPtr.Zero; byte[] workArray = null; try { bstr = Marshal.SecureStringToBSTR(ss); unsafe { byte *bstrBytes = (byte *)bstr; workArray = new byte[ss.Length]; for (int i = 0; i < workArray.Length; i++) { workArray[i] = *bstrBytes++; bstrBytes++; } } method(workArray); } finally { ss.Dispose(); var rng = new RNGCryptoServiceProvider(); if (workArray != null) { rng.ClearBytes(workArray, 0, workArray.Length); } if (bstr != IntPtr.Zero) { Marshal.ZeroFreeBSTR(bstr); } } }