/// <summary> /// Executes a selftest /// </summary> /// <remarks> /// Call this method to make sure that the instance is able to produce /// valid output according to the specification. /// </remarks> /// <returns> /// true: selftest passed / false: selftest failed /// </returns> public static bool RunSelfTest() { uint unHi, unLo; BlowfishECB bfe; unHi = TEST_VECTOR_PLAIN[0]; unLo = TEST_VECTOR_PLAIN[1]; bfe = new BlowfishECB(TEST_KEY, 0, TEST_KEY.Length); bfe.EncryptBlock(unHi, unLo, out unHi, out unLo); if ((TEST_VECTOR_CIPHER[0] != unHi) || (TEST_VECTOR_CIPHER[1] != unLo)) { return(false); } bfe.DecryptBlock(unHi, unLo, out unHi, out unLo); if ((TEST_VECTOR_PLAIN[0] != unHi) || (TEST_VECTOR_PLAIN[1] != unLo)) { return(false); } return(true); }
// notice that return byte[] size must be multiple of block size private static byte[] decryptBytes(BlowfishECB blowFish, byte[] data) { if (data.Length % BlowfishECB.BLOCK_SIZE > 0) { throw new Exception("cannot decrypt, data not in multiple of block size"); } byte[] decrypted_data = new byte[data.Length]; blowFish.Decrypt(data, 0, decrypted_data, 0, data.Length); return(decrypted_data); }
////////////////////////////////////////////////////////////////////// /// <summary> /// Returns an identical, non-shallow copy of the current instance. /// This can be very useful if you need multiple instances all using /// the same key, since the expensive cipher setup will be bypassed. /// </summary> /// <returns> /// The new instance, which usually typecasted back. /// </returns> public object Clone() { BlowfishECB result; result = new BlowfishECB(); result.m_pbox = (uint[])this.m_pbox.Clone(); result.m_sbox1 = (uint[])this.m_sbox1.Clone(); result.m_sbox2 = (uint[])this.m_sbox2.Clone(); result.m_sbox3 = (uint[])this.m_sbox3.Clone(); result.m_sbox4 = (uint[])this.m_sbox4.Clone(); result.m_block = (byte[])this.m_block.Clone(); result.m_nIsWeakKey = this.m_nIsWeakKey; return(result); }
// notice that return byte[] size must be multiple of block size private static byte[] encryptBytes(BlowfishECB blowFish, byte[] data) { int l_block_size = BlowfishECB.BLOCK_SIZE; int nMod = data.Length % l_block_size; int nLen = (nMod == 0) ? data.Length : data.Length - nMod + l_block_size; // console_WriteLine("mod/len = " + nMod + "/" + nLen); byte[] encrypted_data = new byte[nLen]; blowFish.Encrypt(data, 0, encrypted_data, 0, data.Length - nMod); if (nMod > 0) { byte[] last_data = new byte[l_block_size]; Array.Copy(data, data.Length - nMod, last_data, 0, nMod); blowFish.Encrypt(last_data, 0, encrypted_data, data.Length - nMod, l_block_size); } return(encrypted_data); }
private void InvokeMsg() { string fragmentMsg = string.Empty; string revStr = string.Empty; while (true) { try { Dictionary <string, byte[]> dic = new Dictionary <string, byte[]>(); if (MsgQueue.Count > 0 && MsgQueue.TryDequeue(out dic)) { foreach (var key in dic.Keys) { byte[] arrRecMsg = new byte[1024 * 1024]; int length = int.Parse(key.Split('_')[1]); arrRecMsg = dic[key]; if (arrRecMsg == null || arrRecMsg.Length == 0) { continue; } if (this.IsEncrypt) { byte[] arrRecMsgDecrypt = null; if ((arrRecMsg.Length - 4) % BlowfishECB.BLOCK_SIZE == 0) { arrRecMsgDecrypt = BlowfishECB.decryptBytes(Key, arrRecMsg.Skip(4).ToArray()); } else { //arrRecMsgDecrypt = BlowfishECB.decryptBytes(Key, arrRecMsg); BlowFish b = new BlowFish(Key); arrRecMsgDecrypt = b.Decrypt_ECB(arrRecMsg.Skip(4).ToArray()); } revStr = System.Text.Encoding.UTF8.GetString(arrRecMsgDecrypt); //Program.logger.LogInfo("Key:" + System.Text.Encoding.Default.GetString(Key)); //Program.logger.LogInfo("原始报文:" + System.Text.Encoding.Default.GetString(arrRecMsg)); } else { revStr = System.Text.Encoding.UTF8.GetString(arrRecMsg.Skip(4).Take(length - 4).ToArray()); } List <string> messages = new List <string>(); if (!string.IsNullOrEmpty(fragmentMsg) && revStr.IndexOf(messageStart) > 0 && revStr.IndexOf(messageEnd) > revStr.IndexOf(messageStart)) { int indexEnd = revStr.IndexOf(messageEnd); int indexStart = revStr.IndexOf(messageStart); revStr = fragmentMsg + revStr.Substring(indexStart); fragmentMsg = string.Empty; } #region 消息截取,获取格式正常的消息 while (revStr.IndexOf(messageStart) > -1) { int indexEnd = revStr.IndexOf(messageEnd); int indexStart = revStr.IndexOf(messageStart); if (indexEnd > -1) { if (indexStart < indexEnd) { var msgItem = revStr.Substring(indexStart, indexEnd + messageEnd.Length - indexStart); if (messages.Count > 0 && messages.Last() == msgItem) { revStr = revStr.Substring(indexEnd + messageEnd.Length); continue; } else { messages.Add(msgItem); revStr = revStr.Substring(indexEnd + messageEnd.Length); } } else { if (indexStart > 0) { revStr = revStr.Substring(indexStart); } else { revStr = revStr.Substring(indexEnd + messageEnd.Length); } } } else { if (indexStart > -1) { fragmentMsg = revStr.Substring(indexStart); revStr = fragmentMsg; } break; } } #endregion if (MessageReceive != null) { messages.ForEach(_ => MessageReceive.Invoke(_)); messages.Clear(); } } } else { Thread.Sleep(100); } } catch (Exception ex) { Program.logger.LogInfoDetail("Ayers 消息分析线程异常:{0}", ex.Message); } } }
public static byte[] decryptBytes(byte[] key, byte[] data) { BlowfishECB blowFish = new BlowfishECB(key, 0, key.Length); return(decryptBytes(blowFish, data)); }