Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.security.*;
- import java.security.spec.*;
- import javax.crypto.*;
- import java.lang.*;
- import java.io.*;
- import java.math.*;
- /*
- * Greatest thanks for this example go to the address mentioned below:
- */
- // We're following http://www.javamex.com/tutorials/cryptography/asymmetric.shtml
- // to show how to do RSA enc/dec
- public class RSA {
- private static final String algorithm = "RSA";
- // 64 bytes
- private static final String msgChecksum = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
- // Unlike in the original example, we want a TEXT file to actually SEE what it stores
- private static void saveToFile(String fileName, BigInteger mod, BigInteger exp) throws IOException {
- BufferedWriter out = new BufferedWriter(new FileWriter(fileName));
- out.write(mod.toString());
- out.newLine();
- out.write(exp.toString());
- out.newLine();
- out.close();
- }
- private static BigInteger[] readTwoNumbersFromFile(String fileName) throws IOException {
- BigInteger[] ret = new BigInteger[2];
- BufferedReader in = new BufferedReader(new FileReader(fileName));
- ret[0] = new BigInteger(in.readLine());
- ret[1] = new BigInteger(in.readLine());
- in.close();
- return ret;
- }
- public static void main(String[] args) throws Exception {
- if(args.length == 0) {
- System.out.println("Usage: $0 1|2|3");
- System.out.println("1 generate keys");
- System.out.println("2 encrypt msg-checksum with endor key");
- System.out.println("3 decrypt msg-checksum with gbz key");
- System.out.println("4 AnDenNutzer proposal");
- return;
- }
- switch(args[0].charAt(0)) {
- case '1': {
- // Generate keys
- KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
- kpg.initialize(2048);
- KeyPair kp = kpg.genKeyPair();
- Key publicKey = kp.getPublic();
- Key privateKey = kp.getPrivate();
- // Store keys to file
- KeyFactory fact = KeyFactory.getInstance("RSA");
- RSAPublicKeySpec pub = fact.getKeySpec(kp.getPublic(), RSAPublicKeySpec.class);
- RSAPrivateKeySpec priv = fact.getKeySpec(kp.getPrivate(), RSAPrivateKeySpec.class);
- saveToFile("public.key", pub.getModulus(), pub.getPublicExponent());
- saveToFile("private.key", priv.getModulus(), priv.getPrivateExponent());
- // Actually, this is it!
- break;
- }
- case '2': {
- // Encrypt "msgChecksum" field with the "public.key" file (later this file shall NOT be distributed as it is THE important file... in the AnDenNutzer layout, endor actually plays the role of a client as he connects to gbz... or s04... that have a server (started with --client) running)
- // We'll later store our key files in setting-fields of our INI
- // Read data from public.key
- BigInteger[] numbers = readTwoNumbersFromFile("public.key");
- RSAPublicKeySpec keySpec = new RSAPublicKeySpec(numbers[0], numbers[1]);
- KeyFactory fact = KeyFactory.getInstance("RSA");
- PublicKey pubKey = fact.generatePublic(keySpec);
- // Actual encryption
- Cipher cipher = Cipher.getInstance("RSA");
- cipher.init(Cipher.ENCRYPT_MODE, pubKey);
- byte[] cipherData = cipher.doFinal(msgChecksum.getBytes());
- // Write to file
- BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("msg_checksum_encrypted.bin"));
- out.write(cipherData);
- out.close();
- break;
- }
- case '3': {
- // Decrypt
- // modulus, exponent (reihenfolge!)
- BigInteger[] numbers = readTwoNumbersFromFile("private.key");
- RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(numbers[0], numbers[1]);
- KeyFactory fact = KeyFactory.getInstance("RSA");
- PrivateKey privateKey = fact.generatePrivate(keySpec);
- // Actual decryption
- Cipher cipher = Cipher.getInstance("RSA");
- cipher.init(Cipher.DECRYPT_MODE, privateKey);
- BufferedInputStream in = new BufferedInputStream(new FileInputStream("msg_checksum_encrypted.bin"));
- byte[] buffer = new byte[0x100];
- int len;
- while ((len = in.read(buffer)) > 0) {
- cipher.update(buffer, 0, len);
- }
- System.out.println(new String(cipher.doFinal()));
- break;
- }
- case '4': {
- // AnDenNutzer Variante
- // TODO PROBLEM BEI DER GANZEN SACHE: pwd-exponent auf dem Server ist zu leicht zu erraten (weil Standardwert) -- wir müssen auf dem Server sichere Werte als auf dem Client haben, aber der Client ist derjenige, der entschlüsseln muss.
- System.out.println("Generation in progress... Please wait a while...");
- KeyPairGenerator kpg = KeyPairGenerator.getInstance(algorithm);
- kpg.initialize(0x800);
- KeyPair kp = kpg.genKeyPair();
- KeyFactory fact = KeyFactory.getInstance(algorithm);
- RSAPublicKeySpec pub = fact.getKeySpec(kp.getPublic(), RSAPublicKeySpec.class);
- RSAPrivateKeySpec priv = fact.getKeySpec(kp.getPrivate(), RSAPrivateKeySpec.class);
- System.out.println("done.");
- System.out.println();
- System.out.println("Configuration for your server (that you invoke via --client=CLIENT MSG)");
- System.out.println("pwd-modulus=");
- printIndentedBigInteger(pub.getModulus());
- System.out.println("pwd-exponent=");
- printIndentedBigInteger(pub.getPublicExponent());
- System.out.println();
- System.out.println("Configuration for your client (that you invoke via --client)");
- System.out.println("pwd-modulus=");
- printIndentedBigInteger(priv.getModulus());
- System.out.println("pwd-exponent=");
- printIndentedBigInteger(priv.getPrivateExponent());
- System.out.println();
- System.out.println("Remember that you will need a different ini on server and client!");
- System.out.println("The numbers are quite large... you may want to redirect them into a file.");
- System.out.println("You will also have to remove the newlines and spaces between the numbers.");
- }
- default: {
- System.out.println("Use no param for help.");
- }
- }
- }
- private static void printIndentedBigInteger(BigInteger integer) {
- char[] chrData = integer.toString().toCharArray();
- char[] buffer = new char[79];
- int pos = 0;
- for(int i = 0; i < chrData.length; i++) {
- if(pos == 0) {
- pos = 8;
- for(int j = 0; j < 8; j++) {
- buffer[j] = ' ';
- }
- }
- buffer[pos++] = chrData[i];
- if(pos > 78) {
- System.out.println(new String(buffer));
- pos = 0;
- }
- }
- StringBuffer lastLine = new StringBuffer("");
- for(int i = 0; i <= pos; i++) {
- lastLine.append(buffer[i]);
- }
- System.out.println(lastLine);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement