Example usage for org.bouncycastle.crypto PBEParametersGenerator PKCS5PasswordToBytes

List of usage examples for org.bouncycastle.crypto PBEParametersGenerator PKCS5PasswordToBytes

Introduction

In this page you can find the example usage for org.bouncycastle.crypto PBEParametersGenerator PKCS5PasswordToBytes.

Prototype

public static byte[] PKCS5PasswordToBytes(char[] password) 

Source Link

Document

converts a password to a byte array according to the scheme in PKCS5 (ascii, no padding)

Usage

From source file:org.jruby.ext.openssl.x509store.PEMInputOutput.java

License:LGPL

private static byte[] decrypt(byte[] decoded, String dekInfo, char[] passwd)
        throws IOException, GeneralSecurityException {
    if (passwd == null) {
        throw new IOException("Password is null, but a password is required");
    }//www.  j  a  v  a2 s . com
    StringTokenizer tknz = new StringTokenizer(dekInfo, ",");
    String algorithm = tknz.nextToken();
    byte[] iv = Hex.decode(tknz.nextToken());
    if (!CipherModule.isSupportedCipher(algorithm)) {
        throw new IOException("Unknown algorithm: " + algorithm);
    }
    String[] cipher = org.jruby.ext.openssl.Cipher.Algorithm.osslToJsse(algorithm);
    String realName = cipher[3];
    int[] lengths = org.jruby.ext.openssl.Cipher.Algorithm.osslKeyIvLength(algorithm);
    int keyLen = lengths[0];
    int ivLen = lengths[1];
    if (iv.length != ivLen) {
        throw new IOException("Illegal IV length");
    }
    byte[] salt = new byte[8];
    System.arraycopy(iv, 0, salt, 0, 8);
    OpenSSLPBEParametersGenerator pGen = new OpenSSLPBEParametersGenerator();
    pGen.init(PBEParametersGenerator.PKCS5PasswordToBytes(passwd), salt);
    KeyParameter param = (KeyParameter) pGen.generateDerivedParameters(keyLen * 8);
    SecretKey secretKey = new javax.crypto.spec.SecretKeySpec(param.getKey(), realName);
    Cipher c = Cipher.getInstance(realName);
    c.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv));
    return c.doFinal(decoded);
}

From source file:org.opcfoundation.ua.utils.BouncyCastleUtils.java

License:Open Source License

/**
 * Converts a password to a byte array according to the scheme in PKCS5 (ascii, no padding)
 * @param password a character array representing the password.
 * @return a byte array representing the password.
 *//*from  w  w  w .ja v a2  s.c  o  m*/
public static byte[] PKCS5PasswordToBytes(char[] password) {
    return PBEParametersGenerator.PKCS5PasswordToBytes(password);
}

From source file:org.xwiki.crypto.password.PasswordToByteConverter.java

License:Open Source License

/**
 * Convert password to bytes./*from   w w w .j  av a 2  s. c o  m*/
 *
 * @param password password to convert.
 * @param mode mode of conversion.
 * @return a bytes array representing the password.
 */
public static byte[] convert(char[] password, ToBytesMode mode) {
    byte[] passwd;

    switch (mode) {
    case PKCS12:
        passwd = PBEParametersGenerator.PKCS12PasswordToBytes(password);
        break;
    case PKCS5:
        passwd = PBEParametersGenerator.PKCS5PasswordToBytes(password);
        break;
    default:
        passwd = PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(password);
        break;
    }

    return passwd;
}