我目前正在尝试使用RSACrytoServiceProveider类实现一个类来处理我的应用程序实例之间的安全通信.第一个问题:实现单个类来处理发送者/接收者角色或者我应该将角色分成单个类是一个好主意吗?这是我到目前为止所做的:
using System;
using System.Text;
using System.Security.Cryptography;
namespace Agnus.Cipher
{
public class RSA
{
private byte[] plaintextBytes;
private byte[] ciphertextBytes;
private RSACryptoServiceProvider rSAProviderThis;
private RSACryptoServiceProvider rSAProviderOther;
public string PublicKey
{
get { return rSAProviderThis.ToXmlString(false); }
}
public RSA()
{
rSAProviderThis = new RSACryptoServiceProvider { PersistKeyInCsp = true };
plaintextBytes = Encoding.Unicode.GetBytes(PublicKey);
}
public void InitializeRSAProviderOther(string parameters)
{
rSAProviderOther.FromXmlString(parameters);
}
public byte[] Encrypt()
{
return rSAProviderThis.Encrypt(plaintextBytes, true);
}
public byte[] Decrypt()
{
return rSAProviderThis.Decrypt(ciphertextBytes, true);
}
public byte[] Sign()
{
using …Run Code Online (Sandbox Code Playgroud) 我正在开发一个需要通信的工具:通过互联网发送和接收该工具的其他远程实例的文件.在这种情况下哪种通信选项最好用?套接字?
我正在尝试用C++ 实现Eratosthene筛选.但是经过多次尝试后,我总是遇到运行时错误.我认为这与使用的迭代器的状态在某处被破坏有关.我不能把手指放在它上面.这是我的代码:
//Sieves all multiples of the current sequence element
bool multiple_sieve(std::list<int>& num_list)
{
std::list<int>::iterator list_iter(num_list.begin());
std::list<int>::reverse_iterator last_element_iter(num_list.rbegin());
for(std::list<int>::iterator elements_iter(++list_iter);
elements_iter != num_list.end();)
{
if((*elements_iter % *list_iter == 0) &&
(*elements_iter <= *last_element_iter) && (*list_iter != 1))
num_list.erase(elements_iter);
else ++elements_iter;
}
return true;
}
std::list<int>& prime_sieve(std::list<int>& num_list)
{
for(std::list<int>::iterator list_iter(num_list.begin());
list_iter != num_list.end(); ++list_iter)
multiple_sieve(num_list);
return num_list;
}
Run Code Online (Sandbox Code Playgroud)
我做错了什么?什么导致运行时错误?
更新:当我在我的测试中运行它时,我收到错误消息"列表迭代器不兼容".
我想用我正在研究的这种通用扩展方法将IEnumerable实现固定在内存中.它似乎适用于数组,但与其他序列(列表和集合)失败.这是方法实现.
// <summary>
/// Pins an IEnumerable of type T in memory
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sequence"></param>
/// <returns></returns>
public static GCHandle Pin<T>(this IEnumerable<T> @sequence)
{
return GCHandle.Alloc(@sequence, GCHandleType.Pinned);
}
Run Code Online (Sandbox Code Playgroud)
为什么某些类型失败但为其他类型工作?你能解释一下这背后的概念吗?有没有比我的通用方法更好的方法呢?谢谢.