using System; using System.Text; using Microsoft.VisualBasic; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; class DecodePassword { /* Prototype for a safe password generation process for e.g. local Windows administrator accounts. Created by Alexandre Herzog under a CC-BY-SA licence (Creative Commons Attribution + ShareAlike) This work relies on two part: 1. File SafePasswordGenerator.cs which generates a safe password and encrypts it using the public key of a given certificate (see below) 2. This file which allows the decryption of the encrypted string Compile with c:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe DecodePassword.cs /reference:Microsoft.VisualBasic.dll */ private static bool useOAEP = true; public static void Main(string[] args) { Encoding asciiEncoding = Encoding.ASCII; // using VB's Interaction, as we all miss these nice looking GUI components! String certPassword = Interaction.InputBox("Enter the password of your private certificate:", "DecryptPassword", "", -1, -1); X509Certificate2 cert = new X509Certificate2("private_cert.pfx", certPassword); // No Console.ReadLine as the copy-paste tends to be truncated... String strToDecrypt = Interaction.InputBox("Enter the string to decrypt:", "DecryptPassword", "", -1, -1); RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)cert.PrivateKey; byte[] decrypted = rsa.Decrypt(Convert.FromBase64String(strToDecrypt), useOAEP); Console.WriteLine("Decrypted string is below:\n{0}", asciiEncoding.GetString(decrypted)); Console.ReadLine(); } } /* Random string 'Password for machine '[...]': 34-PNxSstfSzZBAq' gets encoded to ' BT[...]lylhw==' ======================== Decrypted string is below: Password for machine '[...]': 34-PNxSstfSzZBAq c:\>DecodePassword.exe Decrypted string is below: Password for machine '[...]': 34-PNxSstfSzZBAq */