List of usage examples for org.bouncycastle.util.encoders Hex encode
public static int encode(byte[] data, OutputStream out) throws IOException
From source file:AmazonDynamoDBSample.java
License:Open Source License
private static void oneTimeAddCitations() { String jsonString = ""; try {/*from ww w. j a v a 2s.c o m*/ jsonString = readFile("./json/citations.json", StandardCharsets.UTF_8); } catch (IOException e) { System.out.println(e); } try { JSONObject rootObject = new JSONObject(jsonString); // Parse the JSON to a JSONObject JSONArray rows = rootObject.getJSONArray("stuff"); // Get all JSONArray rows System.out.println("row lengths: " + rows.length()); set1 = new HashSet<>(); for (int j = 0; j < rows.length(); j++) { // Iterate each element in the elements array JSONObject element = rows.getJSONObject(j); // Get the element object int id = element.getInt("id"); int citationNumber = element.getInt("citation_number"); String citationDate = " "; Boolean isCitationDateNull = element.isNull("citation_date"); if (!isCitationDateNull) citationDate = element.getString("citation_date"); String firstName = element.getString("first_name"); String lastName = element.getString("last_name"); String firstLastName = firstName + lastName; firstLastName = firstLastName.toLowerCase(); set1.add(firstLastName); //System.out.println(firstLastName); String dob = " "; Boolean isDobNull = element.isNull("date_of_birth"); if (!isDobNull) { dob = element.getString("date_of_birth"); dob = (dob.split(" "))[0]; } // pick a ssn from list String ssn = ssnList.get(ssnCounter); ssnCounter--; ssnHashMap.put(firstLastName, ssn); System.out.println(firstLastName + " " + ssn); // compute salt final Random ran = new SecureRandom(); byte[] salt = new byte[32]; ran.nextBytes(salt); String saltString = Base64.encodeBase64String(salt); //System.out.println("saltstring: " + saltString); saltHashMap.put(firstLastName, saltString); // compute ripemd160 hash of ssn + salt String saltPlusSsn = saltString + ssn; //System.out.println("salt plus ssn: " + saltPlusSsn); String resultingHash = ""; try { byte[] r = saltPlusSsn.getBytes("US-ASCII"); RIPEMD160Digest d = new RIPEMD160Digest(); d.update(r, 0, r.length); byte[] o = new byte[d.getDigestSize()]; d.doFinal(o, 0); ByteArrayOutputStream baos = new ByteArrayOutputStream(40); Hex.encode(o, baos); resultingHash = new String(baos.toByteArray(), StandardCharsets.UTF_8); hashedssnHashMap.put(firstLastName, resultingHash); } catch (UnsupportedEncodingException e) { System.out.println(e); } catch (IOException i) { System.out.println(i); } String fldob = firstLastName + dob; String da = " "; Boolean isDaNull = element.isNull("defendant_address"); if (!isDaNull) da = element.getString("defendant_address"); String dc = " "; Boolean isDcNull = element.isNull("defendant_city"); if (!isDcNull) dc = element.getString("defendant_city"); String ds = " "; Boolean isDsNull = element.isNull("defendant_state"); if (!isDsNull) ds = element.getString("defendant_state"); String dln = " "; Boolean isDlnNull = element.isNull("drivers_license_number"); if (!isDlnNull) dln = element.getString("drivers_license_number"); String cd = " "; Boolean isCdNull = element.isNull("court_date"); if (!isCdNull) cd = element.getString("court_date"); String cl = " "; Boolean isClNull = element.isNull("court_location"); if (!isClNull) cl = element.getString("court_location"); String ca = " "; Boolean isCaNull = element.isNull("court_address"); if (!isCaNull) ca = element.getString("court_address"); /* Map<String, AttributeValue> item = newCitationItem(citationNumber, citationDate, firstName, lastName, firstLastName, dob, fldob, resultingHash, da, dc, ds, dln, cd, cl, ca); PutItemRequest putItemRequest = new PutItemRequest("citations-table", item); PutItemResult putItemResult = dynamoDB.putItem(putItemRequest); */ } } catch (JSONException e) { // JSON Parsing error e.printStackTrace(); } }
From source file:AmazonDynamoDBSample.java
License:Open Source License
private static void oneTimeAddWarrants() { String jsonString = ""; try {// w w w . j a va 2 s .c om jsonString = readFile("./json/warrants.json", StandardCharsets.UTF_8); } catch (IOException e) { System.out.println(e); } try { JSONObject rootObject = new JSONObject(jsonString); // Parse the JSON to a JSONObject JSONArray rows = rootObject.getJSONArray("stuff"); // Get all JSONArray rows //System.out.println("row lengths: " + rows.length()); set2 = new HashSet<>(); for (int j = 0; j < rows.length(); j++) { // Iterate each element in the elements array JSONObject element = rows.getJSONObject(j); // Get the element object String defendant = element.getString("Defendant"); String strarr[] = defendant.split(" "); String temp = strarr[1]; int len = strarr[0].length(); strarr[0] = strarr[0].substring(0, len - 1); strarr[1] = strarr[0]; strarr[0] = temp; String firstLast = strarr[0] + strarr[1]; firstLast = firstLast.toLowerCase(); set2.add(firstLast); //System.out.println(firstLast); int zipCode = 0; Boolean isZipCodeNull = element.isNull("ZIP Code"); if (!isZipCodeNull) zipCode = element.getInt("ZIP Code"); String dob = element.getString("Date of Birth"); String caseNumber = element.getString("Case Number"); String firstLastDOB = firstLast + dob; // pick a ssn from list String ssn = ssnList.get(ssnCounter); ssnCounter--; ssnHashMap.put(firstLast, ssn); // compute salt final Random ran = new SecureRandom(); byte[] salt = new byte[32]; ran.nextBytes(salt); String saltString = Base64.encodeBase64String(salt); //System.out.println("saltstring: " + saltString); saltHashMap.put(firstLast, saltString); // compute ripemd160 hash of ssn + salt String saltPlusSsn = saltString + ssn; //System.out.println("salt plus ssn: " + saltPlusSsn); String resultingHash = ""; try { byte[] r = saltPlusSsn.getBytes("US-ASCII"); RIPEMD160Digest d = new RIPEMD160Digest(); d.update(r, 0, r.length); byte[] o = new byte[d.getDigestSize()]; d.doFinal(o, 0); ByteArrayOutputStream baos = new ByteArrayOutputStream(40); Hex.encode(o, baos); resultingHash = new String(baos.toByteArray(), StandardCharsets.UTF_8); hashedssnHashMap.put(firstLast, resultingHash); } catch (UnsupportedEncodingException e) { System.out.println(e); } catch (IOException i) { System.out.println(i); } //compareNames(); Map<String, AttributeValue> item = newWarrantItem(firstLast, firstLastDOB, resultingHash, defendant, zipCode, dob, caseNumber); PutItemRequest putItemRequest = new PutItemRequest("warrants-table", item); PutItemResult putItemResult = dynamoDB.putItem(putItemRequest); } } catch (JSONException e) { // JSON Parsing error e.printStackTrace(); } }
From source file:com.oneops.cms.crypto.CmsCryptoDES.java
License:Apache License
/** * Encrypt./*from w w w . ja v a 2s .c o m*/ * * @param instr the instr * @return the string * @throws java.security.GeneralSecurityException the general security exception */ @Override public String encrypt(String instr) throws GeneralSecurityException { long t1 = System.currentTimeMillis(); byte[] in = instr.getBytes(); PaddedBufferedBlockCipher encryptor = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESedeEngine())); encryptor.init(true, keyParameter); byte[] cipherText = new byte[encryptor.getOutputSize(in.length)]; int outputLen = encryptor.processBytes(in, 0, in.length, cipherText, 0); ByteArrayOutputStream os = new ByteArrayOutputStream(); try { encryptor.doFinal(cipherText, outputLen); Hex.encode(cipherText, os); } catch (Exception e) { e.printStackTrace(); throw new GeneralSecurityException(e); } long t2 = System.currentTimeMillis(); logger.debug("Time taken to encrypt(millis) :" + (t2 - t1)); return ENC_PREFIX + os.toString(); }
From source file:com.oneops.cms.crypto.CmsCryptoOpenSSLImpl.java
License:Apache License
/** * Encrypt.//from w w w. j a v a 2s . co m * * @param instr the instr * @return the string * @throws GeneralSecurityException the general security exception * @throws IOException Signals that an I/O exception has occurred. */ @Override public String encrypt(String instr) throws GeneralSecurityException, IOException { byte[] in = instr.getBytes(); ByteArrayOutputStream os = new ByteArrayOutputStream(); try { byte[] out = encryptor.doFinal(in); Hex.encode(out, os); } catch (IOException e) { e.printStackTrace(); initEncryptorDecryptor(); throw new GeneralSecurityException(e); } catch (ArrayIndexOutOfBoundsException e) { e.printStackTrace(); initEncryptorDecryptor(); throw new GeneralSecurityException("The value is too long for encryption"); } return ENC_PREFIX + os.toString(); }
From source file:org.ejbca.core.protocol.xkms.XKMSEncTest.java
License:Open Source License
@Test public void test02TestAliceRegistrationAuthenticationKey() throws Exception { String authenticationData = "024837"; SecretKey retval = XKMSUtil.getSecretKeyFromPassphrase(authenticationData, true, 20, XKMSUtil.KEY_AUTHENTICATION); assertTrue(retval.getEncoded().length == 20); ByteArrayOutputStream baos = new ByteArrayOutputStream(); Hex.encode(retval.getEncoded(), baos); String resultString = new String(baos.toByteArray()); assertTrue(resultString.equalsIgnoreCase("d6cc34cb83fae2993a393aa8e7de9a06c7fa2c92")); }
From source file:org.ejbca.core.protocol.xkms.XKMSEncTest.java
License:Open Source License
@Test public void test03TestBOBRegistrationPrivateKeyEncryption() throws Exception { String authenticationData = "3N9CJ-K4JKS-04JWF-0934J-SR09JW-IK4"; SecretKey retval = XKMSUtil.getSecretKeyFromPassphrase(authenticationData, true, 24, XKMSUtil.KEY_PRIVATEKEYDATA); assertTrue(retval.getEncoded().length == 24); ByteArrayOutputStream baos = new ByteArrayOutputStream(); Hex.encode(retval.getEncoded(), baos); String resultString = new String(baos.toByteArray()); log.debug(resultString);//ww w. j ava 2 s . c om assertTrue(resultString.equalsIgnoreCase("78e8bbf532d01dece38aa9d2a4a409dbff1a265cdbae1b95")); }
From source file:org.ejbca.core.protocol.xkms.XKMSEncTest.java
License:Open Source License
@Test public void test04TestRevocationCodeIdentifyerGeneration() throws Exception { String authenticationData = "Help I Have Revealed My Key"; ByteArrayOutputStream baos = new ByteArrayOutputStream(); Hex.encode(authenticationData.getBytes(), baos); String resultString = new String(baos.toByteArray()); log.debug(resultString);/* w ww. j a va 2s.c om*/ assertTrue(resultString.equalsIgnoreCase("48656c70204920486176652052657665616c6564204d79204b6579")); SecretKey key1 = XKMSUtil.getSecretKeyFromPassphrase(authenticationData, true, 20, XKMSUtil.KEY_REVOCATIONCODEIDENTIFIER_PASS1); assertTrue(key1.getEncoded().length == 20); baos = new ByteArrayOutputStream(); Hex.encode(key1.getEncoded(), baos); resultString = new String(baos.toByteArray()); log.debug(resultString); assertTrue(resultString.equalsIgnoreCase("1c0857c95458c26f44327efd0ef055b08cad5c78")); SecretKey key2 = XKMSUtil.getSecretKeyFromPassphrase(new String(key1.getEncoded(), "ISO8859-1"), false, 20, XKMSUtil.KEY_REVOCATIONCODEIDENTIFIER_PASS2); assertTrue(key2.getEncoded().length == 20); baos = new ByteArrayOutputStream(); Hex.encode(key2.getEncoded(), baos); resultString = new String(baos.toByteArray()); log.debug(resultString); assertTrue(resultString.equalsIgnoreCase("e6b44dd9c39988c95c889c41a9a7a5ad90c2cd21")); String byte64String = new String(Base64.encode(key2.getEncoded(), false)); log.debug(byte64String); assertTrue(byte64String.equals("5rRN2cOZiMlciJxBqaelrZDCzSE=")); }