【发布时间】: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,n和k到另一边,然后假设读取另一边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