Example usage for org.bouncycastle.crypto.engines DESEngine processBlock

List of usage examples for org.bouncycastle.crypto.engines DESEngine processBlock

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.engines DESEngine processBlock.

Prototype

public int processBlock(byte[] in, int inOff, byte[] out, int outOff) 

Source Link

Usage

From source file:org.alfresco.jlan.client.J2MEPasswordEncryptor.java

License:Open Source License

/**
 * P16 encryption/*from w  w w.  ja va 2 s.c  o m*/
 *
 * @param pwd java.lang.String
 * @param s8 byte[]
 * @return byte[]
* @exception NoSuchAlgorithmException   If a required encryption algorithm is not available
 */
public final byte[] P16(String pwd, byte[] s8) throws NoSuchAlgorithmException {

    //   Make a 14 byte string using the password string. Truncate the
    //   password or pad with nulls to 14 characters.

    StringBuffer p14str = new StringBuffer();
    p14str.append(pwd.toUpperCase());
    if (p14str.length() > 14)
        p14str.setLength(14);

    while (p14str.length() < 14)
        p14str.append((char) 0x00);

    //   Convert the P14 string to an array of bytes. Allocate the return 16 byte array.

    byte[] p14 = p14str.toString().getBytes();
    byte[] p16 = new byte[16];

    try {

        //   DES encrypt the password bytes using the challenge key

        DESEngine des = new DESEngine();

        //   Set the encryption seed using the first 7 bytes of the password string.
        //   Generate the first 8 bytes of the return value.

        byte[] key = generateKey(p14, 0);

        KeyParameter chKey = new KeyParameter(key);
        des.init(true, chKey);
        des.processBlock(s8, 0, p16, 0);

        //   Encrypt the second block

        key = generateKey(p14, 7);

        chKey = new KeyParameter(key);
        des.init(true, chKey);
        des.processBlock(s8, 0, p16, 8);
    } catch (Exception ex) {
        p16 = null;
    }

    //   Return the 16 byte encrypted value

    return p16;
}

From source file:org.alfresco.jlan.client.J2MEPasswordEncryptor.java

License:Open Source License

/**
 * P24 DES encryption//from ww w  .ja  v  a2s.  c  o  m
 *
 * @param p21      Plain password or hashed password bytes
 * @param ch      Challenge bytes
 * @return         Encrypted password
 * @exception NoSuchAlgorithmException   If a required encryption algorithm is not available
 */
protected byte[] P24(byte[] p21, byte[] ch) throws NoSuchAlgorithmException {

    byte[] enc = null;

    try {

        //   DES encrypt the password bytes using the challenge key

        DESEngine des = new DESEngine();

        //   Allocate the output bytes

        enc = new byte[24];

        //   Encrypt the first block

        byte[] key = generateKey(p21, 0);

        KeyParameter chKey = new KeyParameter(key);
        des.init(true, chKey);
        des.processBlock(ch, 0, enc, 0);

        //   Encrypt the second block

        key = generateKey(p21, 7);

        chKey = new KeyParameter(key);
        des.init(true, chKey);
        des.processBlock(ch, 0, enc, 8);

        //   Encrypt the last block

        key = generateKey(p21, 14);

        chKey = new KeyParameter(key);
        des.init(true, chKey);
        des.processBlock(ch, 0, enc, 16);
    } catch (Exception ex) {
        ex.printStackTrace();
        enc = null;
    }

    //   Return the encrypted password, or null if an error occurred

    return enc;
}