Generates a cryptographically secure API key of the specified length.
@param chars the length of the API key to generate
@return a String representation of the API key
433589
public static String generate(final int chars) {
        final SecureRandom secureRandom = new SecureRandom();
        final char[] buff = new char[chars];
        for (int i = 0; i < chars; ++i) {
            if (i % 10 == 0) {
                secureRandom.setSeed(secureRandom.nextLong());
            }
            buff[i] = VALID_CHARACTERS[secureRandom.nextInt(VALID_CHARACTERS.length)];
        }
        return new String(buff);
    }
164131
def _create_secret(self, length=12):
        
        # Charset will have 64 +- characters
        charset = string.digits + string.ascii_letters + '+-'
        return "".join(random.SystemRandom().choice(charset) for _ in
                range(length))
1178013
public static PrivateKey stringToRSAPrivateKey (String str)
    {
        try {
            BigInteger mod = new BigInteger(str.substring(0, str.indexOf(SPLIT)), 16);
            BigInteger exp = new BigInteger(str.substring(str.indexOf(SPLIT) + 1), 16);
            RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(mod, exp);
            KeyFactory kf = KeyFactory.getInstance("RSA");
            return kf.generatePrivate(keySpec);
        } catch (NumberFormatException nfe) {
            log.warning("Failed to read key from string.", "str", str, nfe);
        } catch (GeneralSecurityException gse) {
            log.warning("Failed to read key from string.", "str", str, gse);
        }
        return null;
    }
881159
public static String encodeString(String string, Charset charset) {
        if (charset == null)
            charset = ENCODING;
        byte[] bytes = null;
        bytes = string.getBytes(charset);

        int len = bytes.length;
        byte[] encoded = new byte[bytes.length * 3];
        int n = 0;
        boolean noEncode = true;

        for (int i = 0; i < len; i++) {
            byte b = bytes[i];

            if (b == ' ') {
                noEncode = false;
                encoded[n++] = (byte) '+';
            } else if (b >= 'a' && b <= 'z' ||
                    b >= 'A' && b <= 'Z' ||
                    b >= '0' && b <= '9') {
                encoded[n++] = b;
            } else {
                noEncode = false;
                encoded[n++] = (byte) '%';
                byte nibble = (byte) ((b & 0xf0) >> 4);
                if (nibble >= 10)
                    encoded[n++] = (byte) ('A' + nibble - 10);
                else
                    encoded[n++] = (byte) ('0' + nibble);
                nibble = (byte) (b & 0xf);
                if (nibble >= 10)
                    encoded[n++] = (byte) ('A' + nibble - 10);
                else
                    encoded[n++] = (byte) ('0' + nibble);
            }
        }

        if (noEncode)
            return string;

        return new String(encoded, 0, n, charset);
    }
721030
public static String ascii(String code, int key) {
        StringBuilder string = new StringBuilder();
        int[] keys = CryptUtils.getKeys(key);
        for (int i = 0; i < code.length(); i++) {
            string.append((char) (code.charAt(i) - (i + keys[0]) % keys[1] % keys[2]));
        }
        return string.toString();
    }
1125323
public static CharSequence[] escapeKeys(CharSequence... keys) {
		CharSequence[] escapedKeys = new CharSequence[keys.length];
		
		for (int index = 0; index < keys.length; index++) {
			escapedKeys[index] = escapeKeys(keys[index]);
		}
		
		return escapedKeys;
	}
563479
public String decryptStr(byte[] bytes, Charset charset) {
		return StrUtil.str(decrypt(bytes), charset);
	}
171129
public static char[] encrypt(char[] password)
	{
		if (password == null || password.length == 0)
		{
			return null;
		}

		try
		{
			String encryptedString = null;

			SecretKeySpec key = new SecretKeySpec(init, "Blowfish");
			Cipher cipher = Cipher.getInstance(CIPHER_INSTANCE);
			cipher.init(Cipher.ENCRYPT_MODE, key);
			byte[] encrypted = cipher.doFinal(String.valueOf(password).getBytes(Charset.forName("UTF8")));
			encryptedString = byteArrayToHexString(encrypted);

			return encryptedString.toCharArray();
		}
		catch (Exception e)
		{
			throw new SecurityException("Unable to encrypt password", e);
		}
	}
1009343
public static String xor(String string, String key) {
        return xor(string, CryptUtils.stringToKey(string));
    }
1143482
public static String generateAccessKeyId() {
		// Based on http://stackoverflow.com/a/41156/535203
		final char[] accessKeyId = new char[ACCESS_KEY_ID_LENGTH];
		for (int i = 0; i < accessKeyId.length; i++) {
			accessKeyId[i] = SYMBOLS[SECURE_RANDOM.nextInt(SYMBOLS.length)];
		}
		return new String(accessKeyId);
	}
42117
public static ByteBuffer bytes(String s, Charset charset)
	    {
	        return ByteBuffer.wrap(s.getBytes(charset));
	    }
81401
public static SecretKey generateKey(final byte[] key) {
        return new SecretKey() {

            private byte[] k;

            {
                if (key.length == 16) {
                    k = key;
                } else {
                    k = new byte[16];
                    System.arraycopy(key, 0, k, 0, key.length < 16 ? key.length : 16);
                }
            }

            @Override
            public String getAlgorithm() {
                return "AES";
            }

            @Override
            public String getFormat() {
                return "RAW";
            }

            @Override
            public byte[] getEncoded() {
                return k;
            }
        };
    }
136343
public static String encodeUTF8(String chars)
    {
        try
        {
            return URLEncoder.encode(chars, "UTF-8");
        }
        catch (UnsupportedEncodingException e)
        {
            throw new AssertionError(e); // thanks JDK 1.4
        }
    }
614311
public String decryptString(String encryptedCredentials) {
        byte[] plaintextBytes = decryptBytes(encryptedCredentials);
        return new String(plaintextBytes, Charsets.UTF_8);
    }
705482
public final String getString(String charsetName) {
        if (position >= origin + limit) throw new IllegalArgumentException("limit excceed: " + position);

        byte[] buf = buffer;
        final int len = (0xff & buf[position]);
        if (position + len + 1 > origin + limit) throw new IllegalArgumentException("limit excceed: "
                                                                                    + (position + len + 1 - origin));

        try {
            String string = new String(buf, position + 1, len, charsetName);
            position += len + 1;
            return string;
        } catch (UnsupportedEncodingException e) {
            throw new IllegalArgumentException("Unsupported encoding: " + charsetName, e);
        }
    }
274356
public static String getCipher(Map<String, Object> parameters) {
    return (String)parameters.get(ENCRYPTION_CIPHER_KEY);
  }
649504
@Override
	public final Key keyFromPassword(char[] password) {
		try {
			SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
			int keyLength = Math.min(Cipher.getMaxAllowedKeyLength(algorithm), maximumKeyLength);
			KeySpec spec = new PBEKeySpec(password, salt, iterationCount, keyLength);
			SecretKey tmp = factory.generateSecret(spec);
			return new SecretKeySpec(tmp.getEncoded(), algorithm);
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException(e);
		} catch (InvalidKeySpecException e) {
			throw new RuntimeException(e);
		}
	}