private void encryptFile(object sender, RoutedEventArgs e) { var bytes = File.ReadAllBytes(filePathOpen.Text); var open_key = File.ReadAllLines(SenderPath + @"\open_key.txt"); var D = new BigInteger(open_key[0].Split(' ').Select(a => byte.Parse(a.ToString(), NumberStyles.HexNumber)).ToArray()); var N = new BigInteger(open_key[1].Split(' ').Select(a => byte.Parse(a.ToString(), NumberStyles.HexNumber)).ToArray()); var cryptArr = new BigInteger[(bytes.Length / (N.ToByteArray().Length - 1) + 1)]; for (int i = 0, k = 0; i < bytes.Length; i = i) { var data = bytes.Skip(i).Take(N.ToByteArray().Length - 1).ToArray(); cryptArr[k++] = RSAEx.EnCrypt(new BigInteger(data), D, N); i += data.Length; } File.WriteAllLines(filePathEncrypt.Text, cryptArr.Select(intg => string.Join(" ", intg.ToByteArray().Select(a => a.ToString("X"))))); MessageBox.Show("Шифрование завершено"); }
public static int[] DecryptVector(BigInteger[] vector, PrivateKey privateKey) { return vector.Select(bi => Decrypt(bi, privateKey)).ToArray(); }
public static void Vote(string host, int port, CookieCollection cookieCollection, Guid electionId, BigInteger[] encryptedVector) { var request = CreateRequest(string.Format("http://{0}:{1}/vote", host, port), WebRequestMethods.Http.Post, null, cookieCollection); var data = Encoding.UTF8.GetBytes(string.Format("electionId={0}&vote={1}", HttpUtility.UrlEncode(electionId.ToString()), HttpUtility.UrlEncode(encryptedVector.Select(integer => integer.ToString()).ToArray().ToJsonString()))); using(var requestStream = request.GetRequestStream()) requestStream.Write(data, 0, data.Length); using((HttpWebResponse)request.GetResponse()) { } }
private void EncryptPass(object sender, RoutedEventArgs e) { var bytes = File.ReadAllBytes(pathPassTB.Text); var open_key = File.ReadAllLines(SenderPath + @"\open_key.txt"); var D = new BigInteger(open_key[0].Split(' ').Select(a => byte.Parse(a.ToString(), NumberStyles.HexNumber)).ToArray()); var N = new BigInteger(open_key[1].Split(' ').Select(a => byte.Parse(a.ToString(), NumberStyles.HexNumber)).ToArray()); var cryptArr = new BigInteger[bytes.Length / N.ToByteArray().Length + 1]; for (int i = 0, k = 0; i < bytes.Length; i = i) { var data = bytes.Skip(i).Take(N.ToByteArray().Length).ToArray(); cryptArr[k++] = RSAEx.EnCrypt(new BigInteger(data), D, N); i += data.Length; } File.WriteAllLines(RecipientPath + @"\pass.cod", cryptArr.Select(intg => string.Join(" ", intg.ToByteArray().Select(a => a.ToString("X"))))); decryptPassGrid.Visibility = Visibility.Visible; }
public static async Task VoteAsync(string host, int port, CookieCollection cookieCollection, Guid electionId, BigInteger[] encryptedVector) { var data = Encoding.UTF8.GetBytes(string.Format("electionId={0}&vote={1}", HttpUtility.UrlEncode(electionId.ToString()), HttpUtility.UrlEncode(encryptedVector.Select(integer => integer.ToString()).ToArray().ToJsonString()))); var uri = new Uri(string.Format("http://{0}:{1}/vote", host, port)); var httpResult = await AsyncHttpClient.DoRequestAsync(uri, WebRequestMethods.Http.Post, null, cookieCollection, data); if(httpResult.StatusCode != HttpStatusCode.OK) throw new ServiceException(ExitCode.MUMBLE, string.Format("Failed to process VoteAsync: {0}", httpResult.StatusCode)); }