public ActionResult Process(RequestData reqData) { /*Steps * 1- get request object from view and convert it to BCD to reduce request size as requested * 2- get public key for Encryption from WCF to ecrypt request with it and WCF service will * decrypt it by private key. * 3-convert request to byte array. * 4- Encrypt the request and send it with public key of request and new public key for service to can * Encrypt it's response. * 5-send request * 6- WCF service will make reverse operation like * 7-decrypt the request by privite key. * 8-convert byte array to object. * 9-convert bcd object to normal object * 10- then make this steps for response with public key for ecrypt response. */ /////////////////////// //convert request to BCD Request req = ModelHelper.GetRequestObjectBcd(reqData); //get Encrypted public key from the WCF service base64 string base64PublicKey = GetRequestKey(); //convert base64 public key to string string publicReqKey = Encoding.ASCII.GetString(Convert.FromBase64String(base64PublicKey)); //convert the request to byte array to can encript it. byte[] jsonBytes = ByteArrayHelper.ObjectToByteArray(req, 48); //prepare for encraption by public key rsaEncrypt.FromXmlString(publicReqKey); //Encrypt the request by public key from WCF byte[] encryptedJsonBytes = rsaEncrypt.Encrypt(jsonBytes, false); //prepare public key for response to encript it in WCF service RSACryptoServiceProvider rsaEncryptRes = new RSACryptoServiceProvider(EncraptionKeySize); string publicResKeys = rsaEncryptRes.ToXmlString(false); string privateResKeys = rsaEncryptRes.ToXmlString(true); string base64PublicResKey = Convert.ToBase64String(Encoding.ASCII.GetBytes(publicResKeys)); //Make a request to WCF service with public key for request and response and request Encrypted var response = srv.ProcessRequest(encryptedJsonBytes, base64PublicKey, base64PublicResKey); //Decrypt the response byte[] decryptedBytes = rsaEncryptRes.Decrypt(response, false); //convert response from byte array to object Response res = ByteArrayHelper.ByteArrayToObject <Response>(decryptedBytes); //convert BCD response to Normal response object ResponseData responseData = ModelHelper.GetResponseData(res); //view the response in view return(View(responseData)); }
public byte [] ProcessRequest(byte[] request, string base64PublicKey, string base64PublicResKey) { //convert base64 public key to string string publicXmlKey = Encoding.ASCII.GetString(Convert.FromBase64String(base64PublicKey)); //prepare the object to decypt the request RSACryptoServiceProvider rsaDecrypt = new RSACryptoServiceProvider(EncraptionKeySize); rsaDecrypt.FromXmlString(Keys[publicXmlKey]); //Decrypt request by privite key byte[] decryptedBytes = rsaDecrypt.Decrypt(request, false); //convert request from byte array to object Request req = ByteArrayHelper.ByteArrayToObject <Request>(decryptedBytes); //convert BCD request to Normal request object RequestData requestData = ModelHelper.GetRequestData(req); //remove the key from the list because we done with it. Keys.Remove(publicXmlKey); //make response object ResponseData responseData = new ResponseData() { ApprovalCode = "123123", Message = "Success", DateTime = DateTime.Now, ResponseCode = "00" }; //convert response to BCD Response res = ModelHelper.GetResponseObjectBcd(responseData); //Convert it to byte array byte[] jsonBytes = ByteArrayHelper.ObjectToByteArray(res, 45); //prepare to encypt it by public key string publicResKey = Encoding.ASCII.GetString(Convert.FromBase64String(base64PublicResKey)); rsaDecrypt.FromXmlString(publicResKey); //encrypt the request byte[] encryptedJsonBytes = rsaDecrypt.Encrypt(jsonBytes, false); //return the response. return(encryptedJsonBytes); }
public void Subscribe <TEvent>(IEventHandler <TEvent> eventHandler) where TEvent : IEvent { ConnectionFactory factory = CreateConnectionFactory(); var connection = factory.CreateConnection(); var channel = connection.CreateModel(); channel.QueueDeclare(queue: GenerateQueueName(typeof(TEvent)), durable: false, exclusive: false, autoDelete: false, arguments: null); var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { var body = ea.Body; var message = ByteArrayHelper.ByteArrayToObject <TEvent>(body); eventHandler.Handle(message); }; channel.BasicConsume(queue: GenerateQueueName(typeof(TEvent)), autoAck: true, consumer: consumer); }