1 /* 2 * This file is part of the "GKMap". 3 * GKMap project borrowed from GMap.NET (by radioman). 4 * 5 * Copyright (C) 2009-2018 by radioman (email@radioman.lt). 6 * This program is licensed under the FLAT EARTH License. 7 */ 8 9 using System; 10 using System.ComponentModel; 11 using System.Diagnostics; 12 using System.IO; 13 using System.Reflection; 14 using System.Security.Cryptography; 15 using System.Text; 16 17 namespace GKMap 18 { 19 /// <summary> 20 /// etc functions... 21 /// </summary> 22 internal class Stuff 23 { EnumToString(Enum value)24 public static string EnumToString(Enum value) 25 { 26 FieldInfo fi = value.GetType().GetField(value.ToString()); 27 DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); 28 return (attributes.Length > 0) ? attributes[0].Description : value.ToString(); 29 } 30 31 public static readonly Random Random = new Random(); 32 LoadResourceString(string resName)33 public static string LoadResourceString(string resName) 34 { 35 Assembly assembly = typeof(Stuff).Assembly; 36 Stream resStream = assembly.GetManifestResourceStream(resName); 37 string result; 38 using (StreamReader sr = new StreamReader(resStream)) { 39 result = sr.ReadToEnd(); 40 } 41 return result; 42 } 43 LoadResourceStream(string resName)44 public static Stream LoadResourceStream(string resName) 45 { 46 Assembly assembly = typeof(Stuff).Assembly; 47 Stream resStream = assembly.GetManifestResourceStream(resName); 48 return resStream; 49 } 50 CopyStream(Stream inputStream, bool seekOriginBegin)51 public static MemoryStream CopyStream(Stream inputStream, bool seekOriginBegin) 52 { 53 const int readSize = 32 * 1024; 54 byte[] buffer = new byte[readSize]; 55 MemoryStream ms = new MemoryStream(); 56 { 57 int count; 58 while ((count = inputStream.Read(buffer, 0, readSize)) > 0) { 59 ms.Write(buffer, 0, count); 60 } 61 } 62 if (seekOriginBegin) { 63 inputStream.Seek(0, SeekOrigin.Begin); 64 } 65 ms.Seek(0, SeekOrigin.Begin); 66 return ms; 67 } 68 IsRunningOnWin7orLater()69 public static bool IsRunningOnWin7orLater() 70 { 71 OperatingSystem os = Environment.OSVersion; 72 73 if (os.Platform == PlatformID.Win32NT) { 74 Version vs = os.Version; 75 76 if (vs.Major >= 6 && vs.Minor > 0) { 77 return true; 78 } 79 } 80 81 return false; 82 } 83 EncryptString(string message, string passphrase)84 private static string EncryptString(string message, string passphrase) 85 { 86 byte[] results; 87 88 using (var hashProvider = new SHA1CryptoServiceProvider()) { 89 byte[] tdesKey = hashProvider.ComputeHash(Encoding.UTF8.GetBytes(passphrase)); 90 Array.Resize(ref tdesKey, 16); 91 92 using (TripleDESCryptoServiceProvider tdesAlgorithm = new TripleDESCryptoServiceProvider()) { 93 tdesAlgorithm.Key = tdesKey; 94 tdesAlgorithm.Mode = CipherMode.ECB; 95 tdesAlgorithm.Padding = PaddingMode.PKCS7; 96 97 byte[] dataToEncrypt = Encoding.UTF8.GetBytes(message); 98 99 // Step 5. Attempt to encrypt the string 100 try { 101 using (ICryptoTransform encryptor = tdesAlgorithm.CreateEncryptor()) { 102 results = encryptor.TransformFinalBlock(dataToEncrypt, 0, dataToEncrypt.Length); 103 } 104 } finally { 105 // Clear the TripleDes and Hashprovider services of any sensitive information 106 tdesAlgorithm.Clear(); 107 hashProvider.Clear(); 108 } 109 } 110 } 111 112 // Step 6. Return the encrypted string as a base64 encoded string 113 return Convert.ToBase64String(results); 114 } 115 DecryptString(string message, string passphrase)116 private static string DecryptString(string message, string passphrase) 117 { 118 byte[] results; 119 120 using (var hashProvider = new SHA1CryptoServiceProvider()) { 121 byte[] tdesKey = hashProvider.ComputeHash(Encoding.UTF8.GetBytes(passphrase)); 122 Array.Resize(ref tdesKey, 16); 123 124 // Step 2. Create a new TripleDESCryptoServiceProvider object 125 using (TripleDESCryptoServiceProvider tdesAlgorithm = new TripleDESCryptoServiceProvider()) { 126 // Step 3. Setup the decoder 127 tdesAlgorithm.Key = tdesKey; 128 tdesAlgorithm.Mode = CipherMode.ECB; 129 tdesAlgorithm.Padding = PaddingMode.PKCS7; 130 131 // Step 4. Convert the input string to a byte[] 132 byte[] dataToDecrypt = Convert.FromBase64String(message); 133 134 // Step 5. Attempt to decrypt the string 135 try { 136 using (ICryptoTransform decryptor = tdesAlgorithm.CreateDecryptor()) { 137 results = decryptor.TransformFinalBlock(dataToDecrypt, 0, dataToDecrypt.Length); 138 } 139 } finally { 140 // Clear the TripleDes and Hashprovider services of any sensitive information 141 tdesAlgorithm.Clear(); 142 hashProvider.Clear(); 143 } 144 } 145 } 146 147 // Step 6. Return the decrypted string in UTF8 format 148 return Encoding.UTF8.GetString(results, 0, results.Length); 149 } 150 GString(string message)151 public static string GString(string message) 152 { 153 return DecryptString(message, Manifesto); 154 } 155 156 private static readonly string Manifesto = "GMap.NET is great and Powerful, Free, cross platform, open source .NET control."; 157 GetApplicationDataFolderPath()158 public static string GetApplicationDataFolderPath() 159 { 160 bool isSystem = false; 161 try { 162 using (var identity = System.Security.Principal.WindowsIdentity.GetCurrent()) { 163 isSystem = identity.IsSystem; 164 } 165 } catch (Exception ex) { 166 Trace.WriteLine("SQLitePureImageCache, WindowsIdentity.GetCurrent: " + ex); 167 } 168 169 var specFolder = (isSystem) ? Environment.SpecialFolder.CommonApplicationData : Environment.SpecialFolder.LocalApplicationData; 170 string path = Environment.GetFolderPath(specFolder); 171 172 if (!string.IsNullOrEmpty(path)) { 173 path += Path.DirectorySeparatorChar + "GKMap" + Path.DirectorySeparatorChar; 174 } 175 176 return path; 177 } 178 } 179 } 180