Title: Base64 Encoding With Simple Salt (and decoding)
Console App-
...App.config
<appSettings>
<add key="SoapHeader" value="http://mySvc.com:7147/ws/evoCorp/Header" />
<add key="SoapLines" value="http://mySvc.com:7147/ws/evoCorp/Lines" />
<add key="SoapPosted" value="http://mySvc.com:7147/ws/evoCorp/Posted" />
<add key="domainName" value="myDomain" />
<add key="domainUser" value="myServiceUser"/>
<add key="domainHash" value="myServiceAuth"/>
<add key="domainMode" value="test"/>
</appSettings>
...Program.cs
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace ConsoleBase64x { internal class Program { static int Main(string[] args) { //"8>4D>?]aX+#+A2k" EncodeWithoutSalt = OD40RD4/XWFYKyMrQTJr //"8>4D>?]aX+#+A2k" EncodeWithSalt = MTU4PjREPj9dYVgrIytBMms= Console.WriteLine("Enter the setting name in the app.config to encrypt the setting's value. Ex: domainHash. Then press {enter}."); var sKeyName = Console.ReadLine(); if (string.IsNullOrEmpty(sKeyName)) { Console.WriteLine("The setting name in the app.config was not found or was empty. The program will now exit."); Console.Read(); return -1; } var sHashword = SettingsConfigHelper.GetAppSetting(sKeyName); Console.WriteLine("original: " + sHashword); var sHashword64encoded = SettingsConfigHelper.Base64EncodeWithSalt(sHashword); Console.WriteLine("Base64EncodeWithSalt: " + sHashword64encoded); Console.WriteLine("Highlight the value above, copy it to notepad, and Press any key to decode back to original value."); Console.Read(); var sHashwordDecoded = SettingsConfigHelper.Base64DecodeWithSalt(sHashword64encoded); Console.WriteLine("Base64DecodeWithSalt: " + sHashwordDecoded); Console.WriteLine("If this value is the same as the original value in the App.Config, you can use it in the Control Console Web.Config."); Console.WriteLine("Press any key to exit."); Console.Read(); Console.Read(); return 0; //byte[] imageBytes = SettingsConfigHelper.FromBase64Bytes(sHashword); //var fstrm = new FileStream(@"C:\winnt_copy.bmp", FileMode.CreateNew, FileAccess.Write); //BinaryWriter writer = new BinaryWriter(fstrm); //Console.Write(imageBytes); //writer.Close( ); } } }...SettingsConfigHelper.csusing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleBase64x { public static class SettingsConfigHelper { /* GET CONFIG KEY VALUE */ public static string GetAppSetting(string sKey) { if (sKey == null) { return null; } var sValue = System.Configuration.ConfigurationManager.AppSettings[sKey]; return sValue ?? string.Empty; } /* ENCODE STRING */ public static string Base64Encode(string plainText) { if (plainText == null) { return null; } var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText); return System.Convert.ToBase64String(plainTextBytes); } /* DECODE STRING */ public static string Base64Decode(string base64EncodedData) { if (base64EncodedData == null) { return null; } var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData); return System.Text.Encoding.ASCII.GetString(base64EncodedBytes); } /* ENCODE STRING with salt */ public static string Base64EncodeWithSalt(string plainText) { if (plainText == null) { return null; } var iLen = plainText.Length; var sLen = "00"; if (iLen < 10) { sLen = string.Concat("0", iLen.ToString()); } else { sLen = iLen.ToString(); } var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(sLen + plainText); return System.Convert.ToBase64String(plainTextBytes); } /* DECODE STRING with salt*/ public static string Base64DecodeWithSalt(string base64EncodedData) { if (base64EncodedData == null) { return null; } var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData); var sValue = System.Text.Encoding.ASCII.GetString(base64EncodedBytes); sValue = sValue.Substring(2); return sValue; } /* Decoding an encoded base64 string by using static Convert.FromBase64CharArray method. * This method returns a byte[] that contains the decoded elements of the string. * If you receive a file via email, such as an image file (.bmp), that has previously been converted to a string, * to convert it back into its original bitmap file, you could do something like the following: */ public static byte[] Base64DecodeString(string inputStr) { byte[] decodedByteArray = Convert.FromBase64CharArray(inputStr.ToCharArray(), 0, inputStr.Length); return (decodedByteArray); } /* Convert a Base64 encoded string into a byte[] use Convert.FromBase64String * Base64 is always ascii text. So just do Encoding.ASCII.GetString(base64arr) */ public static byte[] FromBase64Bytes(this byte[] base64Bytes) { string base64String = Encoding.UTF8.GetString(base64Bytes, 0, base64Bytes.Length); return Convert.FromBase64String(base64String); } }}
No comments:
Post a Comment