رمز نگاری ایمیل ، در این نمونه سورس که در محیط کنسول نوشته شده درمواقع عملیات های رمزنگاری در ارسال ایمیل را برسی کرده است ، این پروژه بیشتر در پروژه تحت وب میتواند کاربرد داشته باشد برای مثال یک فایل متنی که حاوی اطلاعات ایمیل است را به پروژه داده و میتوانید با الگوریتم های نوشته در نمونه سورس آن را به حالت Encoding میبرد.امید وارم استفاده کنید.سورس کامل نیز در ادامه براتون قرار دادم.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.RegularExpressions; namespace Program { public class EmailEncoding { private static Dictionary<string, string> m_keyValue = new Dictionary<string, string>(); static void Main(string[] args) { try { string INPUT_FILE = @"input.txt"; string OUTPUT_FILE = @"output.txt"; if (!File.Exists(INPUT_FILE)) { File.Create(INPUT_FILE).Close(); return; } if (!File.Exists(OUTPUT_FILE)) { File.Create(OUTPUT_FILE).Close(); } StreamReader streamReader = new StreamReader(INPUT_FILE); using (streamReader) { using (StreamWriter streamWriter = new StreamWriter(OUTPUT_FILE, false)) { string line; while ((line = streamReader.ReadLine()) != null) { string output = string.Empty; if (ValidateInput(line, out output)) { streamWriter.WriteLine(output); } else { streamWriter.WriteLine("ERROR"); } } } } streamReader.Close(); streamReader.Dispose(); } catch (Exception) { } } public static bool ValidateInput(string input, out string modifiedValue) { modifiedValue = input; bool result = EmailValidate(input); if (!result) { var match = ValidateKey(input); if (match != null && match.Success) { modifiedValue = UpdateKeyValue(match.Value.ToString(), modifiedValue); while (true) { Match nextMatch = match.NextMatch(); if (nextMatch == null || !nextMatch.Success) break; modifiedValue = UpdateKeyValue(nextMatch.Value.ToString(), modifiedValue); match = nextMatch; } result = true; } var keyMatch = ValidateReplaceKey(input); //if (keyMatch.Success && match.Success) // return false; if (keyMatch != null && keyMatch.Success) { modifiedValue = ReplaceKeyToValue(keyMatch.Value.ToString(), modifiedValue); while (true) { Match nextMatch = keyMatch.NextMatch(); if (nextMatch == null || !nextMatch.Success) break; modifiedValue = ReplaceKeyToValue(nextMatch.Value.ToString(), modifiedValue); keyMatch = nextMatch; } result = true; } // After success, check whether modfied string is valid email id. if (result && EmailValidate(modifiedValue)) return result; else result = false; } else return true; return result; } private static string UpdateKeyValue(string matchValue, string originalItem) { string modifiedValue = originalItem; string captureValue = matchValue.Replace("{", "").Replace("}", ""); string[] values = captureValue.Split(' '); if (values.Length > 1) { if (m_keyValue.ContainsKey(values[0])) m_keyValue[values[0]] = values[1]; else m_keyValue.Add(values[0], values[1]); } modifiedValue = modifiedValue.Replace(matchValue, values[1]); return modifiedValue; } private static string ReplaceKeyToValue(string matchValue, string originalItem) { string modifiedValue = originalItem; string captureKey = matchValue.Replace("{", "").Replace("}", ""); if (!m_keyValue.ContainsKey(captureKey)) m_keyValue.Add(captureKey, string.Empty); modifiedValue = modifiedValue.Replace(matchValue, m_keyValue[captureKey]); return modifiedValue; } private static bool EmailValidate(string line) { string pattern = @"^[a-z][a-z|0-9|]*?@[a-z][a-z|0-9|]*\.([a-z][a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$"; Match match = Regex.Match(line.Trim(), pattern, RegexOptions.IgnoreCase); if (match.Success) return true; return false; } private static Match ValidateKey(string line) { string pattern = @"{([a-z|0-9|]+ [a-z|0-9|.|]+)}"; Match match = Regex.Match(line.Trim(), pattern, RegexOptions.IgnoreCase); if (match.Success) return match; return null; } public static Match ValidateReplaceKey(string line) { string pattern = @"{([a-z|0-9|]+)}"; Match match = Regex.Match(line.Trim(), pattern, RegexOptions.IgnoreCase); if (match.Success) return match; return null; } } } |