I have hacked together a small program from bits on the internet I've been learning C# with. It is supposed to take a secure string located in a file and decrypt and display the output in a console. This does not happen however there are no errors shown in VS and the build and debug sessions finish without any errors. I am just looking for a console to top up to display the string... can anyone tell me what I am doing wrong below?
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
namespace simple_vsphere_connect
{
class Program
{
static void Main(string[] args)
{
FileInfo file = new FileInfo("C:\\Users\\dude\\Desktop\\secret.txt");
if (!file.Exists)
{
SecureString password;
using (FileStream fs = file.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
{
byte[] bytes = new byte[fs.Length];
int numBytesToRead = (int)fs.Length;
int numBytesRead = 0;
while (numBytesToRead > 0)
{
int n = fs.Read(bytes, numBytesRead, numBytesToRead);
if (n == 0)
break;
numBytesRead += n;
numBytesToRead -= n;
}
password = Encoding.Unicode.GetString(bytes).DecryptString();
}
Console.WriteLine(password.ToUnsecureString());
Console.ReadLine();
}
}
}
static class Extensions
{
public static readonly byte[] entropy = Encoding.Unicode.GetBytes("Salt Is Not A Password");
public static string EncryptString(this SecureString source)
{
if (source == null)
return null;
var encrypted = ProtectedData.Protect(Encoding.Unicode.GetBytes(source.ToUnsecureString()), entropy, DataProtectionScope.CurrentUser);
return Convert.ToBase64String(encrypted);
}
public static SecureString DecryptString(this string source)
{
if (source == null)
return null;
SecureString result = new SecureString();
try
{
var decrypted = ProtectedData.Unprotect(Convert.FromBase64String(source), entropy, DataProtectionScope.CurrentUser);
result = Encoding.Unicode.GetString(decrypted).ToSecureString();
}
catch
{
result = new SecureString();
}
return result;
}
public static SecureString ToSecureString(this IEnumerable source)
{
if (source == null)
return null;
SecureString result = new SecureString();
foreach (char value in source)
result.AppendChar(value);
result.MakeReadOnly();
return result;
}
public static string ToUnsecureString(this SecureString source)
{
if (source == null)
return null;
var ptr = Marshal.SecureStringToBSTR(source);
try
{
return Marshal.PtrToStringBSTR(ptr);
}
finally
{
Marshal.ZeroFreeBSTR(ptr);
}
}
}
}
Saroj Kumar SahuPosted Jun 16, 2015, 6:14 AM
Saroj Kumar SahuPosted Jun 16, 2015, 3:50 AM
Claude McAdamsPosted Jun 15, 2015, 10:02 AM
Saroj Kumar SahuPosted Jun 15, 2015, 9:24 AM
Claude McAdamsPosted Jun 15, 2015, 9:08 AM
Saroj Kumar SahuPosted Jun 15, 2015, 8:42 AM
Claude McAdamsPosted Jun 15, 2015, 8:05 AM
Saroj Kumar SahuPosted Jun 15, 2015, 1:35 AM
Claude McAdamsPosted Jun 14, 2015, 10:00 PM
01000000d08c9ddf0115d1118c7a00c04fc297eb0100000052ded6c2db80e748933432e19b9de8b10000
000002000000000003660000c00000001000000016dc35885d76d07bab289eb9927cfc1e000000000480
0000a0000000100000003106cde553f45b08d13d89d11336170b280000005cc865c1ee1b57e84ed3d1a2
d3f2d0ec0f189b532e61c18d1f31444d6f119a1e8368477fd2d81f54140000000cb0262e58b08ae14f37
22c14c69684841b6b21c
Claude McAdamsPosted Jun 12, 2015, 11:28 AM
01000000d08c9ddf0115d1118c7a00c04fc297eb0100000052ded6c2db80e748933432e19b9de8b10000
000002000000000003660000c00000001000000016dc35885d76d07bab289eb9927cfc1e000000000480
0000a0000000100000003106cde553f45b08d13d89d11336170b280000005cc865c1ee1b57e84ed3d1a2
d3f2d0ec0f189b532e61c18d1f31444d6f119a1e8368477fd2d81f54140000000cb0262e58b08ae14f37
22c14c69684841b6b21c
Saroj Kumar SahuPosted Jun 12, 2015, 8:18 AM