static void Main(string[] args) { // initialize connection to active mq with producer and consumer IConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616"); IConnection connection = factory.CreateConnection(); connection.Start(); ISession session = connection.CreateSession(); IDestination destRequestQueue = session.GetQueue(HashFinderConfig.RequestQueueName); IMessageProducer producer = session.CreateProducer(destRequestQueue); IDestination destResponseQueue = session.GetQueue(HashFinderConfig.ResponseQueueName); IMessageConsumer consumer = session.CreateConsumer(destResponseQueue); // ------------------------------------------------------------------------------------------------------------ // calculate a hash to find based on user input PrintTimestamp(); Console.Write("Please enter your pin: "); var myPin = Console.ReadLine(); PrintTimestamp(); var hash = PinUtil.GetPinHash(myPin); PrintTimestamp(); Console.WriteLine(); Console.WriteLine("Your hash is: " + hash); // ------------------------------------------------------------------------------------------------------------ // send workers their work to do for (int i = 0; i <= 9999; i++) { var objectMessage = producer.CreateObjectMessage( new HashFinderRequest() { PinToCalculate = i.ToString("0000"), HashToFind = hash }); producer.Send(objectMessage); } PrintTimestamp(); // ------------------------------------------------------------------------------------------------------------ IMessage message; if ((message = consumer.Receive(TimeSpan.FromMinutes(2))) != null) { var objectMessage = message as IObjectMessage; var mapMessage = objectMessage?.Body as HashFinderResponse; Console.WriteLine("Successfully found pin by worker: " + (mapMessage?.FoundPin ?? "<invalid pin>")); PrintTimestamp(); } PrintTimestamp(); Console.WriteLine("... processing finished"); }
static void Main(string[] args) { // init connection to activeMQ IConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616"); ((ConnectionFactory)factory).PrefetchPolicy.SetAll(0); IConnection connection = factory.CreateConnection(); connection.Start(); ISession session = connection.CreateSession(); IDestination requestDestination = session.GetQueue(HashFinderConfig.RequestQueueName); IMessageConsumer consumer = session.CreateConsumer(requestDestination); IDestination responseDestination = session.GetQueue(HashFinderConfig.ResponseQueueName); IMessageProducer producer = session.CreateProducer(responseDestination); // ----------------------------------------------------------------------------------------------- // read worker-messages IMessage message; while ((message = consumer.Receive(TimeSpan.FromMinutes(1))) != null) { // map message body to our strongly typed message type var objectMessage = message as IObjectMessage; if (objectMessage?.Body is RequestMessage mapMessage) { // debug output Console.WriteLine(mapMessage.PinToCalculate); // analyze pin if (PinUtil.GetMD5Hash(mapMessage.PinToCalculate) == mapMessage.ResultHash) { // pin found! Console.WriteLine( $"yeaaah found the hash {mapMessage.PinToCalculate} - {mapMessage.ResultHash}"); producer.Send(producer.CreateObjectMessage( new ResponseMessage() { ResultPin = mapMessage.PinToCalculate, ResultHash = mapMessage.ResultHash })); break; } } } while (consumer.Receive(TimeSpan.FromSeconds(1)) != null) { } Console.WriteLine("... processing finished"); Console.WriteLine("Press enter to continue..."); Console.ReadLine(); }
static void Main(string[] args) { // initialize connection IConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616"); ((ConnectionFactory)factory).PrefetchPolicy.SetAll(0); IConnection connection = factory.CreateConnection(); connection.Start(); ISession session = connection.CreateSession(); IDestination destRequestQueue = session.GetQueue(HashFinderConfig.RequestQueueName); IMessageConsumer consumer = session.CreateConsumer(destRequestQueue); IDestination destResponseQueue = session.GetQueue(HashFinderConfig.ResponseQueueName); IMessageProducer producer = session.CreateProducer(destResponseQueue); // ------------------------------------------------------------------------------------------------------------ // receive packages IMessage message; while ((message = consumer.Receive(TimeSpan.FromMinutes(1))) != null) { var objectMessage = message as IObjectMessage; var mapMessage = objectMessage?.Body as HashFinderRequest; // analyze packages if (PinUtil.GetPinHash(mapMessage?.PinToCalculate) == (mapMessage?.HashToFind ?? "invalid hash")) { // successfully found hash Console.WriteLine("hash found for pin " + (mapMessage.PinToCalculate ?? "<invalid>")); // answer to response queue producer.Send( producer.CreateObjectMessage(new HashFinderResponse() { FoundPin = mapMessage.PinToCalculate, HashToFind = mapMessage?.HashToFind })); break; } } // clean queue, so that in the next start of this service we don't need to bother with old data while (consumer.Receive(TimeSpan.FromSeconds(1)) != null) { // clean request queue } // ------------------------------------------------------------------------------------------------------------ // keep window open, so we can see the std-out (and check if the pin to find is right) Console.WriteLine("... processing finished"); Console.WriteLine("press enter to stop"); Console.ReadLine(); }
/// <summary> /// PIN加密-解密按钮事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnPinDecrypt_Click(object sender, EventArgs e) { string masterKey = this.tboxPinMasterKey.Text.Trim(); string pinKey = this.tboxPinKey.Text.Trim(); string cardNo = this.tboxPinCardNo.Text.Trim(); string pin = this.tboxPinPassword.Text.Trim(); if (masterKey != "" && masterKey.Length % 8 != 0) { MessageBox.Show("主密钥长度有误!"); return; } if (pinKey == "") { MessageBox.Show("PIN密钥不能为空!"); return; } if (pinKey.Length % 8 != 0) { MessageBox.Show("PIN密钥长度有误!"); return; } if (pin == "") { MessageBox.Show("密码不能为空!"); return; } if (masterKey != "") { pinKey = DesEncryptUtil.desDecrypt(pinKey, masterKey); } //带卡号加密 if (cardNo != "") { //解密 string pinBlock = DesEncryptUtil.desDecrypt(pin, pinKey); byte[] pinBlockClearText = PinUtil.reverse(cardNo, pinBlock); string result = StringUtil.byteToHexString(pinBlockClearText); this.tboxPinResult.Text = result; } //不带卡号加密 else { string result = DesEncryptUtil.desDecrypt(pin, pinKey); this.tboxPinResult.Text = result; } }
static void Main(string[] args) { // connect to activeMQ IConnectionFactory factory = new ConnectionFactory(HashFinderConfig.ActiveMQEndpoint); IConnection connection = factory.CreateConnection(); connection.Start(); ISession session = connection.CreateSession(); IDestination requestDestination = session.GetQueue(HashFinderConfig.RequestQueueName); IMessageProducer producer = session.CreateProducer(requestDestination); IDestination responseDestination = session.GetQueue(HashFinderConfig.ResponseQueueName); IMessageConsumer consumer = session.CreateConsumer(responseDestination); // ----------------------------------------------------------------------------------------------- // handle UI input and calculate Hash Console.Write("Please enter your pin: "); var pin = Console.ReadLine(); var hashedPin = PinUtil.GetMD5Hash(pin); Console.WriteLine(); Console.WriteLine("your hash is: " + hashedPin); // ----------------------------------------------------------------------------------------------- var itemFound = false; Task t = Task.Run(() => { // create messages for workers for (int i = 0; i <= 9999 && !itemFound; i++) { var objectMessage = producer.CreateObjectMessage( new RequestMessage() { PinToCalculate = i.ToString("0000"), ResultHash = hashedPin, }); producer.Send(objectMessage); } }); // ----------------------------------------------------------------------------------------------- // read worker-responses IMessage message; if ((message = consumer.Receive(TimeSpan.FromMinutes(1))) != null) { itemFound = true; // map message body to our strongly typed message type var objectMessage = message as IObjectMessage; var mapMessage = objectMessage?.Body as ResponseMessage; Console.WriteLine($"pin is: {mapMessage.ResultPin} ({mapMessage.ResultHash})"); } Task.WaitAll(new[] { t }); // final UI candy Console.WriteLine("... processing finished"); Console.WriteLine("Press enter to continue..."); Console.ReadLine(); }