Example usage for org.apache.shiro.codec CodecSupport toBytes

List of usage examples for org.apache.shiro.codec CodecSupport toBytes

Introduction

In this page you can find the example usage for org.apache.shiro.codec CodecSupport toBytes.

Prototype

protected byte[] toBytes(InputStream in) 

Source Link

Document

Converts the specified InputStream InputStream into a byte array.

Usage

From source file:cn.powerdash.libsystem.common.security.util.CryptoUtil.java

License:Open Source License

public static String encrypt(String plainText, String key) {
    AesCipherService aes = new AesCipherService();
    byte[] keyBytes = CodecSupport.toBytes(key);
    ByteSource encryptedBytes = aes.encrypt(CodecSupport.toBytes(plainText), keyBytes);
    return encryptedBytes.toString();
}

From source file:cn.powerdash.libsystem.common.security.util.CryptoUtil.java

License:Open Source License

public static String decrypt(String cipherText, String key) {
    AesCipherService aesCs = new AesCipherService();
    byte[] keyBytes = CodecSupport.toBytes(key);

    ByteSource decryptedBytes = aesCs.decrypt(Base64.decode(CodecSupport.toBytes(cipherText)), keyBytes);
    return CodecSupport.toString(decryptedBytes.getBytes());
}

From source file:com.rencw.framework.shiro.SimpleByteSource.java

License:Apache License

/**
 * Creates an instance by converting the characters to a byte array (assumes UTF-8 encoding).
 *
 * @param chars the source characters to use to create the underlying byte array.
 * @since 1.1//from   w  w w  . ja  v a  2  s .c  om
 */
public SimpleByteSource(char[] chars) {
    this.bytes = CodecSupport.toBytes(chars);
}

From source file:com.rencw.framework.shiro.SimpleByteSource.java

License:Apache License

/**
 * Creates an instance by converting the String to a byte array (assumes UTF-8 encoding).
 *
 * @param string the source string to convert to a byte array (assumes UTF-8 encoding).
 * @since 1.1/*from   www.ja  va2  s. c o m*/
 */
public SimpleByteSource(String string) {
    this.bytes = CodecSupport.toBytes(string);
}

From source file:com.sanweibook.lingdu.shiro.util.MySimpleByteSource.java

License:Apache License

/**
 * Creates an instance by converting the characters to a byte array (assumes UTF-8 encoding).
 *
 * @param chars the source characters to use to create the underlying byte array.
 * @since 1.1/*www .  j  a  v a  2 s .c o  m*/
 */
public MySimpleByteSource(char[] chars) {
    this.bytes = CodecSupport.toBytes(chars);
}

From source file:com.sanweibook.lingdu.shiro.util.MySimpleByteSource.java

License:Apache License

/**
 * Creates an instance by converting the String to a byte array (assumes UTF-8 encoding).
 *
 * @param string the source string to convert to a byte array (assumes UTF-8 encoding).
 * @since 1.1//ww  w  .  j  a  v a  2s  . co m
 */
public MySimpleByteSource(String string) {
    this.bytes = CodecSupport.toBytes(string);
}

From source file:de.dominikschadow.javasecurity.symmetric.AES.java

License:Apache License

public static void main(String[] args) {
    final String initialText = "AES encryption sample text";
    final char[] keystorePassword = "samples".toCharArray();
    final String keyAlias = "symmetric-sample";
    final char[] keyPassword = "symmetric-sample".toCharArray();

    try {//ww w  . j  av  a  2  s  .c o  m
        KeyStore ks = loadKeystore(keystorePassword);
        Key key = loadKey(ks, keyAlias, keyPassword);
        byte[] ciphertext = encrypt(key, CodecSupport.toBytes(initialText));
        byte[] plaintext = decrypt(key, ciphertext);

        printReadableMessages(initialText, ciphertext, plaintext);
    } catch (NoSuchAlgorithmException | KeyStoreException | CertificateException | UnrecoverableKeyException
            | IOException ex) {
        log.error(ex.getMessage(), ex);
    }
}

From source file:de.dominikschadow.javasecurity.symmetric.AESEncryptionSample.java

License:Apache License

public static void main(String[] args) {
    AESEncryptionSample res = new AESEncryptionSample();
    final String initialText = "AES encryption sample text";
    final char[] keystorePassword = "samples".toCharArray();
    final String keyAlias = "symmetric-sample";
    final char[] keyPassword = "symmetric-sample".toCharArray();

    try {/*  www.  j  av  a2 s .  c o m*/
        KeyStore ks = res.loadKeystore(KEYSTORE_PATH, keystorePassword);
        Key key = res.loadKey(ks, keyAlias, keyPassword);
        byte[] ciphertext = res.encrypt(key, CodecSupport.toBytes(initialText));
        byte[] plaintext = res.decrypt(key, ciphertext);

        res.printReadableMessages(initialText, ciphertext, plaintext);
    } catch (NoSuchAlgorithmException | KeyStoreException | CertificateException | UnrecoverableKeyException
            | IOException ex) {
        logger.error(ex.getMessage(), ex);
    }
}

From source file:org.egomez.irpgeditor.env.AS400Systems.java

License:Open Source License

/**
 * saves the system settings./*from   w  ww.jav  a  2  s  .c  o  m*/
 */
public void saveSettings() throws IOException {
    // FileOutputStream fos;
    Properties props;
    AS400System system;

    AesCipherService cipher = new AesCipherService();
    Key key = cipher.generateNewKey();
    byte[] secretBytes = null;
    ByteSource encrypted = null;

    props = new Properties();
    props.setProperty("system.count", Integer.toString(listSystems.size()));
    system = getDefault();
    if (system != null) {
        props.setProperty("system.default", system.getName());
    }
    for (int x = 0; x < listSystems.size(); x++) {
        system = listSystems.get(x);
        props.setProperty("system." + x + ".name", system.getName());
        props.setProperty("system." + x + ".address", system.getAddress());
        props.setProperty("system." + x + ".user", system.getUser());
        props.setProperty("system." + x + ".properties", Base64.encodeBytes(key.getEncoded()));
        // Encriptamos la clave
        secretBytes = CodecSupport.toBytes(system.getPassword());
        encrypted = cipher.encrypt(secretBytes, key.getEncoded());
        props.setProperty("system." + x + ".password", Base64.encodeBytes(encrypted.getBytes()));
    }
    // Creamos los directorios
    File file = new File(System.getProperty("user.home") + File.separator + ".iRPGEditor" + File.separator
            + "conf" + File.separator + "systems.properties");
    if (file.exists() == false) {
        file = new File(System.getProperty("user.home") + File.separator + ".iRPGEditor");
        file.mkdir();
        file = new File(
                System.getProperty("user.home") + File.separator + ".iRPGEditor" + File.separator + "conf");
        file.mkdir();
        file = new File(
                System.getProperty("user.home") + File.separator + ".iRPGEditor" + File.separator + "projects");
        file.mkdir();
    }
    file = new File(System.getProperty("user.home") + File.separator + ".iRPGEditor" + File.separator + "conf"
            + File.separator + "systems.properties");

    // fos = new FileOutputStream(System.getProperty("user.home") +
    // File.separator + ".iRPGEditor" + File.separator
    // + "conf" + File.separator + "systems.properties");
    save(props, file);
    // props.store(fos, "");
}

From source file:org.lunifera.examples.kwiee.erp.module.core.services.internal.AdministrationServiceComponent.java

License:Open Source License

private String encodePassword(String password2Encode) {

    byte[] saltKey;
    int encryptedSecretLenght;
    int saltKeyLenght;

    BlowfishCipherService cipher = new BlowfishCipherService();

    // generate key with default 256 bits size
    saltKey = cipher.generateNewKey(256).getEncoded();
    saltKeyLenght = saltKey.length;// w ww.jav a 2  s  .  c  o  m

    // encrypt the secret
    ByteSource encryptedSecret = cipher.encrypt(CodecSupport.toBytes(password2Encode), saltKey);
    encryptedSecretLenght = encryptedSecret.getBytes().length;

    // create a destination array that is the size of the two arrays
    byte[] destination = new byte[saltKeyLenght + encryptedSecretLenght];

    // copy ciphertext into start of destination (from pos 0, copy
    // ciphertext.length bytes)
    System.arraycopy(encryptedSecret.getBytes(), 0, destination, 0, encryptedSecretLenght);

    // copy mac into end of destination (from pos ciphertext.length, copy
    // mac.length bytes)
    System.arraycopy(saltKey, 0, destination, encryptedSecretLenght, saltKeyLenght);

    return Base64.encodeToString(destination);

}