/// <summary> /// BlowFish加密bytes /// </summary> /// <param name="encodeStr"></param> /// <param name="key"></param> /// <returns></returns> public static byte[] BlowFishEncryptBytes(byte[] bytes, string key) { byte[] keys = ASCIIEncoding.ASCII.GetBytes(key); BlowFishCS.BlowFish blowFish = new BlowFishCS.BlowFish(keys); byte[] result = blowFish.Encrypt_ECB(bytes); return(result); }
// ----------------------------------------------------------------------------------- /// <summary> /// Writes bytes to an encrypted file /// </summary> /// <param name="path">Path of the file</param> /// <param name="data">Binary data to save</param> /// <param name="shuffledKey">Optional encryption key (shuffled with Util.ShuffleKey)</param> public static void WriteBytes(string path, byte[] data, string shuffledKey = DefaultShuffledKey) { BlowFishCS.BlowFish blowfish = new BlowFishCS.BlowFish(RestoreKey(shuffledKey)); data = blowfish.Encrypt_ECB(data); FileStream stream = new FileStream(path, FileMode.Create); stream.Write(data, 0, data.Length); stream.Close(); }
private void SendThread() { try { while (true) { if (!Connected) { _sendThread = null; break; } _waitSend.WaitOne(); //阻塞当前线程 _waitSend.Reset(); //线程只处理一次,立即阻塞 lock (_sendQueue) { while (_sendQueue.Count > 0) { _sendProtocolItem = _sendQueue.Peek(); _bytes = _sendProtocolItem.ProtocolItemBytes; #if blowfish _mbw.Clear(); _bytes = _encryptBF.Encrypt_ECB(_bytes); _mbw.Write(_bytes.Length + ProtocolConf.PACKGE_LEN); _mbw.Write(_bytes); _bytes = _mbw.ToArray(); #endif int result = 0; int byteSize = _bytes.Length; while (result != byteSize) { result += _socket.Send(_bytes, result, byteSize - result, SocketFlags.None); } _sendQueue.Dequeue(); Debugger.Log("给服务器发送数据:" + result + ", 协议体:" + _sendProtocolItem.ToString()); } } } } catch (SocketException se) { Debugger.LogError("MSocketException" + se); } catch (Exception e) { Debugger.LogError("MSocketException" + e); } }
// ----------------------------------------------------------------------------------- #if UNITY_EDITOR /// <summary> /// Creates a file with the character sheet and each round image /// Only available in editor mode /// encrypting them separately /// </summary> /// <param name="filename">File to save to</param> /// <param name="guid"> Unique id to identify the character </param> /// <param name="tags"> comma separated tags, like IB </param> /// <param name="charSheetFile"> PNG file with the character sheet </param> /// <param name="roundFiles"> PNG files for each round (base, shadow) </param> /// <param name="updateFile"> File to update. If null, a new one is created </param> public static void CreateFile(string filename, string guid, string characterName, string artist, string tags, string charSheetFile, List <string[]> roundFiles, File updateFile = null) { string tempFile = Application.temporaryCachePath + "/temp_charfile.chr"; // count the number of available rounds int availableRounds = updateFile != null ? updateFile.availableRounds : 0; for (int i = availableRounds; i < Config.Rounds; i++) { // can only add extra rounds in this version // not remove or leave empty gaps. So, only increase count // if a new one is added, otherwise keep previous count if (roundFiles[i] != null) { availableRounds++; } } // ----- Validation check ----- // the update file is null, so all other png files must exist and be set string errors = ""; if (updateFile == null) { if (string.IsNullOrEmpty(charSheetFile)) { errors += "You must set the character sheet file\n"; } if (availableRounds == 0) { errors += "Set at least one round file!\n"; } // make sure we don't leave empty holes for (int i = 0; i < availableRounds; i++) { if (string.IsNullOrEmpty(roundFiles[i][0])) { errors += string.Format("You must set the base image file for round {0}\n", i + 1); } if (string.IsNullOrEmpty(roundFiles[i][1])) { errors += string.Format("You must set the shadow image file for round {0}\n", i + 1); } } } // no need to continue at this point if (!string.IsNullOrEmpty(errors)) { throw new Exception(errors); } // ----- File creation ----- BinaryWriter bw = new BinaryWriter(System.IO.File.Open(tempFile, FileMode.Create)); BlowFishCS.BlowFish blowfish = new BlowFishCS.BlowFish(IllogicGate.Data.EncryptedFile.RestoreKey(ShuffledKey)); // save an empty header to make space for it Header header = new Header(Version1, guid); header.availableRounds = availableRounds; header.tags = tags; header.artist = artist; header.characterName = characterName; // if we're updating, keep creation date if (updateFile != null) { header.createdDate = updateFile.header.createdDate; } header.Save(bw); byte[] data; // encrypt and save the character sheet file if (string.IsNullOrEmpty(charSheetFile)) { data = updateFile.baseSheet.source.GetRawTextureData(); } else { data = GetRawTextureData(charSheetFile); } data = LZMAtools.CompressByteArrayToLZMAByteArray(data); data = blowfish.Encrypt_ECB(data); header.characterSheet = new Entry((int)bw.BaseStream.Position, data.Length); bw.Write(data); // encrypt and save round images int img_w, img_h; Orientation orientation; for (int i = 0; i < header.availableRounds; i++) { // load original images if available RoundImages original = null; if (updateFile != null && i < updateFile.availableRounds) { original = updateFile.LoadRound(i); } // just copy the image from the previously loaded character file if (roundFiles[i] == null) { data = original.baseImage.GetRawTextureData(); img_w = original.baseImage.width; img_h = original.baseImage.height; } // load from file else { data = GetRawTextureData(roundFiles[i][0], out img_w, out img_h); } orientation = CheckOrientation(img_w, img_h); if (orientation == Orientation.Invalid) { throw new Exception("Invalid image size for base image " + (i + 1)); } data = LZMAtools.CompressByteArrayToLZMAByteArray(data); data = blowfish.Encrypt_ECB(data); header.roundBase[i] = new Entry((int)bw.BaseStream.Position, data.Length); bw.Write(data); // just copy the image from the previously loaded character file if (roundFiles[i] == null) { data = original.shadowImage.GetRawTextureData(); img_w = original.shadowImage.width; img_h = original.shadowImage.height; } // load from file else { data = GetRawTextureData(roundFiles[i][1], out img_w, out img_h, true); } orientation = CheckOrientation(img_w, img_h); if (orientation == Orientation.Invalid) { throw new Exception("Invalid image size for shadow image " + (i + 1)); } data = LZMAtools.CompressByteArrayToLZMAByteArray(data); data = blowfish.Encrypt_ECB(data); header.roundShadow[i] = new Entry((int)bw.BaseStream.Position, data.Length); bw.Write(data); header.isPortrait[i] = orientation == Orientation.Portrait; } // rewind and overwrite header bw.Seek(0, SeekOrigin.Begin); header.Save(bw); bw.Close(); // replace files if (System.IO.File.Exists(filename)) { System.IO.File.Delete(filename); } System.IO.File.Move(tempFile, filename); }
public string CalculatePasswordHash(string password) { BlowFishCS.BlowFish b = new BlowFishCS.BlowFish(salt); return(b.Encrypt_ECB(password)); }
public static async Task LoginNPortal(string id, string password) { string captchaText; do { var captchaImage = await GetCaptchaImage(); captchaText = await GetCaptchaText(captchaImage); } while (string.IsNullOrEmpty(captchaText) || captchaText.Length != 4); var BlowFish = new BlowFishCS.BlowFish(Encoding.UTF8.GetBytes(password)); var md5Code = BlowFish.Encrypt_ECB(id); var response = await Request("https://nportal.ntut.edu.tw/login.do", "POST", new Dictionary <string, object>() { { "muid", id }, { "mpassword", password }, { "authcode", captchaText }, { "md5Code", md5Code } }, new Dictionary <string, object>() { { "User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36" }, { "Referer", "https://nportal.ntut.edu.tw/index.do?thetime=" + TimeStamp } }); string responseString = await ConvertStreamToString(await response.Content.ReadAsStreamAsync()); response.Dispose(); if (responseString.Contains("登入失敗")) { if (responseString.Contains("密碼錯誤")) { throw new NPException("帳號密碼錯誤", ErrorType.WrongIdPassword); } else if (responseString.Contains("驗證碼")) { await LoginNPortal(id, password); //Retry } else if (responseString.Contains("帳號已被鎖住")) { throw new NPException("嘗試錯誤太多次,帳號已被鎖定10分鐘", ErrorType.AccountLocked); } } else if (!responseString.Contains("\"myPortal.do?thetime=")) { throw new NPException("遇到不明的錯誤", ErrorType.Unknown); } //The folling 2 request will make the server allowing us to login sub-systems response = await Request("https://nportal.ntut.edu.tw/myPortalHeader.do"); //responseString = await ConvertStreamToString(await response.Content.ReadAsStreamAsync()); response.Dispose(); response = await Request("https://nportal.ntut.edu.tw/aptreeBox.do"); //responseString = await ConvertStreamToString(await response.Content.ReadAsStreamAsync()); response.Dispose(); //Login aps await LoginSubSystem("https://nportal.ntut.edu.tw/ssoIndex.do?apUrl=https://aps.ntut.edu.tw/course/tw/courseSID.jsp&apOu=aa_0010-&sso=true&datetime1=" + TimeStamp); //Login aps-stu await LoginSubSystem("https://nportal.ntut.edu.tw/ssoIndex.do?apUrl=https://aps-stu.ntut.edu.tw/StuQuery/LoginSID.jsp&apOu=aa_003&sso=big5&datetime1=" + TimeStamp); }