@Override public void handle(Map<String, String> urlParams, IHTTPSession session) throws Exception { String alias = urlParams.get("alias"); String privateKeyEncoded = session.getParameters().get("privateKey").get(0); String certEncoded = session.getParameters().get("cert").get(0); Decoder base64 = Base64.getDecoder(); KeyFactory kf = KeyFactory.getInstance("RSA"); PrivateKey privateKey = kf.generatePrivate(new PKCS8EncodedKeySpec( base64.decode(privateKeyEncoded))); CertificateFactory cf = CertificateFactory.getInstance("X.509"); X509Certificate cert = (X509Certificate) cf.generateCertificate( new ByteArrayInputStream(base64.decode(certEncoded))); TlsSniServer server = BammBamm.instance().getTlsSniServer(); server.addCertificate(alias, privateKey, cert); }
StaticKeySource(final ClientSideStateProperties properties) { final Decoder decoder = Base64.getDecoder(); final String encryptionKeyAlgorithm = properties.getEncryptionAlgorithm().replaceFirst("/.*", ""); encryptionKey = new SecretKeySpec(decoder.decode(properties.getEncryptionKey()), encryptionKeyAlgorithm); authenticationKey = new SecretKeySpec(decoder.decode(properties.getAuthenticationKey()), properties.getAuthenticationAlgorithm()); }
private static void test1() throws Exception { byte[] src = new byte[] { 46, -97, -35, -44, 127, -60, -39, -4, -112, 34, -57, 47, -14, 67, 40, 18, 90, -59, 68, 112, 23, 121, -91, 94, 35, 49, 104, 17, 30, -80, -104, -3, -53, 27, 38, -72, -47, 113, -52, 18, 5, -126 }; Encoder encoder = Base64.getMimeEncoder(49, new byte[] { 0x7e }); byte[] encoded = encoder.encode(src); Decoder decoder = Base64.getMimeDecoder(); byte[] decoded = decoder.decode(encoded); if (!Objects.deepEquals(src, decoded)) { throw new RuntimeException(); } }
public static String aesDecryptString(String content, String key) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { Decoder decoder = Base64.getDecoder(); byte[] encryptedBytes = decoder.decode(content); byte[] keyBytes = key.getBytes(charset); byte[] decryptedBytes = aesDecryptBytes(encryptedBytes, keyBytes); return new String(decryptedBytes, charset); }
/** * Decrypt an array of integers from a String. * * @param encrypted * @param context * @return * @throws ServletException */ public static int[] decryptIntegers(String encrypted, String password) throws ServletException { String encryptedParts[] = encrypted.split("\\|"); if (encryptedParts.length != 3) { throw new ServletException("Invalid encrypted string."); } /* Extract the encrypted data, initialisation vector, and salt from the cookie. */ Decoder decoder = Base64.getDecoder(); byte ciphertext[] = decoder.decode(encryptedParts[0]); byte iv[] = decoder.decode(encryptedParts[1]); byte salt[] = decoder.decode(encryptedParts[2]); byte plainbytes[]; try { /* Derive the key, given password and salt. */ SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 65536, 256); SecretKey tmp = factory.generateSecret(spec); SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES"); /* Decrypt the message, given derived key and initialization vector. */ Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv)); plainbytes = cipher.doFinal(ciphertext); } catch (Exception ex) { throw new ServletException(ex); } IntBuffer buff = ByteBuffer.wrap(plainbytes).asIntBuffer(); int integers[] = new int[buff.remaining()]; for (int i = 0; i < integers.length; ++i) { integers[i] = buff.get(); } return integers; }
/** * Decrypt an integer from a String. * * @param encrypted * @param context * @return * @throws ServletException */ private static Integer decryptInteger(String encrypted, ServletContext context) throws ServletException { String encryptedParts[] = encrypted.split("\\|"); if (encryptedParts.length != 3) { throw new ServletException("Invalid encrypted string."); } /* Get password. */ String password = context.getInitParameter("SampleSetIDEncryptionPassword"); /* Extract the encrypted data, initialisation vector, and salt from the cookie. */ Decoder decoder = Base64.getDecoder(); byte ciphertext[] = decoder.decode(encryptedParts[0]); byte iv[] = decoder.decode(encryptedParts[1]); byte salt[] = decoder.decode(encryptedParts[2]); byte plainbytes[]; try { /* Derive the key, given password and salt. */ SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 65536, 256); SecretKey tmp = factory.generateSecret(spec); SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES"); /* Decrypt the message, given derived key and initialization vector. */ Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv)); plainbytes = cipher.doFinal(ciphertext); } catch (Exception ex) { throw new ServletException(ex); } return ByteBuffer.wrap(plainbytes).asIntBuffer().get(); }
private String getPropertyValue(final String propertyValue) throws IllegalArgumentException, EncryptionException { final Decoder decoder = Base64.getUrlDecoder(); final String value = new String(decoder.decode(propertyValue)); return value; }
public Optional<String> getValueAsString() { return value.map(val-> { Decoder decoder = Base64.getDecoder(); return new String(decoder.decode(val), Charset.forName("UTF-8")); }); }
/** * 对字符串做base64解码 * @param src * @return */ public static byte[] decodeBase64(String src) { Decoder decoder = Base64.getDecoder(); return decoder.decode(src.getBytes()); }
@Override public CredentialSource createCredentialSource(final Map<String, String> configuration) throws GeneralSecurityException, IOException { final String algorithmParamsBase64 = option(configuration, CREDENTIAL_STORE_PROTECTION_PARAMS, ""); final Decoder decoder = Base64.getDecoder(); final byte[] encodedAlgorithmParams = decoder.decode(algorithmParamsBase64); final String algorithm = option(configuration, CREDENTIAL_STORE_PROTECTION_ALGORITHM, DEFAULT_ALGORITHM); final Provider provider = ProviderHelper .provider(option(configuration, CREDENTIAL_STORE_PROTECTION_PROVIDER, ProviderHelper.WILDFLY_PROVIDER)); final AlgorithmParameters algorithmParameters = AlgorithmParameters.getInstance(algorithm, provider); algorithmParameters.init(encodedAlgorithmParams); final MaskedPasswordAlgorithmSpec maskedPasswordAlgorithmSpec = algorithmParameters .getParameterSpec(MaskedPasswordAlgorithmSpec.class); final char[] initialKeyMaterial = maskedPasswordAlgorithmSpec.getInitialKeyMaterial(); final int iterationCount = maskedPasswordAlgorithmSpec.getIterationCount(); final byte[] salt = maskedPasswordAlgorithmSpec.getSalt(); final String maskedPasswordBase64 = option(configuration, CREDENTIAL_STORE_PROTECTION, ""); final byte[] maskedPasswordBytes = decoder.decode(maskedPasswordBase64); final MaskedPasswordSpec maskedPasswordSpec = new MaskedPasswordSpec(initialKeyMaterial, iterationCount, salt, maskedPasswordBytes); final PasswordFactory passwordFactory = PasswordFactory.getInstance(algorithm, provider); final Password maskedPassword = passwordFactory.generatePassword(maskedPasswordSpec); final PasswordFactory clearPasswordFactory = PasswordFactory.getInstance(ClearPassword.ALGORITHM_CLEAR, provider); final ClearPasswordSpec clearPasswordSpec = passwordFactory.getKeySpec(maskedPassword, ClearPasswordSpec.class); final Password password = clearPasswordFactory.generatePassword(clearPasswordSpec); final PasswordCredential passwordCredential = new PasswordCredential(password); return IdentityCredentials.NONE.withCredential(passwordCredential); }