【问题标题】:How do I write Objects through a ObjectOutputStream, when the Objects are created in a different class?当在不同的类中创建对象时,如何通过 ObjectOutputStream 编写对象?
【发布时间】:2015-05-31 20:50:48
【问题描述】:

我试图总结我的问题,但实际上还有更多问题。对于 uni 的任务,我们应该创建两个构造函数(英语不是我的第一语言)

1) public SecureInputStream(InputStream base, OutputStream remote)

2) public SecureOutputStream(OutputStream base, InputStream remote)

1) 应该创建 BigIntegers g,n,k 然后生成一个公钥 K=g^k mod n,然后通过远程发送g,nk到另一边,然后假设读取另一边k并创建秘密s= k^k mod n并用它启动随机发生器.我应该覆盖 write(int b),所以 write 将加密 s,使用 BigInteger 类中的 xor 方法,然后通过远程将其发送到另一端。

2) 应该等待来自 1) 的 g.n,创建它自己的 k,然后通过远程将其发送回来。构造函数还将使用秘密 s 启动随机生成器

我们的老师给了我们两个“简单”的客户端和服务器类,它们使用了 Socket,Port Situation。我的问题是,每个类都有自己的输入/输出流,可以读/写一些东西。我如何进入其中,更重要的是,一旦读取信息,发送的信息到底在哪里?这两天我所做的就是在线阅读和观看教程。我肯定明白什么是流,虽然我不能真正理解它们是如何工作的。

如何通过该流发送多个内容?对方怎么知道我发的是哪个?一般怎么寄东西?

我希望有人可以在这里帮助我。好吧,这是我到目前为止的代码,主要方法来自我们的导师。想象一下,我做了所有的导入。

//Error Method: Invalid method declaration, return type missing,
//I'm guessing the constructor needs to sit in a class called 
//SecureInputStream, but how can I use the stuff in the class Client
//when the constructor I need is in another?

public SecureInputStream(InputStream base, OutputStream remote) {
    super();
    //g,n,k,s are created 
    BigInteger g = BigInteger.probablePrime(1024, new SecureRandom());
    BigInteger n = BigInteger.probablePrime(1024, new SecureRandom());
    BigInteger k = BigInteger.probablePrime(1024, new SecureRandom());
    BigInteger s = BigInteger.probablePrime(1024, new SecureRandom());
    Random r = new Random();
    r.nextInt();
    BigInteger exponent1 = new BigInteger("r");
    BigInteger exponent2 = new BigInteger("k");
    k = g.modPow(exponent1, n);
    s = k.modPow(exponent2, n);

}

public static void main(String... args) {

    Socket server = null;

    try {
        // Connect to server on local machine ("localhost") and port 3145.
        server = new Socket("localhost", 3145);

        // Get input stream from server and read its message
        Scanner in = new Scanner(server.getInputStream());
        // If we need to send messages to the server:
        // OutputStream out = server.getOutputStream();

        while (in.hasNext()) {
            System.out.println(in.nextLine());
        }
        in.close();

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (server != null) {
                server.close();
            }
        } catch (IOException e) {

        }
    }
}

【问题讨论】:

  • 有趣的事实:每次程序员写一个空的catch子句,熊猫就会死。
  • 只是一个快速提示 - 查看 Java BufferedinputStream/BufferdOutputStream 的源代码。 If 不能解决您的练习,但它演示了装饰器模式。这就是您“在”现有流之间“获取”的方式……您没有!你把它们包起来。例如,您服务器的基本 OutputStream 在传输原始字节方面做得很好。就这样吧。只需用一个安全的输出流包装它,该输出流将接收数据写入 write(),将其转换为加密的原始字节,然后将这些原始字节推送到基本流中。
  • (续)还有一些我没有时间仔细考虑的细节。例如。您可能在构造函数上执行的初始握手(密钥交换),或者可能在第一次读/写时懒惰地执行。例如我不确定为什么您的安全流会收到 2 个构造函数参数(inputStream 和 OutputStream)...

标签: java encryption inputstream outputstream


【解决方案1】:

如何通过ObjectOutputStream,写对象

你打电话给ObjectOutputStream.writeObject().

  • 您没有问的第二个问题:如何通过ObjectInputStream 读取对象?

你打电话给ObjectInputStream.readObject().

  • 您没有问的第三个问题:如何发现传入对象的类型?

您可以通过instanceof 运算符发现接收到的对象的类型。但是对于大多数协议,包括您描述的协议,您不需要这样做。协议通常定义了什么时候发送,所以你所要做的就是相应地阅读。

什么时候在不同的类中创建对象?

创建对象的位置没有任何区别。

【讨论】:

  • ... 除非它们是在不同的 Java 运行时创建的。运行时中的类需要兼容。就我个人而言,我宁愿想出一个协议并使用BigInteger.toByteArray()。但是,对于分配,对象流就足够了。
  • @MaartenBodewes 问题是“当在不同的类中创建对象时,如何通过 ObjectOutputStream 编写对象?”要写入ObjectOutputStream 的对象必须已在同一个 JVM 中创建。
  • 嗯,当然。只是对象流应该由兼容的运行时读取。我并没有使答案无效,我只是添加了一些警告,以防有人将它用于通用协议 - 对象通过套接字发送,谁知道它们最终会在哪里。
猜你喜欢
  • 2018-03-17
  • 1970-01-01
  • 1970-01-01
  • 2021-08-16
  • 1970-01-01
  • 1970-01-01
  • 2020-11-18
  • 1970-01-01
  • 2016-07-17
相关资源
最近更新 更多