Encrypts a string using the passed key. - Java Security

Java examples for Security:Key

Description

Encrypts a string using the passed key.

Demo Code

/*//from  w  w w .  j a v a2  s  .co m
 * -- $Id: Blowfish.java,v 1.4 2001/06/28 10:45:43 nikhil Exp $ --
 * ----------------------------------------------------------------------------
 * GNU LESSER GENERAL PUBLIC LICENSE Version 2.1, February 1999
 * ----------------------------------------------------------------------------
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version. This library is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
 * General Public License for more details. You should have received a copy of
 * the GNU Lesser General Public License along with this library; if not, write
 * to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307 USA -- Copyright (C) 2001 Auriga Logic Pvt. Ltd.
 * <http://www.aurigalogic.com> Author: Nikhil Gupte <ngupte@aurigalogic.com>
 */
//package com.java2s;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.security.Provider;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;

public class Main {
    public static void main(String[] argv) throws Exception {
        String cleartext = "java2s.com";
        String key = "java2s.com";
        System.out.println(encrypt(cleartext, key));
    }

    /**
     * Encrypts a string using the passed key.
     * <p/>
     * @param cleartext The clear text string to encrypt.
     * @param key The key to use for the encryption.
     * <p/>
     * @return a string representing the resulting ciphertext.
     */
    public static String encrypt(String cleartext, String key)
            throws Exception {
        return crypt(cleartext, key, Cipher.ENCRYPT_MODE);
    }

    private static String crypt(String input, String key, int mode)
            throws Exception {

        // Install SunJCE provider
        Provider sunJce = new com.sun.crypto.provider.SunJCE();
        Security.addProvider(sunJce);

        KeyGenerator kgen = KeyGenerator.getInstance("Blowfish");
        kgen.init(448);
        //       SecretKey skey = kgen.generateKey();

        byte[] raw = key.getBytes();
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "Blowfish");

        Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
        cipher.init(mode, skeySpec);

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ByteArrayInputStream bis = new ByteArrayInputStream(
                input.getBytes());
        CipherOutputStream cos = new CipherOutputStream(bos, cipher);

        int length = 0;
        byte[] buffer = new byte[8192];

        while ((length = bis.read(buffer)) != -1)
            cos.write(buffer, 0, length);

        bis.close();
        cos.close();

        return bos.toString();
    }
}

Related Tutorials