gpt4 book ai didi

java - 在java中加密泛型类型

转载 作者:行者123 更新时间:2023-12-02 10:20:56 25 4
gpt4 key购买 nike

我在处理 java 8 中的泛型类型时遇到一些问题。在我正在从事的一个项目中,我有这 3 类

public class User{
private String Id;
private String password;
}

public class Data<E>{
private <E> data;
private String Owner; //can read and write the data
private Vector<String> Others; //can only read the data
}

public class MySecureDataContainer<E>{
Vector<User> users;
Vector<Data<E>> data;

public share(String Id, String passw, E data, String Other);
}

我刚刚在这里写下了我遇到问题的元素。这个想法很简单:MySecureDataContainer 类在本地工作,没有线程或套接字。 vector 的每个用户一旦注册,就可以创建数据、修改数据、读取他创建的所有数据、与其他用户共享数据。每个操作仅在用户“登录”时才有效。

该项目的任务之一是加密每个用户创建的所有通用数据。所以我有两个问题:1)如何加密通用类型? (想法是使用密码作为 key )2)如果数据被加密,我如何与其他用户共享?而且,如果其他用户只能读取数据而不知道所有者的密码,那么他如何才能读取数据呢?我可以在类中添加变量,但无法在方法中添加变量

最佳答案

1) how can i encrypt a generic type? (the idea is to use the password as a key)

正如已经评论过的,加密是在字节数组之上工作的。您可能仍然有办法将数据转换为字节数组并返回,例如通过 Java 序列化、XML、JSON、EDI 等等。这超出了本答案的范围。一旦有了序列化和反序列化的方法,就可以对序列化(字节数组)形式进行加密或解密

使用用户的凭据密码来加密数据本身可能不是最好的主意。用户可能不想传递其密码。您可以使用随机加密 key ,然后使用某种非对称加密来传递加密 key 。

2) if the data is encrypted, how can i share it with another user? and, plus it, how can another user read the data, if he can only read it, without knowing the password of the owner?

事实上,RSA(或其他非对称加密)允许将数据发送给不同的用户,而无需交换用户的私钥或密码本身。

I can add variables in the classes, but i can not add variables into the method

你可以使用方法变量,但它们是方法范围内的变量,而不是对象属性

关于java - 在java中加密泛型类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54344529/

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