private void UploadKey(string hashsecret) { PublicKey keytoupload = new PublicKey { key = MyPublicKey.Modulus.ToBase64(), exp = MyPublicKey.Exponent.ToBase64(), secret = hashsecret }; string json = JsonConvert.SerializeObject(keytoupload); const string url = @"http://cst407.azurewebsites.net/Lab6/UploadPubKey"; WebClient webClient = new WebClient(); webClient.Headers[HttpRequestHeader.ContentType] = "application/json"; string responsejson = webClient.UploadString(url, json); AcmeResponse <EmployeeID> response = JsonConvert.DeserializeObject <AcmeResponse <EmployeeID> >(responsejson); if (response.Status == "OK") { EmpID = response.Data.ID; SaveEmpID(); } else { throw new Exception(response.Message); } }
private MissionDetails GetMissionDetails() { const string url = @"http://cst407.azurewebsites.net/Lab6/GetMissionDetails"; WebClient webClient = new WebClient(); string responsejson = webClient.DownloadString(url + "?id=" + EmpID); AcmeResponse <MissionDetails> response = JsonConvert.DeserializeObject <AcmeResponse <MissionDetails> >(responsejson); if (response.Status == "OK") { return(response.Data); } else { throw new Exception(response.Message); } }
public void UploadMissionDetails() { byte[] key = new byte[32]; byte[] iv = new byte[16]; using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider()) { rng.GetBytes(key); rng.GetBytes(iv); } Aes aes = Aes.Create(); aes.Key = key; aes.IV = iv; ICryptoTransform encryptor = aes.CreateEncryptor(); byte[] encryptedMessageBytes; using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) { byte[] decryptedMissionBytes = DecryptedMessageResponse.GetBytes(); cs.Write(decryptedMissionBytes, 0, decryptedMissionBytes.Length); } encryptedMessageBytes = ms.ToArray(); } string encryptedMessage = encryptedMessageBytes.ToBase64(); RSA rsa = RSA.Create(2048); rsa.ImportParameters(AcmePublicKey); byte[] encryptedKeyBytes = rsa.Encrypt(key, RSAEncryptionPadding.Pkcs1); string encryptedKey = encryptedKeyBytes.ToBase64(); rsa.ImportParameters(MyPrivateKey); byte[] signatureBytes = rsa.SignData(encryptedMessageBytes, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); string signature = signatureBytes.ToBase64(); MissionResponse missionresponse = new MissionResponse { id = EmpID, key = encryptedKey, iv = iv.ToBase64(), msg = encryptedMessage, sig = signature }; string json = JsonConvert.SerializeObject(missionresponse); const string url = @"http://cst407.azurewebsites.net/Lab6/FinishMission"; WebClient webClient = new WebClient(); webClient.Headers[HttpRequestHeader.ContentType] = "application/json"; string responsejson = webClient.UploadString(url, json); AcmeResponse <FinishMissionResponse> response = JsonConvert.DeserializeObject <AcmeResponse <FinishMissionResponse> >(responsejson); if (response.Status == "OK") { MissionResponse = response.Data.Response; } else { throw new Exception(response.Message); } }