|
| 1 | +/* |
| 2 | + * Copyright (c) 2017 Patrick Scheibe |
| 3 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | + * of this software and associated documentation files (the "Software"), to deal |
| 5 | + * in the Software without restriction, including without limitation the rights |
| 6 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | + * copies of the Software, and to permit persons to whom the Software is |
| 8 | + * furnished to do so, subject to the following conditions: |
| 9 | + * |
| 10 | + * The above copyright notice and this permission notice shall be included in |
| 11 | + * all copies or substantial portions of the Software. |
| 12 | + * |
| 13 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | + * THE SOFTWARE. |
| 20 | + */ |
| 21 | + |
| 22 | +package de.halirutan.mathematica.errorreporting; |
| 23 | + |
| 24 | +import org.apache.commons.codec.binary.Base64; |
| 25 | + |
| 26 | +import javax.crypto.Cipher; |
| 27 | +import javax.crypto.spec.IvParameterSpec; |
| 28 | +import javax.crypto.spec.SecretKeySpec; |
| 29 | +import java.io.*; |
| 30 | + |
| 31 | +/** |
| 32 | + * Provides functionality to encode and decode secret tokens to make them not directly readable. Let me be clear: |
| 33 | + * THIS IS THE OPPOSITE OF SECURITY! |
| 34 | + * @author patrick (20.06.17). |
| 35 | + */ |
| 36 | +public class GitHubAccessTokenScrambler { |
| 37 | + |
| 38 | + private static final String myInitVector = "RandomInitVector"; |
| 39 | + private static final String myKey = "GitHubErrorToken"; |
| 40 | + |
| 41 | + public static void main(String[] args) { |
| 42 | + if (args.length != 2) { |
| 43 | + return; |
| 44 | + } |
| 45 | + String horse = args[0]; |
| 46 | + String outputFile = args[1]; |
| 47 | + try { |
| 48 | + final String e = encrypt(horse); |
| 49 | + final ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(outputFile)); |
| 50 | + o.writeObject(e); |
| 51 | + o.close(); |
| 52 | + } catch (Exception e) { |
| 53 | + e.printStackTrace(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private static String encrypt(String value) { |
| 58 | + try { |
| 59 | + IvParameterSpec iv = new IvParameterSpec(myInitVector.getBytes("UTF-8")); |
| 60 | + SecretKeySpec keySpec = new SecretKeySpec(myKey.getBytes("UTF-8"), "AES"); |
| 61 | + |
| 62 | + Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); |
| 63 | + cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv); |
| 64 | + |
| 65 | + byte[] encrypted = cipher.doFinal(value.getBytes()); |
| 66 | + return Base64.encodeBase64String(encrypted); |
| 67 | + } catch (Exception ex) { |
| 68 | + ex.printStackTrace(); |
| 69 | + } |
| 70 | + return null; |
| 71 | + } |
| 72 | + |
| 73 | + static String decrypt(String file) throws Exception { |
| 74 | + String in; |
| 75 | + final ObjectInputStream o = new ObjectInputStream(new FileInputStream(file)); |
| 76 | + in = (String) o.readObject(); |
| 77 | + IvParameterSpec iv = new IvParameterSpec(myInitVector.getBytes("UTF-8")); |
| 78 | + SecretKeySpec keySpec = new SecretKeySpec(myKey.getBytes("UTF-8"), "AES"); |
| 79 | + |
| 80 | + Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); |
| 81 | + cipher.init(Cipher.DECRYPT_MODE, keySpec, iv); |
| 82 | + |
| 83 | + byte[] original = cipher.doFinal(Base64.decodeBase64(in)); |
| 84 | + return new String(original); |
| 85 | + } |
| 86 | +} |
0 commit comments