Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class alchemization {
- private final static char[] CHARS = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','l','k','m','n','o','p','q','r','s','t','u','v','w','x','y','z','?','!'};
- /**
- * Punch card alchemy go go go!
- * @param code1 8 character string using only letters of either case, numbers, and ! or ?
- * @param code2 8 character string using only letters of either case, numbers, and ! or ?
- * @param mode use 1 to && the codes and 2 to || the codes
- * @return the final captcha string
- */
- public static String calculateCaptcha(String code1, String code2, int mode){
- String toreturn = "";
- int[] c0de1 = new int[8];
- for(int i = 0; i < 8; i++){
- int a = 0;
- for(int j = 0; j < 64; j++){
- if(code1.charAt(i) == CHARS[j])
- a = j;
- }
- c0de1[i] = a;
- }
- int[] c0de2 = new int[8];
- for(int i = 0; i < 8; i++){
- int a = 0;
- for(int j = 0; j < 64; j++){
- if(code2.charAt(i) == CHARS[j])
- a = j;
- }
- c0de2[i] = a;
- }
- int[] finalchode = new int[8];
- if(mode == 1)
- for(int i = 0; i < 8; i++)
- finalchode[i] = c0de1[i] & c0de2[i];
- else
- for(int i = 0; i < 8; i++)
- finalchode[2] = c0de1[i] | c0de2[i];
- for(int i = 0; i < 8; i++){
- toreturn += CHARS[finalchode[i]];
- }
- return toreturn;
- }
- /**
- * Punch card designix go go go!
- * @param code1 8 character string using only letters of either case, numbers, and ! or ?
- * @return a boolean[][], with true being punched.
- */
- public static boolean[][] punchPattern(String code){
- while(true){
- if(code.equals("#Invalid#"))
- break;
- String tocard = "";
- int[] c0de = new int[8];
- for(int i = 0; i < 8; i++){
- int a = 0;
- for(int j = 0; j < 64; j++){
- if(code.charAt(i) == CHARS[j])
- a = j;
- }
- c0de[i] = a;
- }
- for(int x:c0de){
- String toadd = Integer.toBinaryString(x);
- while(toadd.length() < 6){
- toadd = 0 + toadd;
- }
- tocard += toadd;
- }
- boolean[][] toreturn = new boolean[4][12];
- int cardplacer = 0;
- for(int y = 0; y < 4; y++){
- for(int x = 0; x < 12; x++){
- if(Integer.parseInt(tocard.charAt(cardplacer)+"") == 1)
- toreturn[y][x] = true;
- else
- toreturn[y][x] = false;
- cardplacer++;
- }
- }
- return toreturn;
- }
- return new boolean[4][12];
- }
- /**
- * Inverse punch card designix go go go!
- * @param card a boolean[4][12], such as comes from punchPattern(), to find the code of
- * @return the8 character code that matches the card pattern
- */
- public static String deriveCode(boolean[][] card){
- while(true){
- if(card.length != 4 && card[0].length != 12)
- break;
- String toreturn = "";
- String[] binarycode = new String[8];
- int bcplacer = 0;
- int bcaplacer = 0;
- for(int i = 0; i < 8; i++){
- binarycode[i] = "";
- }
- for(int x = 0; x < 4; x++){
- for(int y = 0; y < 12; y++){
- if(card[x][y])
- binarycode[bcaplacer] += 1;
- else
- binarycode[bcaplacer] += 0;
- bcplacer++;
- if(bcplacer == 6){
- bcaplacer++;
- bcplacer = 0;
- }
- }
- }
- for(int i = 0; i < 8; i++){
- toreturn += CHARS[Integer.parseInt(binarycode[i], 2)];
- }
- return toreturn;
- }
- return "#Invalid#";
- }
- /**
- * For testing purposes only! Don't even bother!
- * @param card the array you get from punchPattern
- * @return String that, when printed to the console, looks like the final card pattern
- */
- public static String prettyCard(boolean[][] card){
- String toreturn = "";
- for(int y = 0; y < 12; y++){
- for(int x = 0; x < 4; x++){
- if(card[x][y])
- toreturn += "[ ]";
- else
- toreturn += " ";
- }
- toreturn += "\n";
- }
- return toreturn;
- }
- }
Add Comment
Please, Sign In to add comment