通过ServiceStack加密消息
给定一个应用程序,需要为来自oAuth的ConsumerKey / ConsumerSecret交换信息
- server running "WebApi" over ServiceStack - consumer is a desktop app
希望使用已经存在的公钥/私钥加密消息
是否有任何支持加密/解密消息,希望透明? 即加密/解密是通过服务器上的filter来处理的吗?
要么
有关如何执行此操作的任何解决方法?
仅供参考 – 服务器已经要求SSL并且请求已经过身份validation,但注册是免费/自动的,因此需要加密
更新
v4.0.42中发布的加密消息传递function现在为所有服务提供安全通道,为客户提供保护,这些客户现在可以通过不安全的HTTP轻松发送和接收加密消息。
目前还没有对ServiceStack中内置的消息级加密的发布支持,尽管这是我们将来考虑添加的内容。 下面是一个您今天可以尝试使用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包装器 ,可以轻松创建公钥/私钥以及使用它们加密/解密文本。
然后,您可以拥有一项服务,并执行以下操作:
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()), }; } }
客户端还需要一个您将在带外交换的公钥/私钥对的副本。