TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Claude McAdams
NA
11
1.7k
Need help getting console output in a C# program
Jun 12 2015 7:06 AM
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<char> 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);
}
}
}
}
Reply
Answers (
11
)
how to retrive data in gridview using stored procedure
change com port number to specific number