gpt4 book ai didi

c# - 如何构建这个允许多个用户/对象的加密系统

转载 作者:行者123 更新时间:2023-11-30 15:48:14 25 4
gpt4 key购买 nike

我正在尝试找出如何为我的项目创建最佳解决方案。我在 Photoshop 中制作了这张简单的图片,试图说明问题以及我想要它的方式(如果可能的话)。

Illustrative image

我也试着根据图片来解释吧。首先我们在左边有几个对象,这些对象都用自己的加密 key (图片上的 EKey)加密,然后存储在数据库中。另一方面,我们将不同的用户置于角色中(一个用户可以担任很多角色),并且角色与不同的对象相关联。因此,一个人只能访问该角色提供的对象。因此,例如角色 A 可能可以访问对象 A 和对象 B。角色 B 只能访问对象 C,而角色 C 可以访问所有对象。这没什么奇怪的,对吧?不同的角色可以访问不同的对象。

现在进入问题部分。

每个用户都必须使用他/她的用户名/密码登录,然后他/她才能访问他/她的角色提供的对象。所有对象都已加密,因此她需要以某种方式获得解密 key 。我不想将加密 key 作为文本字符串存储在服务器上。如果可能,应该使用用户密码(连同角色)或类似密码对其进行解密。这样,您必须是服务器上的用户才能解密对象并使用它。

我正在考虑制作一个公钥/私钥加密系统,但我有点卡在如何为不同的用户提供对象的解密 key 上。因为我需要能够将用户移入和移出角色、添加新用户、添加新角色以及创建/删除对象。

然后会有一位管理员添加一些数据,以允许该角色的用户获得解密 key 来解密对象。

没有什么是静态的,我正在尝试了解如何构建它,或者是否有更好的解决方案。

唯一的标准是:

-加密对象。

-解密 key 不应存储为文本。

-不同的用户可以访问不同的对象。

-不必有角色。

最佳答案

这可以通过使用 SQL Server 加密基础结构来实现。您使用自己的对称 key 加密每个对象(A、B、C、D)(数据始终使用对称 key 加密,从不使用非对称 key )。每个角色都有一个非对称 key 或证书,因此存在非对称 key A、B 和 C。角色非对称 key 使用角色的密码进行加密。每个对称 key 都由有权访问它的角色的非对称 key 加密(对称 key 可以多次加密)。当用户进入系统时,它会使用角色特定的证书/对称 key 打开其角色的对称 key 。这会将对称 key 放置在当前用户的钥匙串(keychain)中,从而可以访问使用这些 key 加密的对象。

这是一个代码演示:

:setvar server .
:setvar dbname cryptdemo

:connect $(server)
use master;

if db_id('$(dbname)') is not null
drop database [$(dbname)];

create database [$(dbname)];
go

:connect $(server)
use [$(dbname)];
go
create certificate RoleA
encryption by password = '123!#Password'
with subject = 'RoleA'

create certificate RoleB
encryption by password = '213!#Password'
with subject = 'RoleB'

create certificate RoleC
encryption by password = '312!#Password'
with subject = 'RoleC'
go

:connect $(server)
use [$(dbname)];
go
-- Role A has access to Object A and Object B
create symmetric key ObjectA WITH ALGORITHM = AES_256
encryption by certificate RoleA;
create symmetric key ObjectB WITH ALGORITHM = AES_256
encryption by certificate RoleA;
go

:connect $(server)
use [$(dbname)];
go
-- Role B has access to Object C
create symmetric key ObjectC WITH ALGORITHM = AES_256
encryption by certificate Roleb;
go

:connect $(server)
use [$(dbname)];
go
-- Role C has access to Objects A, B and C
open symmetric key ObjectA
decryption by certificate RoleA with password = '123!#Password'
alter symmetric key ObjectA
add encryption by certificate RoleC;

open symmetric key ObjectB
decryption by certificate RoleA with password = '123!#Password'
alter symmetric key ObjectB
add encryption by certificate RoleC;

open symmetric key ObjectC
decryption by certificate RoleB with password = '213!#Password'
alter symmetric key ObjectC
add encryption by certificate RoleC;
go

:connect $(server)
use [$(dbname)];
go
create table Objects (
id int not null identity(1,1) primary key,
data varbinary(max));
go

:connect $(server)
use [$(dbname)];
go
-- Role A inserts an Object A and an Object B:
open symmetric key ObjectA
decryption by certificate RoleA with password = '123!#Password'
open symmetric key ObjectB
decryption by certificate RoleA with password = '123!#Password'

insert into Objects (data) values (encryptbykey(Key_GUID('ObjectA'), 'Object A inserted by Role A'));
insert into Objects (data) values (encryptbykey(Key_GUID('ObjectB'), 'Object B inserted by Role A'));
go

:connect $(server)
use [$(dbname)];
go
-- Role B inserts an Object C
open symmetric key ObjectC
decryption by certificate RoleB with password = '213!#Password'

insert into Objects (data) values (encryptbykey(Key_GUID('ObjectC'), 'Object C inserted by Role B'));
go

:connect $(server)
use [$(dbname)];
go
-- Role C inserts objects A, B, C
open symmetric key ObjectA
decryption by certificate RoleC with password = '312!#Password'
open symmetric key ObjectB
decryption by certificate RoleC with password = '312!#Password'
open symmetric key ObjectC
decryption by certificate RoleC with password = '312!#Password'

insert into Objects (data) values (encryptbykey(Key_GUID('ObjectA'), 'Object A inserted by Role C'));
insert into Objects (data) values (encryptbykey(Key_GUID('ObjectB'), 'Object B inserted by Role C'));
insert into Objects (data) values (encryptbykey(Key_GUID('ObjectC'), 'Object C inserted by Role C'));
go

:connect $(server)
use [$(dbname)];
go
-- Role A can see Objects A and B:
open symmetric key ObjectA
decryption by certificate RoleA with password = '123!#Password'
open symmetric key ObjectB
decryption by certificate RoleA with password = '123!#Password'

select id, data, cast(decryptbykey(data) as varchar(max)) as decrypted from Objects ;
go

:connect $(server)
use [$(dbname)];
go
-- Role B can see Object C
open symmetric key ObjectC
decryption by certificate RoleB with password = '213!#Password'

select id, data, cast(decryptbykey(data) as varchar(max)) as decrypted from Objects ;
go


:connect $(server)
use [$(dbname)];
go
-- Role C can see Objects A, B and C
open symmetric key ObjectA
decryption by certificate RoleC with password = '312!#Password'
open symmetric key ObjectB
decryption by certificate RoleC with password = '312!#Password'
open symmetric key ObjectC
decryption by certificate RoleC with password = '312!#Password'

select id, data, cast(decryptbykey(data) as varchar(max)) as decrypted from Objects ;
go

不过,这是一个明智的想法吗?不会。加密永远不会解决访问权限问题。您显然不了解 key 管理和供应,并且您会制造无用的伪安全困惑。很抱歉打扰您的游行,但这是必要的。

关于c# - 如何构建这个允许多个用户/对象的加密系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2844630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com