public static String decryptJuniper9CipherText(String key) { return INSTANCE.decrypt(key); }
private JuniperUtils() { _codeMatrix = initCodeMatrix(); _characterFamilies = new String[] { "QzF3n6/9CAtpu0O", "B1IREhcSyrleKvMW8LXx", "7N-dVbwsY2g4oaJZGUDj", "iHkq.mPf5T" }; _characterFamilyReverseIndexMap = initCharacterFamilyReverseIndexMap(); _allCharacters = initAllCharacters(); _allCharactersIndexMap = initAllCharactersIndexMap(); _validationRegex = initValidationRegex(); }
private String decrypt(String key) { if (!isJuniper9CipherText(key)) { throw new BatfishException("Invalid Juniper $9$ ciphertext: \"" + key + "\""); } String[] chars = new String[] {key.substring("$9$".length())}; char first = nibble(chars, 1).charAt(0); nibble(chars, _characterFamilyReverseIndexMap.get(first)); char prev = first; StringBuilder decrypted = new StringBuilder(); while (chars[0].length() > 0) { List<Integer> decode = _codeMatrix.get(decrypted.length() % _codeMatrix.size()); int len = decode.size(); char[] nibbleChars = new char[len]; nibble(chars, len).getChars(0, len, nibbleChars, 0); List<Integer> gaps = new ArrayList<>(); for (int i = 0; i < len; i++) { char nibbleChar = nibbleChars[i]; int g = gap(prev, nibbleChar); prev = nibbleChar; gaps.add(g); } char newChar = gapDecode(gaps, decode); decrypted.append(newChar); } return decrypted.toString(); }
private @Nonnull String decryptIfNeededAndHash(@Nonnull String text, int line) { if (JuniperUtils.isJuniper9CipherText(text)) { return JuniperUtils.decryptAndHashJuniper9CipherText(text); } else { _w.redFlag(String.format("Unencrypted key stored at line: %d", line)); return saltAndHash(text); } }
public static String decryptAndHashJuniper9CipherText(String key) { String privateSecret = decryptJuniper9CipherText(key); String saltedSecret = privateSecret + CommonUtil.salt(); String sha256Digest = CommonUtil.sha256Digest(saltedSecret); return sha256Digest; }