· 7 years ago · May 20, 2018, 10:06 PM
1/*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6package jpd;
7
8import java.io.BufferedReader;
9import java.io.File;
10import java.io.FileInputStream;
11import java.io.IOException;
12import java.io.InputStreamReader;
13import java.io.UnsupportedEncodingException;
14import java.nio.charset.StandardCharsets;
15import java.nio.file.Files;
16import java.nio.file.Paths;
17import java.security.MessageDigest;
18
19/**
20 *
21 * @author
22 */
23public class JPD {
24
25 /**
26 * @param args the command line arguments
27 */
28 public static void main(String[] args) {
29 // TODO code application logic here
30
31
32 AES testaes = new AES();
33
34
35
36 String str="";
37 String hudson_str="";
38 try{
39 hudson_str = new String(Files.readAllBytes(Paths.get("/home/hudson.util.Secret")), StandardCharsets.UTF_8);
40 System.out.println("Contents (Java 7 with character encoding ) : " + hudson_str);
41
42
43 }
44 catch (UnsupportedEncodingException e)
45 {
46 System.out.println(e.getMessage());
47 }
48 catch (IOException e)
49 {
50 System.out.println(e.getMessage());
51 }
52 catch (Exception e)
53 {
54 System.out.println(e.getMessage());
55 }
56
57 String str2="";
58 String master_key ="";
59 try {
60 File fileDir = new File("/home/master.key");
61
62 BufferedReader in = new BufferedReader(
63 new InputStreamReader(
64 new FileInputStream(fileDir), "UTF8"));
65
66
67
68 while ((str2 = in.readLine()) != null) {
69 //System.out.println(str);
70 master_key += str2;
71 }
72
73 in.close();
74 }
75 catch (UnsupportedEncodingException e)
76 {
77 System.out.println(e.getMessage());
78 }
79 catch (IOException e)
80 {
81 System.out.println(e.getMessage());
82 }
83 catch (Exception e)
84 {
85 System.out.println(e.getMessage());
86 }
87
88
89
90 String hudson_secret_key= "";
91 String hashed_master = "";
92 String encodedhashstring="";
93
94 String encryptedstring = "";
95
96
97 try{
98 MessageDigest digest = MessageDigest.getInstance("SHA-256");
99 byte[] encodedhash = digest.digest(master_key.getBytes(StandardCharsets.UTF_8));
100
101 System.out.println("hashed_master_key in HEX: "+ encodedhashstring);
102 byte[] encodedhash2 = encodedhashstring.getBytes(StandardCharsets.UTF_8);*/
103 byte[] encodedhash2 = new byte[16];
104 for (int i = 0; i < 16; i++) {
105 encodedhash2[i] = encodedhash[i];
106 }
107 String s = encodedhash2.toString();
108 System.out.println(hudson_str);
109 System.out.println(master_key);
110 testaes.decrypt(hudson_str, encodedhashstring);
111
112
113 } catch(Exception ex){
114 throw new RuntimeException(ex);
115 }
116
117
118
119 }
120
121
122
123
124}
125
126
127
128
129/*
130 * To change this license header, choose License Headers in Project Properties.
131 * To change this template file, choose Tools | Templates
132 * and open the template in the editor.
133 */
134package jpd;
135import java.io.UnsupportedEncodingException;
136import java.security.MessageDigest;
137import java.security.NoSuchAlgorithmException;
138import java.util.Arrays;
139import java.util.Base64;
140
141import javax.crypto.Cipher;
142import javax.crypto.SecretKey;
143import javax.crypto.spec.SecretKeySpec;
144
145/**
146 *
147 * @author
148 */
149public class AES {
150 private static SecretKeySpec secretKey;
151 private static byte[] key;
152
153 public static void setKey(String myKey)
154 {
155 MessageDigest sha = null;
156 try {
157 key = myKey.getBytes("UTF-8");
158 sha = MessageDigest.getInstance("SHA-256");
159 key = sha.digest(key);
160 key = Arrays.copyOf(key, 16);
161 secretKey = new SecretKeySpec(key, "AES");
162 }
163 catch (NoSuchAlgorithmException e) {
164 e.printStackTrace();
165 }
166 catch (UnsupportedEncodingException e) {
167 e.printStackTrace();
168 }
169 }
170
171 public static String encrypt(String strToEncrypt, String secret)
172 {
173 try
174 {
175 setKey(secret);
176 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
177 cipher.init(Cipher.ENCRYPT_MODE, secretKey);
178 return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
179 }
180 catch (Exception e)
181 {
182 System.out.println("Error while encrypting: " + e.toString());
183 }
184 return null;
185 }
186
187 public static String decrypt(String strToDecrypt, String secret)
188 {
189 try
190 {
191 setKey(secret);
192 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
193 cipher.init(Cipher.DECRYPT_MODE, secretKey);
194 return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
195 }
196 catch (Exception e)
197 {
198 System.out.println("Error while decrypting: " + e.toString());
199 }
200 return null;
201 }
202
203 public static SecretKey toAes128Key(String s) {
204 try {
205 // turn secretKey into 256 bit hash
206 MessageDigest digest = MessageDigest.getInstance("SHA-256");
207 digest.reset();
208 digest.update(s.getBytes("UTF-8"));
209
210 // Due to the stupid US export restriction JDK only ships 128bit version.
211 return new SecretKeySpec(digest.digest(),0,128/8, "AES");
212 } catch (NoSuchAlgorithmException e) {
213 throw new Error(e);
214 } catch (UnsupportedEncodingException e) {
215 throw new Error(e);
216 }
217}
218}