Skip to content

Instantly share code, notes, and snippets.

@odds-get-evened
Last active November 5, 2021 08:12
Show Gist options
  • Save odds-get-evened/1ddb6f23b1763680766424056864c40a to your computer and use it in GitHub Desktop.
Save odds-get-evened/1ddb6f23b1763680766424056864c40a to your computer and use it in GitHub Desktop.
quick script to take a string, convert to byte array, and make each array have a predetermined length.
import io.leonard.Base58;
import org.apache.commons.codec.binary.Hex;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.util.Arrays;
/**
* quick script to take a string, convert to byte array,
* and make each array have a predetermined length.
*
*/
class StringBufferScratch {
public static void main(String[] args) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
String s = "Spaceflight will never tolerate carelessness, incapacity, and neglect. Somewhere, somehow, we screwed up. It could have been in design, build, or test. Whatever it was, we should have caught it. We were too gung ho about the schedule and we locked out all of the problems we saw each day in our work.\n" +
"\n" +
"\n" +
"“Every element of the program was in trouble and so were we. The simulators were not working, Mission Control was behind in virtually every area, and the flight and test procedures changed daily. Nothing we did had any shelf life. Not one of us stood up and said, ‘Dammit, stop!’ I don’t know what Thompson’s committee will find as the cause, but I know what I find. We are the cause! We were not ready! We did not do our job. We were rolling the dice, hoping that things would come together by launch day, when in our hearts we knew it would take a miracle. We were pushing the schedule and betting that the Cape would slip before we did.\n" +
"\n" +
"\n" +
"“From this day forward, Flight Control will be known by two words: ‘Tough’ and ‘Competent.’ Tough means we are forever accountable for what we do or what we fail to do. We will never again compromise our responsibilities. Every time we walk into Mission Control we will know what we stand for. Competent means we will never take anything for granted. We will never be found short in our knowledge and in our skills. Mission Control will be perfect.\n" +
"\n" +
"\n" +
"When you leave this meeting today you will go to your office and the first thing you will do there is to write ‘Tough and Competent’ on your blackboards. It will never be erased. Each day when you enter the room these words will remind you of the price paid by Grissom, White, and Chaffee. These words are the price of admission to the ranks of Mission Control.";
byte[] b = s.getBytes(StandardCharsets.UTF_8);
final int bufferLength = 64;
// add +1 for possible remainder chars
byte[][] rows = new byte[b.length / bufferLength+1][bufferLength];
int k = 0;
for(int i=0; i<b.length; i++) {
int j = i%bufferLength;
//System.out.println(String.format("row: %d | index: %d", k, j));
rows[k][j] = b[i];
if(j == bufferLength-1) k++;
}
KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
KeyPair pair = gen.generateKeyPair();
PrivateKey privateKey = pair.getPrivate();
PublicKey publicKey = pair.getPublic();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
Arrays.stream(rows).map(row -> littleEndian(cipherIt(row, cipher))).forEach(row -> {
System.out.println(Hex.encodeHexString(row));
});
}
public static byte[] cipherIt(byte[] message, Cipher cipher) {
try {
return cipher.doFinal(message);
} catch (IllegalBlockSizeException | BadPaddingException e) {
e.printStackTrace();
}
return "shit!".getBytes(StandardCharsets.UTF_8);
}
private static byte[] littleEndian(byte[] data) {
ByteBuffer bb = ByteBuffer.allocate(data.length);
bb.order(ByteOrder.LITTLE_ENDIAN);
bb.put(data);
return bb.array();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment