Last active
October 26, 2016 12:23
-
-
Save serkanince/03062467ba3dfd24e012f053d8e2a375 to your computer and use it in GitHub Desktop.
SecureString Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Security; | |
namespace Sample07_SecureString | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
SecureString secString = new SecureString(); | |
string samplePass = "ZAQ!2wsx"; | |
Console.WriteLine("Secure String Sample.."); | |
Console.WriteLine("Password : " + samplePass.ToString()); | |
if (!string.IsNullOrEmpty(samplePass)) | |
{ | |
SecureStringExtension miscObj = new SecureStringExtension(); | |
//Güvensiz değeri güvenli stringe dönüştür | |
secString = miscObj.convertToSecureString(samplePass); | |
//samplePass nesnesini temizle | |
samplePass = String.Empty; | |
//Sonuç | |
Console.WriteLine("Password converted to Secure Password : " + secString.ToString()); | |
Console.WriteLine("Cleared actual Password : " + samplePass.ToString()); | |
//Güvenli değeri decrypt yap | |
samplePass = miscObj.convertToUNSecureString(secString); | |
//Decrypt yapılmış değeri göster | |
Console.WriteLine("Copied Un Secured password from Secure Password :" + samplePass.ToString()); | |
Console.ReadKey(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
using System.Security; | |
namespace Sample07_SecureString | |
{ | |
public class SecureStringExtension | |
{ | |
public SecureString convertToSecureString(string strPass) | |
{ | |
var secureStr = new SecureString(); | |
if (strPass.Length > 0) | |
{ | |
foreach (var c in strPass.ToCharArray()) secureStr.AppendChar(c); | |
} | |
secureStr.MakeReadOnly(); | |
return secureStr; | |
} | |
public string convertToUNSecureString(SecureString secstrPassword) | |
{ | |
IntPtr unmanagedString = IntPtr.Zero; | |
try | |
{ | |
unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(secstrPassword); | |
return Marshal.PtrToStringUni(unmanagedString); | |
} | |
finally | |
{ | |
Marshal.ZeroFreeGlobalAllocUnicode(unmanagedString); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment