· 4 years ago · Aug 23, 2021, 09:26 PM
1Skip to content
2Search or jump to…
3Pull requests
4Issues
5Marketplace
6Explore
7
8@SeanOBrien1997
9SeanOBrien1997
10/
11CS211-LABS
120
130
140
15Code
16Issues
17Pull requests
18Actions
19Projects
20Wiki
21Security
22Insights
23Settings
24CS211-LABS/lab0.java /
25@SeanOBrien1997
26SeanOBrien1997 Add files via upload
27Latest commit 72bfe87 on 13 Feb 2019
28 History
29 1 contributor
30140 lines (129 sloc) 4.48 KB
31
32import java.io.FileNotFoundException;
33import java.io.FileOutputStream;
34import java.io.PrintStream;
35import java.math.BigInteger;
36import java.security.MessageDigest;
37import java.security.NoSuchAlgorithmException;
38import java.security.SecureRandom;
39import java.util.ArrayList;
40import java.util.List;
41import java.util.Scanner;
42
43/*
44 * This is steaming trash
45 */
46public class lab0 {
47
48 public static void main(String[] args) throws NoSuchAlgorithmException {
49 SecureRandom rand = new SecureRandom(); // better than regular random
50 // make hex string
51 String[] a = { "A", "B", "C", "D", "E", "F", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
52 String s = "";
53 for (int i = 0; i < 64; i++) {
54 s += a[rand.nextInt(a.length)];
55 }
56 System.out.println("64 Digit HexString: " + s);
57
58 // make eighty string
59 String eighty = "80" + s;
60 System.out.println("Eighty: " + eighty);
61
62 // get doublehash of the 80 string
63 String doublehash = sha256(sha256(eighty));
64 System.out.println("256 of 256 of Eighty: " + doublehash);
65
66 // concat first 8 bits from double hash onto front of 80 string
67 String doubleeighty = eighty;
68 for (int i = 0; i < 8; i++) {
69 doubleeighty += doublehash.charAt(i);
70 }
71 System.out.println("Double Eighty + 8 Hash: " + doubleeighty);
72
73 // compute 58
74 String secret = base58(base10(doubleeighty));
75 System.out.println("Your Bit/Vertcoin private key is: " + secret);
76
77 // optional
78 System.out.println("Output to textfile? (Y/n): ");
79 Scanner sc = new Scanner(System.in);
80 String reply = sc.nextLine().toUpperCase();
81 if (reply.equals("Y")) {
82 try {
83 PrintStream out = new PrintStream(new FileOutputStream("secret.txt"));
84 System.setOut(out);
85 System.out.println(secret);
86 } catch (FileNotFoundException e) {
87 System.out.println("Error outputing key to text file");
88 e.printStackTrace();
89 }
90 }
91 }
92
93 public static String sha256(String input) throws NoSuchAlgorithmException {
94 byte[] in = hexStringToByteArray(input);
95 MessageDigest mDigest = MessageDigest.getInstance("SHA-256");
96 byte[] result = mDigest.digest(in);
97 StringBuffer sb = new StringBuffer();
98 for (int i = 0; i < result.length; i++) {
99 sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
100 }
101 return sb.toString();
102 }
103
104 public static byte[] hexStringToByteArray(String s) {
105 int len = s.length();
106 byte[] data = new byte[len / 2];
107 for (int i = 0; i < len; i += 2) {
108 data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
109 }
110 return data;
111 }
112
113 public static String base58(BigInteger bi) {
114 List<BigInteger> list = new ArrayList<BigInteger>();
115 while (!bi.equals(BigInteger.ZERO)) {
116 list.add(bi.mod(BigInteger.valueOf(58)));
117 bi = bi.divide(BigInteger.valueOf(58));
118 }
119 String[] alphabet = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "J",
120 "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f",
121 "g", "h", "i", "j", "k", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
122 String result = "";
123 for (int i = list.size() - 1; i >= 0; i--) {
124 result += alphabet[(int) list.get(i).longValue()];
125 }
126 return result;
127 }
128
129 public static BigInteger base10(String s) {
130 BigInteger bi = BigInteger.valueOf(0);
131 int count = 0;
132 for (int i = s.length() - 1; i >= 0; i--) {
133 BigInteger total = BigInteger.valueOf(16);
134 String currentNum = "" + s.charAt(i);
135 try {
136 long num = Long.parseLong(currentNum);
137 total = (total.pow(count).multiply(BigInteger.valueOf(num)));
138 bi = bi.add(total);
139 } catch (Exception e) {
140 switch (currentNum.toUpperCase()) {
141 case "A":
142 total = (total.pow(count).multiply(BigInteger.valueOf(10)));
143 bi = bi.add(total);
144 break;
145 case "B":
146 total = (total.pow(count).multiply(BigInteger.valueOf(11)));
147 bi = bi.add(total);
148 break;
149 case "C":
150 total = (total.pow(count).multiply(BigInteger.valueOf(12)));
151 bi = bi.add(total);
152 break;
153 case "D":
154 total = (total.pow(count).multiply(BigInteger.valueOf(13)));
155 bi = bi.add(total);
156 break;
157 case "E":
158 total = (total.pow(count).multiply(BigInteger.valueOf(14)));
159 bi = bi.add(total);
160 break;
161 case "F":
162 total = (total.pow(count).multiply(BigInteger.valueOf(15)));
163 bi = bi.add(total);
164 break;
165 }
166 }
167 count++;
168 }
169 return bi;
170 }
171}
172© 2021 GitHub, Inc.
173Terms
174Privacy
175Security
176Status
177Docs
178Contact GitHub
179Pricing
180API
181Training
182Blog
183About
184Loading complete