List of usage examples for org.bouncycastle.util.encoders Hex decode
public static byte[] decode(String data)
From source file:ECIESTest.java
public TestResult perform() { SecureRandom random = new SecureRandom(); ECCurve.Fp curve = new ECCurve.Fp( new BigInteger("883423532389192164791648750360308885314476597252960362792450860609699839"), // q new BigInteger("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc", 16), // a new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16)); // b ECDomainParameters params = new ECDomainParameters(curve, curve.decodePoint(Hex.decode("020ffa963cdca8816ccc33b8642bedf905c3d358573d3f27fbbd3b3cb9aaaf")), // G new BigInteger("883423532389192164791648750360308884807550341691627752275345424702807307")); // n ECKeyPairGenerator pGen = new ECKeyPairGenerator(); ECKeyGenerationParameters genParam = new ECKeyGenerationParameters(params, random); pGen.init(genParam);//from w w w . j a va2s .co m AsymmetricCipherKeyPair p1 = pGen.generateKeyPair(); AsymmetricCipherKeyPair p2 = pGen.generateKeyPair(); // // stream test // IESEngine i1 = new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()), new HMac(new SHA1Digest())); IESEngine i2 = new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()), new HMac(new SHA1Digest())); byte[] d = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; byte[] e = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 }; IESParameters p = new IESParameters(d, e, 64); i1.init(true, p1.getPrivate(), p2.getPublic(), p); i2.init(false, p2.getPrivate(), p1.getPublic(), p); byte[] message = Hex.decode("1234567890abcdef"); try { byte[] out1 = i1.processBlock(message, 0, message.length); byte[] out2 = i2.processBlock(out1, 0, out1.length); if (!sameAs(out2, message)) { return new SimpleTestResult(false, this.getName() + ": stream cipher test failed"); } } catch (Exception ex) { return new SimpleTestResult(false, this.getName() + ": stream cipher test exception " + ex.toString()); } // // twofish with IV0 test // BufferedBlockCipher c1 = new PaddedBufferedBlockCipher(new CBCBlockCipher(new TwofishEngine())); BufferedBlockCipher c2 = new PaddedBufferedBlockCipher(new CBCBlockCipher(new TwofishEngine())); i1 = new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()), new HMac(new SHA1Digest()), c1); i2 = new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()), new HMac(new SHA1Digest()), c2); d = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; e = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 }; p = new IESWithCipherParameters(d, e, 64, 128); i1.init(true, p1.getPrivate(), p2.getPublic(), p); i2.init(false, p2.getPrivate(), p1.getPublic(), p); message = Hex.decode("1234567890abcdef"); try { byte[] out1 = i1.processBlock(message, 0, message.length); byte[] out2 = i2.processBlock(out1, 0, out1.length); if (!sameAs(out2, message)) { return new SimpleTestResult(false, this.getName() + ": twofish cipher test failed"); } } catch (Exception ex) { return new SimpleTestResult(false, this.getName() + ": twofish cipher test exception " + ex.toString()); } return new SimpleTestResult(true, this.getName() + ": Okay"); }
From source file:SampleUser.java
License:Open Source License
/** * Restore the state of this user from the key value store (if found). If not found, do nothing. *//*ww w. j av a 2 s. com*/ SampleUser restoreState() { String memberStr = keyValStore.getValue(keyValStoreName); if (null != memberStr) { // The user was found in the key value store, so restore the // state. byte[] serialized = Hex.decode(memberStr); ByteArrayInputStream bis = new ByteArrayInputStream(serialized); try { ObjectInputStream ois = new ObjectInputStream(bis); SampleUser state = (SampleUser) ois.readObject(); if (state != null) { this.name = state.name; this.roles = state.roles; this.account = state.account; this.affiliation = state.affiliation; this.organization = state.organization; this.enrollmentSecret = state.enrollmentSecret; this.enrollment = state.enrollment; this.mspId = state.mspId; return this; } } catch (Exception e) { throw new RuntimeException(String.format("Could not restore state of member %s", this.name), e); } } return null; }
From source file:alexkantas.cryptography.algorithms.AES.java
License:Apache License
/** * Sets a new 256-bit AES key//w ww. java 2 s .co m * * @param keyString Key in HEX String */ public void setkey(String keyString) { byte[] keyBytes = Hex.decode(keyString.getBytes()); SecretKey AES_key = new SecretKeySpec(keyBytes, "AES"); this.AES_key = AES_key; }
From source file:be.fedict.eid.applet.service.impl.handler.AuthenticationDataMessageHandler.java
License:Open Source License
private void checkSessionIdChannelBinding(AuthenticationDataMessage message, HttpServletRequest request) { LOG.debug("using TLS session Id channel binding"); byte[] sessionId = message.sessionId; /*// www.j a v a 2s. co m * Next is Tomcat specific. */ String actualSessionId = (String) request.getAttribute("javax.servlet.request.ssl_session"); if (null == actualSessionId) { /* * Servlet specs v3.0 */ actualSessionId = (String) request.getAttribute("javax.servlet.request.ssl_session_id"); } if (null == actualSessionId) { LOG.warn("could not verify the SSL session identifier"); return; } if (false == Arrays.equals(sessionId, Hex.decode(actualSessionId))) { LOG.warn("SSL session Id mismatch"); LOG.debug("signed SSL session Id: " + new String(Hex.encode(sessionId))); LOG.debug("actual SSL session Id: " + actualSessionId); throw new SecurityException("SSL session Id mismatch"); } LOG.debug("SSL session identifier checked"); }
From source file:bisq.common.crypto.Encryption.java
License:Open Source License
public static byte[] decryptPayloadWithHmac(byte[] encryptedPayloadWithHmac, SecretKey secretKey) throws CryptoException { byte[] payloadWithHmac = decrypt(encryptedPayloadWithHmac, secretKey); String payloadWithHmacAsHex = Hex.toHexString(payloadWithHmac); // first part is raw message int length = payloadWithHmacAsHex.length(); int sep = length - 64; String payloadAsHex = payloadWithHmacAsHex.substring(0, sep); // last 64 bytes is hmac String hmacAsHex = payloadWithHmacAsHex.substring(sep, length); if (verifyHmac(Hex.decode(payloadAsHex), Hex.decode(hmacAsHex), secretKey)) { return Hex.decode(payloadAsHex); } else {//from w ww .j a v a 2 s . c o m throw new CryptoException("Hmac does not match."); } }
From source file:br.com.hackathon.rest.criptografia.Encriptor.java
public byte[] decrypt(String str) throws NoSuchAlgorithmException, NoSuchPaddingException { byte[] senhaPlana = null; try {//www. j ava2 s . c o m byte[] bufferSenha = Hex.decode(str); cipher = Cipher.getInstance("AES"); secretKeySpec = new SecretKeySpec(getKey(), "AES"); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); senhaPlana = cipher.doFinal(bufferSenha); } catch (UnsupportedEncodingException | NoSuchAlgorithmException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | StringIndexOutOfBoundsException ex) { } return senhaPlana; }
From source file:ca.trustpoint.m2m.AuthorityKeyIdentifierTest.java
License:Apache License
/** * Test method for/*from w w w . ja v a 2s .c om*/ * {@link ca.trustpoint.m2m.AuthorityKeyIdentifier#AuthorityKeyIdentifier(byte[], * ca.trustpoint.m2m.GeneralName, java.math.BigInteger)}. */ @Test public void testAuthorityKeyIdentifierByteArrayGeneralNameBigInteger() { byte[] keyId = Hex.decode("3f2a7529ba22"); GeneralName certIssuer = new GeneralName(); BigInteger certSerialNumber = new BigInteger("2836741231236324239234726261890882"); testConstructor(keyId, null, null); testConstructor(null, certIssuer, null); testConstructor(null, null, certSerialNumber); testConstructor(keyId, certIssuer, null); testConstructor(keyId, null, certSerialNumber); testConstructor(null, certIssuer, certSerialNumber); testConstructor(keyId, certIssuer, certSerialNumber); }
From source file:ca.trustpoint.m2m.AuthorityKeyIdentifierTest.java
License:Apache License
/** * Test method for {@link ca.trustpoint.m2m.AuthorityKeyIdentifier#setKeyIdentifier(byte[])}. *///from ww w. ja va2 s. co m @Test public void testSetKeyIdentifier() { AuthorityKeyIdentifier authKeyId = new AuthorityKeyIdentifier(); assertNull(authKeyId.getKeyIdentifier()); byte[] expectedValue = Hex.decode("02ac6e4a2285"); authKeyId.setKeyIdentifier(expectedValue); assertArrayEquals(expectedValue, authKeyId.getKeyIdentifier()); expectedValue = Hex.decode("99fe72ad72"); authKeyId.setKeyIdentifier(expectedValue); assertArrayEquals(expectedValue, authKeyId.getKeyIdentifier()); }
From source file:ca.trustpoint.m2m.AuthorityKeyIdentifierTest.java
License:Apache License
/** * Test method for {@link ca.trustpoint.m2m.AuthorityKeyIdentifier#isValid()}. */// w w w. j a va2 s. c om @Test public void testIsValid() { AuthorityKeyIdentifier authKeyId = new AuthorityKeyIdentifier(); assertFalse(authKeyId.isValid()); authKeyId.setKeyIdentifier(new byte[0]); assertFalse(authKeyId.isValid()); authKeyId.setKeyIdentifier(Hex.decode("2347234aade22aec")); assertTrue(authKeyId.isValid()); authKeyId.setCertificateIssuer(new GeneralName()); assertFalse(authKeyId.isValid()); EntityName entityName = new EntityName(); entityName.addAttribute(new EntityNameAttribute(EntityNameAttributeId.Country, "CA")); GeneralName validName = new GeneralName(entityName); authKeyId.setCertificateIssuer(validName); assertTrue(authKeyId.isValid()); authKeyId.setCertificateSerialNumber(new BigInteger(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15 })); assertFalse(authKeyId.isValid()); authKeyId.setCertificateSerialNumber(new BigInteger("1231296712907230496192873916192874")); assertTrue(authKeyId.isValid()); }
From source file:ca.trustpoint.m2m.AuthorityKeyIdentifierTest.java
License:Apache License
/** * Test method for {@link ca.trustpoint.m2m.AuthorityKeyIdentifier#getEncoded()}. *///from w w w . j av a2 s.c o m @Test public void testGetEncoded() throws IOException { boolean exceptionThrown = false; AuthorityKeyIdentifier authKeyId = new AuthorityKeyIdentifier(); try { authKeyId.getEncoded(); } catch (IOException ex) { exceptionThrown = true; } assertTrue(exceptionThrown); byte[] expectedEncoding = new byte[] { 0x30, 0x08, (byte) 0x80, 0x06, 0x73, 0x68, (byte) 0xA3, (byte) 0xDC, 0x6E, 0x4F }; authKeyId.setKeyIdentifier(Hex.decode("7368a3dc6e4f")); assertArrayEquals(expectedEncoding, authKeyId.getEncoded()); expectedEncoding = new byte[] { 0x30, 0x16, (byte) 0x80, 0x06, 0x73, 0x68, (byte) 0xA3, (byte) 0xDC, 0x6E, 0x4F, (byte) 0xA1, 0x0C, (byte) 0x81, 0x0A, 0x74, 0x65, 0x73, 0x74, 0x64, 0x6F, 0x6D, 0x61, 0x69, 0x6E }; authKeyId.setCertificateIssuer(new GeneralName(GeneralNameAttributeId.DnsName, "testdomain")); assertArrayEquals(expectedEncoding, authKeyId.getEncoded()); EntityName entityName = new EntityName(); entityName.addAttribute(new EntityNameAttribute(EntityNameAttributeId.Country, "CA")); GeneralName validName = new GeneralName(entityName); expectedEncoding = new byte[] { 0x30, 0x10, (byte) 0x80, 0x06, 0x73, 0x68, (byte) 0xA3, (byte) 0xDC, 0x6E, 0x4F, (byte) 0xA1, 0x06, (byte) 0xA2, 0x04, (byte) 0x80, 0x02, 0x43, 0x41 }; authKeyId.setCertificateIssuer(validName); assertArrayEquals(expectedEncoding, authKeyId.getEncoded()); expectedEncoding = new byte[] { 0x30, 0x20, (byte) 0x80, 0x06, 0x73, 0x68, (byte) 0xA3, (byte) 0xDC, 0x6E, 0x4F, (byte) 0xA1, 0x06, (byte) 0xA2, 0x04, (byte) 0x80, 0x02, 0x43, 0x41, (byte) 0x82, 0x0E, 0x3C, (byte) 0xB5, 0x26, 0x41, 0x37, (byte) 0x95, (byte) 0xFC, (byte) 0xD3, (byte) 0xEA, (byte) 0xFC, 0x22, (byte) 0x92, (byte) 0xD8, 0x6A }; authKeyId.setCertificateSerialNumber(new BigInteger("1231296712907230496192873916192874")); assertArrayEquals(expectedEncoding, authKeyId.getEncoded()); }