Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class SHA256 {
- public static char[] CurrentBinaryString;
- public static char[] PreviousBinaryString;
- public static char[] nameToCharArr;
- public static int currentDEC;
- public static char currentASCII;
- public static void binToDec() {
- String currBIN = String.valueOf(CurrentBinaryString);
- currentDEC = Integer.parseInt(currBIN, 2);
- if (currentDEC < 33 || currentDEC > 126) {
- Random random = new Random();
- int min = 33;
- int max = 127;
- currentDEC = random.nextInt(max - min + 1) + min;
- }
- System.out.println(currentDEC);
- }
- public static void decToASCII() {
- currentASCII = (char) currentDEC;
- }
- public static void ASCIIToNameCharArr(int index) {
- nameToCharArr[index] = currentASCII;
- }
- public static void XOR() {
- for (int i = 0; i < CurrentBinaryString.length; i++) {
- if (CurrentBinaryString[i] == PreviousBinaryString[i]) {
- CurrentBinaryString[i] = '0';
- } else {
- CurrentBinaryString[i] = '1';
- }
- }
- }
- public static void RORandSHR(boolean rorOrshr, int rotations) {
- if (!rorOrshr) {
- for (int i = rotations; i > 0; i--) {
- char last = CurrentBinaryString[CurrentBinaryString.length - 1];
- for (int j = CurrentBinaryString.length - 1; j > 0; j--) {
- CurrentBinaryString[j] = CurrentBinaryString[j - 1];
- }
- CurrentBinaryString[0] = last;
- }
- } else {
- for (int i = rotations; i > 0; i--) {
- for (int j = CurrentBinaryString.length - 1; j > 0; j--) {
- CurrentBinaryString[j] = CurrentBinaryString[j - 1];
- }
- CurrentBinaryString[0] = '0';
- }
- }
- }
- public static void addBytes() {
- char ele = '0';
- int index = 0;
- char[] tempArr = new char[CurrentBinaryString.length + 1];
- int j = 0;
- for (int i = 0; i < tempArr.length; i++) {
- if (i == index) {
- tempArr[i] = ele;
- } else {
- tempArr[i] = CurrentBinaryString[j];
- j++;
- }
- }
- CurrentBinaryString = tempArr;
- }
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- System.out.println("Input string: ");
- String Name = scan.nextLine();
- nameToCharArr = Name.toCharArray();
- int arrLength = nameToCharArr.length;
- int index = 0;
- for (int i = 0; i < arrLength; i++) {
- int ascII = nameToCharArr[i];
- PreviousBinaryString = new char[arrLength];
- CurrentBinaryString = new char[arrLength];
- CurrentBinaryString = Integer.toBinaryString(ascII).toCharArray();
- PreviousBinaryString = Arrays.copyOf(CurrentBinaryString, arrLength);
- if (CurrentBinaryString.length < 8) {
- while (CurrentBinaryString.length < 8) {
- addBytes();
- }
- }
- int rotations = 13;
- PreviousBinaryString = Arrays.copyOf(CurrentBinaryString, CurrentBinaryString.length);
- RORandSHR(false, rotations);
- XOR();
- PreviousBinaryString = Arrays.copyOf(CurrentBinaryString, CurrentBinaryString.length);
- rotations = 7;
- RORandSHR(false, rotations);
- XOR();
- int shifts = 3;
- RORandSHR(true, shifts);
- binToDec();
- decToASCII();
- ASCIIToNameCharArr(index);
- index++;
- System.out.println(Arrays.toString(CurrentBinaryString));
- System.out.println(currentASCII);
- }
- System.out.println(Arrays.toString(nameToCharArr));
- }
- }
Add Comment
Please, Sign In to add comment