public ActionResult VerifySignature(HttpPostedFileBase file, String[] RSAPriv, string[] RSAPub, string [] Sign) { if (file.ContentLength > 0) { MemoryStream target = new MemoryStream(); file.InputStream.CopyTo(target); byte[] data = target.ToArray(); RsaKeyParameters pubk; using (var stringReader = new StringReader(string.Join("\n", RSAPub))) { var pemReader = new PemReader(stringReader); var pemObject = pemReader.ReadObject(); // null! pubk = (RsaKeyParameters)pemObject; } try { if (RSASigner.VerifySignedHash(data, Convert.FromBase64String(string.Join("\n", Sign)), pubk)) { ViewBag.Message = "Zgadza się!"; } else { ViewBag.Message = "Błąd weryfikacji"; } return(View("UploadFile")); } catch { ViewBag.Message = "Błędny podpis/klucz"; return(View("UploadFile")); } } ViewBag.Message = "Błąd wysyłania pliku."; return(View("UploadFile")); }
public LicenseResult(byte[] licenseData, Client client) { try { var rsaSigner = new RSASigner(); //We always should have the same format using (var sourceStream = new MemoryStream(licenseData)) using (var reader = new BinaryReader(sourceStream)) { long dataLength = reader.ReadInt64(); byte[] data = reader.ReadBytes((int)dataLength); byte[] sign = reader.ReadBytes((int)(licenseData.Length - 8 - dataLength)); var signBuffer = RSASigner.GetSignBuffer(data, new[] { BitConverter.GetBytes(DateTime.UtcNow.Year), client.GetSerialNumber() }); bool valid = rsaSigner.Validate(signBuffer, sign); if (!valid) { throw new LicenseValidationException("License sign is invalid (maybe clock shifting)"); } LicenseData = client.Decrypt(data); } } catch (Exception e) { throw new LicenseException("License corrupted", e); } }
public string ToOfflineData() { using (var memoryStream = new MemoryStream()) { using (var writer = new BinaryWriter(memoryStream)) { var header = (byte)((byte)Type << 7 | ClientId.Length); writer.Write(header); writer.Write(ClientId); if (Type == RequestType.Activate) { //Write cert and license data writer.Write((ushort)Certificate.Length); writer.Write(Certificate); if (LicenseKey.Length > 0) { writer.Write((ushort)LicenseKey.Length); writer.Write(LicenseKey); } } var data = memoryStream.ToArray(); var signBuffer = RSASigner.GetSignBuffer(data, new[] { HashSecret.GetSecret() }); writer.Write(SHA512.Create().ComputeHash(signBuffer)); return(DataEncoder.ToHexString(memoryStream.ToArray())); } } }
public ActionResult GenerateRSA() { AsymmetricCipherKeyPair newkeys = RSASigner.GetKeyPairWithDotNet(); this.Session["keys"] = newkeys; ViewBag.pubkey = RSASigner.FormatToPEM(newkeys.Public); ViewBag.privkey = RSASigner.FormatToPEM(newkeys.Private); return(View("UploadFile")); }
public ActionResult SignFile(HttpPostedFileBase file, String[] RSAPriv, string [] RSAPub) { if (file != null && file.ContentLength > 0) { MemoryStream target = new MemoryStream(); file.InputStream.CopyTo(target); byte[] data = target.ToArray(); ViewBag.rsasig = RSASigner.Sign(data, RSAPriv); ViewBag.Message = "Podpisano!"; return(View("UploadFile")); } ViewBag.Message = "Błąd wysyłania pliku."; return(View("UploadFile")); }
public static LicenseRequest FromOfflineData(string data) { using (var memoryStream = new MemoryStream(DataEncoder.FromHexString(data))) { using (var reader = new BinaryReader(memoryStream)) { var header = reader.ReadByte(); var type = (RequestType)(header >> 7); var cidLength = header ^ (byte)type << 7; var request = new LicenseRequest(type) { ClientId = reader.ReadBytes(cidLength) }; if (type == RequestType.Activate) { request.Certificate = reader.ReadBytes(reader.ReadUInt16()); if (memoryStream.Position < memoryStream.Length - 66 /*UInt32+SHA512*/) { var licenseLength = reader.ReadUInt16(); if (licenseLength > 0) { request.LicenseKey = reader.ReadBytes(licenseLength); } } else { request.LicenseKey = new byte[0]; } } var dataEnd = memoryStream.Position; var sign = reader.ReadBytes(64); //Verify sign memoryStream.Position = 0; var databuffer = reader.ReadBytes((int)dataEnd); var signBuffer = RSASigner.GetSignBuffer(databuffer, new[] { HashSecret.GetSecret() }); var signToValidate = SHA512.Create().ComputeHash(signBuffer); if (sign.Where((t, i) => t != signToValidate[i]).Any()) { throw new LicenseValidationException("Signatures doesn't match"); } return(request); } } }