Update
The 加密消息功能 https://github.com/ServiceStack/ServiceStack/wiki/Encrypted-Messaging发布于v4.0.42 https://github.com/ServiceStack/ServiceStack/blob/master/docs/2015/release-notes.md#v4042-release-notes现在为所有服务提供了一个安全通道,为现在可以通过不安全的 HTTP 轻松发送和接收加密消息的客户端提供保护。
没有发布对内置消息级加密的支持服务栈 http://www.servicestack.net目前,尽管这是我们正在考虑在未来添加的内容。下面是您现在可以尝试使用 ServiceStack 进行探索的方法示例(您需要进行更多的研发才能得出良好的可靠设计),并且由于这是一个将来可能会发生变化的领域,因此它不是一个我们现在支持的用例,但这里有一些可以与 ServiceStack 一起使用的东西,因为它现在是:
class EncryptedRequest
{
//Name of the request type
public string Type { get; set; }
//Serialized Request DTO using something like JSON
public string EncryptedBody { get; set; }
//optional: let server the private key that was used (if multiple)
public string PrivateKeyMd5Hash { get; set; }
}
class EncryptedResponse
{
//Name of the response type
public string Type { get; set; }
//Serialized Response DTO
public string EncryptedBody { get; set; }
//optional
public string PublicKeyMd5Hash { get; set; }
}
这里有一个方便的CryptUtils 包装器 https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack.Common/CryptUtils.cs这使得创建公钥/私钥以及使用它们加密/解密文本变得容易。
然后你可以拥有一个服务,并执行以下操作:
class EncryptedService : Service
{
const string PublicKey = ...;
const string PrivateKey = ...;
EncryptedResponse Any(EncryptedRequest request)
{
var requestType = Type.GetType("{0}.{1}"
.Fmt(typeof(EncryptedRequest).Namespace, request.Type));
var json = CryptUtils.Decrypt(PrivateKey, request.EncryptedBody);
var requestDto = JsonSerializer.DeserializeFromString(json,requestType);
var responseDto = GetAppHost().Config.ServiceController
.Execute(requestDto, base.RequestContext);
return new EncryptedResponse {
Type = responseDto.GetType().Name,
EncryptedBody = CryptUtils.Encrypt(PublicKey, responseDto.ToJson()),
};
}
}
客户端还需要您将在带外交换的公钥/私钥对的副本。