3

给定一个应用程序需要从

- server running "WebApi" over ServiceStack
- consumer is a desktop app

希望使用已经存在的公钥/私钥加密消息

是否支持加密/解密消息,希望是透明的?即加密/解密是通过服务器上的过滤器处理的?

或者

关于如何做到这一点的任何解决方法?

仅供参考 - 服务器已经需要 SSL 并且请求已经过身份验证,但是注册是免费/自动的,因此需要加密

4

1 回答 1

2

更新

v4.0.42中发布的加密消息功能现在为所有服务提供安全通道,为现在可以通过不安全的 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()), 
        }; 
    } 
} 

客户端还需要您将在带外交换的公钥/私钥对的副本。

于 2013-04-19T17:27:37.280 回答