Example usage for javax.crypto Cipher update

List of usage examples for javax.crypto Cipher update

Introduction

In this page you can find the example usage for javax.crypto Cipher update.

Prototype

public final int update(byte[] input, int inputOffset, int inputLen, byte[] output)
        throws ShortBufferException 

Source Link

Document

Continues a multiple-part encryption or decryption operation (depending on how this cipher was initialized), processing another data part.

Usage

From source file:DataStudioCrak.java

public static void main(String[] args) {
    String company = "labthink";
    byte[] companyByteArray = company.getBytes();
    byte[] companyByteIntArray = intToByteArray(compute(companyByteArray, companyByteArray.length));
    // byte[] ff = "zhulixia".getBytes();
    byte[] snByte = new byte[32];
    byte[] byte1 = new byte[] { 7, 1 };
    byte[] byte2 = "zhaodapengpojiehahahahahaha".getBytes();
    byte[] byte3 = new byte[] { 127 };
    byte[] snMain = new byte[24];
    System.arraycopy(byte1, 0, snMain, 0, 2);
    System.arraycopy(byte2, 0, snMain, 2, 17);
    System.arraycopy(companyByteIntArray, 0, snMain, 19, 4);
    System.arraycopy(byte3, 0, snMain, 23, 1);
    //  1 - single license,2 - site license ,3 educational license
    snMain[2] = (byte) 1;
    int intSn = compute(snMain, snMain.length);
    System.out.println("intSn=" + intSn);
    byte[] key1 = "dddd".getBytes();
    byte[] key2 = intToByteArray(intSn);
    byte[] key = new byte[8];
    System.arraycopy(key1, 0, key, 0, 4);
    System.arraycopy(key2, 0, key, 4, 4);

    byte encodedSnMain[] = new byte[snMain.length];
    try {//from   ww  w.  jav  a 2s  . c  o m
        DESKeySpec deskeyspec = new DESKeySpec(key);
        javax.crypto.SecretKey secretkey = SecretKeyFactory.getInstance("DES").generateSecret(deskeyspec);
        Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, secretkey);
        cipher.update(snMain, 0, snMain.length, encodedSnMain);
        cipher.doFinal();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    System.arraycopy(key1, 0, snByte, 0, 4);
    System.arraycopy(key2, 0, snByte, 28, 4);
    System.arraycopy(encodedSnMain, 0, snByte, 4, 24);
    char[] snCharArray = Hex.encodeHex(snByte);
    String sn = new String(snCharArray);
    System.out.println("sn=" + sn);
}

From source file:net.labthink.run.DataStudioCrak.java

public static void main(String[] args) {
    String company = "Labthink";
    byte[] companyByteArray = company.getBytes();
    byte[] companyByteIntArray = intToByteArray(compute(companyByteArray, companyByteArray.length));
    // byte[] ff = "zhulixia".getBytes();
    byte[] snByte = new byte[32];
    byte[] byte1 = new byte[] { 7, 1 };
    byte[] byte2 = "zhaodapengpojiehahahahahaha".getBytes();
    byte[] byte3 = new byte[] { 127 };
    byte[] snMain = new byte[24];
    System.arraycopy(byte1, 0, snMain, 0, 2);
    System.arraycopy(byte2, 0, snMain, 2, 17);
    System.arraycopy(companyByteIntArray, 0, snMain, 19, 4);
    System.arraycopy(byte3, 0, snMain, 23, 1);
    //  1 - single license,2 - site license ,3 educational license
    snMain[2] = (byte) 1;
    int intSn = compute(snMain, snMain.length);
    System.out.println("intSn=" + intSn);
    byte[] key1 = "dddd".getBytes();
    byte[] key2 = intToByteArray(intSn);
    byte[] key = new byte[8];
    System.arraycopy(key1, 0, key, 0, 4);
    System.arraycopy(key2, 0, key, 4, 4);

    byte encodedSnMain[] = new byte[snMain.length];
    try {//from   ww  w  .  ja v a2 s.c o m
        DESKeySpec deskeyspec = new DESKeySpec(key);
        javax.crypto.SecretKey secretkey = SecretKeyFactory.getInstance("DES").generateSecret(deskeyspec);
        Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, secretkey);
        cipher.update(snMain, 0, snMain.length, encodedSnMain);
        cipher.doFinal();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    System.arraycopy(key1, 0, snByte, 0, 4);
    System.arraycopy(key2, 0, snByte, 28, 4);
    System.arraycopy(encodedSnMain, 0, snByte, 4, 24);
    char[] snCharArray = Hex.encodeHex(snByte);
    String sn = new String(snCharArray);
    System.out.println("sn=" + sn);
}

From source file:AESTest.java

/**
 * Uses a cipher to transform the bytes in an input stream and sends the transformed bytes to an
 * output stream./*from   w  ww .  j  a  va  2 s . c om*/
 * @param in the input stream
 * @param out the output stream
 * @param cipher the cipher that transforms the bytes
 */
public static void crypt(InputStream in, OutputStream out, Cipher cipher)
        throws IOException, GeneralSecurityException {
    int blockSize = cipher.getBlockSize();
    int outputSize = cipher.getOutputSize(blockSize);
    byte[] inBytes = new byte[blockSize];
    byte[] outBytes = new byte[outputSize];

    int inLength = 0;
    boolean more = true;
    while (more) {
        inLength = in.read(inBytes);
        if (inLength == blockSize) {
            int outLength = cipher.update(inBytes, 0, blockSize, outBytes);
            out.write(outBytes, 0, outLength);
        } else
            more = false;
    }
    if (inLength > 0)
        outBytes = cipher.doFinal(inBytes, 0, inLength);
    else
        outBytes = cipher.doFinal();
    out.write(outBytes);
}

From source file:RSATest.java

/**
 * Uses a cipher to transform the bytes in an input stream and sends the transformed bytes to an
 * output stream.//  w w w  .j a  v a 2s .com
 * @param in the input stream
 * @param out the output stream
 * @param cipher the cipher that transforms the bytes
 */
public static void crypt(InputStream in, OutputStream out, Cipher cipher)
        throws IOException, GeneralSecurityException {
    int blockSize = cipher.getBlockSize();
    int outputSize = cipher.getOutputSize(blockSize);
    byte[] inBytes = new byte[blockSize];
    byte[] outBytes = new byte[outputSize];

    int inLength = 0;
    ;
    boolean more = true;
    while (more) {
        inLength = in.read(inBytes);
        if (inLength == blockSize) {
            int outLength = cipher.update(inBytes, 0, blockSize, outBytes);
            out.write(outBytes, 0, outLength);
        } else
            more = false;
    }
    if (inLength > 0)
        outBytes = cipher.doFinal(inBytes, 0, inLength);
    else
        outBytes = cipher.doFinal();
    out.write(outBytes);
}

From source file:ai.serotonin.backup.Base.java

void cipherizeFile(final File inFile, final File outFile, final Cipher cipher) throws Exception {
    try (final FileInputStream fis = new FileInputStream(inFile);
            final FileOutputStream fos = new FileOutputStream(outFile)) {
        final byte[] inbuf = new byte[1028];
        final byte[] outbuf = new byte[2056];
        int readCount, encCount;
        while ((readCount = fis.read(inbuf)) != -1) {
            encCount = cipher.update(inbuf, 0, readCount, outbuf);
            fos.write(outbuf, 0, encCount);
        }//from  w  w w.  ja va  2  s. c  om

        encCount = cipher.doFinal(inbuf, 0, 0, outbuf);
        fos.write(outbuf, 0, encCount);
    }
}

From source file:org.auscope.portal.server.web.controllers.GridLoginController.java

/**
 * Uses a cipher to decrypt data from an input stream.
 *///from w  w  w. ja  va 2s. co m
private String decryptString(InputStream in, Cipher cipher) throws GeneralSecurityException, IOException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    int blockSize = cipher.getBlockSize();
    int outputSize = cipher.getOutputSize(blockSize);
    byte[] inBlock = new byte[blockSize];
    byte[] outBlock = new byte[outputSize];
    int bytesRead;
    do {
        bytesRead = in.read(inBlock);
        if (bytesRead == blockSize) {
            int len = cipher.update(inBlock, 0, blockSize, outBlock);
            output.write(outBlock, 0, len);
        }
    } while (bytesRead == blockSize);

    if (bytesRead > 0) {
        outBlock = cipher.doFinal(inBlock, 0, bytesRead);
    } else {
        outBlock = cipher.doFinal();
    }
    output.write(outBlock);
    return output.toString();
}

From source file:com.slamd.admin.AdminServlet.java

/**
 * Generates page content based on an MD5-digest of the query string.
 *
 * @param  requestInfo   The state information for this request.
 * @param  digestString  The base64-encoded MD5 digest of the query string to
 *                       use to generate the page.
 *//* w ww. j av a  2 s  . c o  m*/
static void generatePageFromMD5(RequestInfo requestInfo, String digestString) {
    try {
        String dataFile = Constants.MD5_CONTENT_BASE_PATH + '/' + digestString;
        InputStream inputStream = slamdServer.getClass().getClassLoader().getResourceAsStream(dataFile);

        byte[] salt = { 0, 0, 0, 0, 0, 0, 0, 0 };
        char[] queryChars = requestInfo.request.getQueryString().toCharArray();
        int iterations = 1000;
        String cipherName = "PBEWithMD5AndDES";
        StringBuilder htmlBody = requestInfo.htmlBody;

        AlgorithmParameters algorithmParams = AlgorithmParameters.getInstance(cipherName);
        algorithmParams.init(new PBEParameterSpec(salt, iterations));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(cipherName);
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(queryChars));
        Cipher cipher = Cipher.getInstance(cipherName);
        cipher.init(Cipher.DECRYPT_MODE, key, algorithmParams);

        int bytesIn;
        int bytesOut;
        byte[] inBuffer = new byte[4096];
        byte[] outBuffer = new byte[8192];
        while ((bytesIn = inputStream.read(inBuffer)) > 0) {
            bytesOut = cipher.update(inBuffer, 0, bytesIn, outBuffer);
            htmlBody.append(new String(outBuffer, 0, bytesOut));
        }

        htmlBody.append(new String(cipher.doFinal()));
        inputStream.close();
    } catch (Exception e) {
        requestInfo.htmlBody.append(JobClass.stackTraceToString(e));
    }
}