📄 onetimenoclist.java
字号:
package net.jtank.protocol;
import javax.swing.JFrame;
import java.util.Random;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
/**
* Uses a form of One-Time Pad Encryption to digitally encrypt textual data.
*
* Encrypts data using a one-time cyper system.
* Theorecically unbreakable if a totally random key is used (ie, one made up by throwing a dice etc);
* Using the randomly generated key isn't as safe, because java uses a pseudo-random number generator
* meaning they look random, but actually are not.
*
*
*@author William Denniss
*
* Versions:
*
* 04 Jan 2002: v1.0
* - Command Line with full functionality
*
* 05 Jan 2002: v1.1
* - Basic GUI added
*
* 13 Mar 2002: v1.2
* - New much improved GUI
*
* 05 Apr 2002: v1.3
* - GUI improved, but separated from this file. This class vastly improved to make it more abstract
*
* William Denniss has asserted his right under the Australian Copyright, Designs and Patents Act 1988 to be identified as the author of this work.
*/
public class OneTimeNOCList {
public String file1 = "text.txt";
public String file2 = "code1.txt";
public String file3 = "code2.txt";
public boolean encrypt = true;
public static void encrypt (BufferedReader brPlain, BufferedReader bwKey, BufferedWriter bwEnc) throws IOException{
//int currentChar = 0;
char [] encrypted = new char [2];
String line = brPlain.readLine();
String keyLine = bwKey.readLine();
while (line != null && keyLine != null) {
for (int i = 0; i < line.length(); i++) {
char currentChar = line.charAt(i);
char currentKey = keyLine.charAt(i);
if ( currentChar>= 32) {
encrypted = encryptChar ( (char) currentChar, currentKey);
bwEnc.write(encrypted[0]);
} else {
bwEnc.write( (char) currentChar);
}
}
line = brPlain.readLine();
keyLine = bwKey.readLine();
if (line != null) {
bwEnc.write( '\n');
}
}
//bw1.close();
bwEnc.close();
bwKey.close();
brPlain.close();
}
public static void decrypt (BufferedWriter bw, BufferedReader br1, BufferedReader br2) throws IOException {
//int currentChar = 0;
char [] encrypted = new char [2];
String line1 = br1.readLine();
String line2 = br2.readLine();
while (line1 != null) {
for (int i = 0; i < line1.length(); i++) {
encrypted[0] = line1.charAt(i);
encrypted[1] = line2.charAt(i);
if ( encrypted[0] >= 32) {
// encrypted = encryptChar ( (char) currentChar);
bw.write(decryptChar(encrypted));
//pw2.print(encrypted[1]);
} else {
bw.write(encrypted[0]);
}
}
line1 = br1.readLine();
line2 = br2.readLine();
if (line1 != null) {
bw.write( '\n');
}
}
bw.close();
}
public static String generateRandomKey (String toEncode) {
String toReturn = "";
for (int i = 0; i < toEncode.length(); i++) {
toReturn += (char) (randomChar() + 32);
}
//return (toReturn);
return formatString(toReturn, toEncode);
}
public static String generateRandomKey (String toEncode, String password, int method) {
if (method == 0) {
long hashnumber = 0;
for (int i = 0; i < password.length(); i++) {
hashnumber *= 10;
hashnumber += password.charAt(i);
}
//System.out.println(hashnumber);
long bigNumber = Long.parseLong("99736599483849");
hashnumber *= bigNumber;
//System.out.println(hashnumber);
hashnumber = Math.abs(hashnumber ^ (int) ((hashnumber * 10) /2));
//System.out.println(hashnumber);
long seednumber = (int) (hashnumber / 10000000);
long offsetnumber = hashnumber % 10000000;
//System.out.println(seednumber + " - " + offsetnumber);
return generateRandomKey(toEncode, seednumber, offsetnumber);
} else {
return null;
}
}
public static String generateRandomKey (String toEncode, long seed, long offset) {
Random numbergenerator = new Random (seed);
for (int i = 0; i < offset; i++) {
numbergenerator.nextLong();
}
String toReturn = "";
for (int i = 0; i < toEncode.length(); i++) {
char randomChar = (char) numbergenerator.nextLong();
randomChar %= (127-32);
randomChar += 32;
toReturn += randomChar;
}
return formatString(toReturn, toEncode);
}
public static String formatString (String toFormat, String formatTo) {
String toReturn = "";
for (int i = 0; i < formatTo.length(); i++) {
if (formatTo.charAt(i) == '\n')
toReturn += '\n';
else
toReturn += toFormat.charAt(i);
}
return toReturn;
}
public static char randomChar () {
char randomOffset = (char) (Math.random() * 1000);
randomOffset %= (127-32);
return randomOffset;
}
public static char [] encryptChar(char toencrypt) {
char randomOffset = randomChar();
return encryptChar(toencrypt, (char) (randomOffset+32));
}
public static char [] encryptChar(char toencrypt, char key) {
if (toencrypt == '\n')
return new char [] {'\n','\n'};
char [] encrypted = new char [2];
char randomOffset = (char) ((int) key - 32);
//Basic ASCII
if (toencrypt < 0x7F) {
encrypted[0] = toencrypt;
encrypted[1] = (char) (randomOffset + 32); //store random index
encrypted[0] -= 32; //cut first 32 ASCII characters
encrypted[0] += randomOffset; //add the offset
encrypted[0] %= 127-32; //mod it so it wraps around
encrypted[0] += 32; //increase it back to where it should be
//Extended ASCII
} else if (toencrypt < 0xFF) {
randomOffset ++;
encrypted[0] = toencrypt;
encrypted[1] = (char) (randomOffset + 32 +127);
encrypted[0] -= 32+128;
encrypted[0] += randomOffset;
encrypted[0] %= 127-32;
encrypted[0] += +32+128;
//if (!(encrypted[0] >= 160 && encrypted[0] <= 254))
// System.out.println(encrypted[0]+0);
//if (!(encrypted[1] >= 160 && encrypted[1] <= 254))
// System.out.println(encrypted[1]+0+"r");
//not supported
} else {
}
return encrypted;
}
public static char decryptChar(char todecrypt []) {
if (todecrypt[0] == '\n')
return '\n';
int decrypted = 0;
int randomOffset;
//Basic ASCII
if (todecrypt[0] < 0x7F) {
decrypted = todecrypt[0];
randomOffset = (char) (todecrypt[1] - 32);
decrypted -= 32;
decrypted -= randomOffset;
if (decrypted < 0)
decrypted += (127-32);
decrypted += 32;
//Extended ASCII
} else if (todecrypt[0] < 0xFF) {
decrypted = todecrypt[0];
randomOffset = (char) (todecrypt[1] - 32 -127);
decrypted -= 32 + 127;
decrypted -= randomOffset;
if (decrypted < 1)
decrypted += (127-32);
decrypted += 32 +127;
//if (!(decrypted >= 160 && decrypted <= 254))
// System.out.println(decrypted+0);
//not supported
} else {
}
return (char) decrypted;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -