作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一些时间,并决定实现一次性垫只是为了乐趣和自我教育。现在我最终得到了一个奇怪的数据行为。它让我发疯^^。你能帮我吗?提前致谢。
有一种 encrypt 方法可以作为参数:
InputStream
对于纯文本 OutputStreams
为密文OutputStreams
为 key 。 InputStream
为密文InputStream
用于 key OutputStreams
对于纯文本。 import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.SecureRandom;
public class MyPad {
public static void encrypt(InputStream plainTextInputStream, OutputStream cipherTextOutputStream, OutputStream keyOutputStream) {
int plainTextByte;
SecureRandom random = new SecureRandom();
System.out.println("plain\tkey\tcipher");
try {
while((plainTextByte = plainTextInputStream.read()) != -1){
int keyByte = random.nextInt(256);
int cipherTextByte = (plainTextByte + keyByte) % 256;
System.out.println(plainTextByte + "\t" + keyByte + "\t" + cipherTextByte);
cipherTextOutputStream.write(cipherTextByte);
keyOutputStream.write(keyByte);
}
plainTextInputStream.close();
cipherTextOutputStream.close();
keyOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void decrypt(InputStream cipherTextInputStream, InputStream keyInputStream, OutputStream plainTextOutputStream) {
int cipherTextByte;
System.out.println("plain\tkey\tcipher");
try {
while((cipherTextByte = cipherTextInputStream.read()) != -1){
int keyByte = keyInputStream.read();
int plainTextByte = Math.abs(cipherTextByte - keyByte) % 256;
System.out.println(plainTextByte + "\t" + keyByte + "\t" + cipherTextByte);
plainTextOutputStream.write(plainTextByte);
}
cipherTextInputStream.close();
keyInputStream.close();
plainTextOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String plainText = "This is my plain text.";
InputStream plainTextInputStream = new ByteArrayInputStream(plainText.getBytes());
ByteArrayOutputStream cipherTextOutputStream = new ByteArrayOutputStream();
ByteArrayOutputStream keyOutputStream = new ByteArrayOutputStream();
System.out.println("------------------------------------ encrypting");
encrypt(plainTextInputStream, cipherTextOutputStream, keyOutputStream);
String cipherText = cipherTextOutputStream.toString();
String key = keyOutputStream.toString();
System.out.println("plaintext:\t" + plainText);
System.out.println("ciphertext:\t" + cipherText);
System.out.println("key:\t" + key);
InputStream cipherTextInputStream = new ByteArrayInputStream(cipherText.getBytes());
InputStream keyInputStream = new ByteArrayInputStream(key.getBytes());
ByteArrayOutputStream plainTextOutputStream = new ByteArrayOutputStream();
System.out.println("------------------------------------ decrypting");
decrypt(cipherTextInputStream, keyInputStream, plainTextOutputStream);
plainText = plainTextOutputStream.toString();
System.out.println("plaintext:\t" + plainText);
}
}
最佳答案
有两个问题:
Math.abs(cipherTextByte - keyByte) % 256
是错的。 (0 - 1) (mod 256) = 255 而不是 1。你应该使用 (256 + cipherTextByte - keyByte) % 256
关于java - OneTimePad 实现失败。也许是流问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3721565/
我有一些时间,并决定实现一次性垫只是为了乐趣和自我教育。现在我最终得到了一个奇怪的数据行为。它让我发疯^^。你能帮我吗?提前致谢。 有一种 encrypt 方法可以作为参数: InputStream对
我已经在 pythonanywhere 网站上部署了我的网络应用程序,并且该网站运行正常,但我还创建了 virtualenv我安装我的应用程序包的地方一切正常 但是当我在我的 pyton 文件中导入新
我是一名优秀的程序员,十分优秀!