Example usage for javax.crypto Cipher getOutputSize

List of usage examples for javax.crypto Cipher getOutputSize

Introduction

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

Prototype

public final int getOutputSize(int inputLen) 

Source Link

Document

Returns the length in bytes that an output buffer would need to be in order to hold the result of the next update or doFinal operation, given the input length inputLen (in bytes).

Usage

From source file:acp.sdk.SecureUtil.java

/**
 * //ww w . j  a va  2s .c  om
 * @param privateKey
 * @param cryptPin
 * @return
 * @throws Exception
 */
public static byte[] decryptedPin(PrivateKey privateKey, byte[] cryptPin) throws Exception {

    try {
        /** ?PIN Block **/
        byte[] pinBlock = SecureUtil.base64Decode(cryptPin);
        // 
        //         Cipher cipher = CliperInstance.getInstance();
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        int blockSize = cipher.getBlockSize();
        int outputSize = cipher.getOutputSize(pinBlock.length);
        int leavedSize = pinBlock.length % blockSize;
        int blocksSize = leavedSize != 0 ? pinBlock.length / blockSize + 1 : pinBlock.length / blockSize;
        byte[] pinData = new byte[outputSize * blocksSize];
        int i = 0;
        while (pinBlock.length - i * blockSize > 0) {
            if (pinBlock.length - i * blockSize > blockSize) {
                cipher.doFinal(pinBlock, i * blockSize, blockSize, pinData, i * outputSize);
            } else {
                cipher.doFinal(pinBlock, i * blockSize, pinBlock.length - i * blockSize, pinData,
                        i * outputSize);
            }
            i++;
        }
        return pinData;
    } catch (Exception e) {
        LogUtil.writeErrorLog("", e);
    }
    return null;
}

From source file:acp.sdk.SecureUtil.java

/**
 * ???byte[]//  w ww  .j  ava2s  .co  m
 * 
 * @param publicKey
 * @param plainPin
 * @return
 * @throws Exception
 */
public static byte[] encryptedPin(PublicKey publicKey, byte[] plainPin) throws Exception {
    try {
        // y
        // Cipher cipher = Cipher.getInstance("DES",
        // new org.bouncycastle.jce.provider.BouncyCastleProvider());

        // 
        //         Cipher cipher = CliperInstance.getInstance();
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        int blockSize = cipher.getBlockSize();
        int outputSize = cipher.getOutputSize(plainPin.length);
        int leavedSize = plainPin.length % blockSize;
        int blocksSize = leavedSize != 0 ? plainPin.length / blockSize + 1 : plainPin.length / blockSize;
        byte[] raw = new byte[outputSize * blocksSize];
        int i = 0;
        while (plainPin.length - i * blockSize > 0) {
            if (plainPin.length - i * blockSize > blockSize) {
                cipher.doFinal(plainPin, i * blockSize, blockSize, raw, i * outputSize);
            } else {
                cipher.doFinal(plainPin, i * blockSize, plainPin.length - i * blockSize, raw, i * outputSize);
            }
            i++;
        }
        return raw;

        /*Cipher cipher = CliperInstance.getInstance();
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] output = cipher.doFinal(plainPin);
        return output;*/

    } catch (Exception e) {
        throw new Exception(e.getMessage());
    }
}

From source file:com.puyuntech.flowerToHome.plugin.unionpayPayment.SecureUtil.java

/**
 * /*from   w  w  w  .  j av a  2  s  .c  om*/
 * @param privateKey
 * @param cryptPin
 * @return
 * @throws Exception
 */
public static byte[] decryptedPin(PrivateKey privateKey, byte[] cryptPin) throws Exception {

    try {
        /** ?PIN Block **/
        byte[] pinBlock = SecureUtil.base64Decode(cryptPin);
        // 
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding",
                new org.bouncycastle.jce.provider.BouncyCastleProvider());
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        int blockSize = cipher.getBlockSize();
        int outputSize = cipher.getOutputSize(pinBlock.length);
        int leavedSize = pinBlock.length % blockSize;
        int blocksSize = leavedSize != 0 ? pinBlock.length / blockSize + 1 : pinBlock.length / blockSize;
        byte[] pinData = new byte[outputSize * blocksSize];
        int i = 0;
        while (pinBlock.length - i * blockSize > 0) {
            if (pinBlock.length - i * blockSize > blockSize) {
                cipher.doFinal(pinBlock, i * blockSize, blockSize, pinData, i * outputSize);
            } else {
                cipher.doFinal(pinBlock, i * blockSize, pinBlock.length - i * blockSize, pinData,
                        i * outputSize);
            }
            i++;
        }
        return pinData;
    } catch (Exception e) {
    }
    return null;
}

From source file:cn.usually.common.pay.union.sdk.SecureUtil.java

/**
 * //from  www. jav  a 2 s.  c  om
 * @param privateKey
 * @param cryptPin
 * @return
 * @throws Exception
 */
public static byte[] decryptedPin(PrivateKey privateKey, byte[] cryptPin) throws Exception {

    try {
        /** ?PIN Block **/
        byte[] pinBlock = SecureUtil.base64Decode(cryptPin);
        // 
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding",
                new org.bouncycastle.jce.provider.BouncyCastleProvider());
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        int blockSize = cipher.getBlockSize();
        int outputSize = cipher.getOutputSize(pinBlock.length);
        int leavedSize = pinBlock.length % blockSize;
        int blocksSize = leavedSize != 0 ? pinBlock.length / blockSize + 1 : pinBlock.length / blockSize;
        byte[] pinData = new byte[outputSize * blocksSize];
        int i = 0;
        while (pinBlock.length - i * blockSize > 0) {
            if (pinBlock.length - i * blockSize > blockSize) {
                cipher.doFinal(pinBlock, i * blockSize, blockSize, pinData, i * outputSize);
            } else {
                cipher.doFinal(pinBlock, i * blockSize, pinBlock.length - i * blockSize, pinData,
                        i * outputSize);
            }
            i++;
        }
        return pinData;
    } catch (Exception e) {
        LogUtil.writeErrorLog("", e);
    }
    return null;
}

From source file:unionpayUtil.sdk.SecureUtil.java

/**
 * /* ww w . ja  v  a2 s .co  m*/
 * @param privateKey
 * @param cryptPin
 * @return
 * @throws Exception
 */
public static byte[] decryptedPin(PrivateKey privateKey, byte[] cryptPin) throws Exception {

    try {
        /** ?PIN Block **/
        byte[] pinBlock = SecureUtil.base64Decode(cryptPin);
        // ?
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding",
                new org.bouncycastle.jce.provider.BouncyCastleProvider());
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        int blockSize = cipher.getBlockSize();
        int outputSize = cipher.getOutputSize(pinBlock.length);
        int leavedSize = pinBlock.length % blockSize;
        int blocksSize = leavedSize != 0 ? pinBlock.length / blockSize + 1 : pinBlock.length / blockSize;
        byte[] pinData = new byte[outputSize * blocksSize];
        int i = 0;
        while (pinBlock.length - i * blockSize > 0) {
            if (pinBlock.length - i * blockSize > blockSize) {
                cipher.doFinal(pinBlock, i * blockSize, blockSize, pinData, i * outputSize);
            } else {
                cipher.doFinal(pinBlock, i * blockSize, pinBlock.length - i * blockSize, pinData,
                        i * outputSize);
            }
            i++;
        }
        return pinData;
    } catch (Exception e) {
        LogUtil.writeErrorLog("", e);
    }
    return null;
}

From source file:com.wxsoft.framework.util.unionpay.SecureUtil.java

/**
 * /*from   w  w  w. j  ava2 s .c  o  m*/
 * @param privateKey
 * @param cryptPin
 * @return
 * @throws Exception
 */
public static byte[] decryptedPin(PrivateKey privateKey, byte[] cryptPin) throws Exception {

    try {
        /** ?PIN Block **/
        byte[] pinBlock = SecureUtil.base64Decode(cryptPin);
        // 
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding",
                new org.bouncycastle.jce.provider.BouncyCastleProvider());
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        int blockSize = cipher.getBlockSize();
        int outputSize = cipher.getOutputSize(pinBlock.length);
        int leavedSize = pinBlock.length % blockSize;
        int blocksSize = leavedSize != 0 ? pinBlock.length / blockSize + 1 : pinBlock.length / blockSize;
        byte[] pinData = new byte[outputSize * blocksSize];
        int i = 0;
        while (pinBlock.length - i * blockSize > 0) {
            if (pinBlock.length - i * blockSize > blockSize) {
                cipher.doFinal(pinBlock, i * blockSize, blockSize, pinData, i * outputSize);
            } else {
                cipher.doFinal(pinBlock, i * blockSize, pinBlock.length - i * blockSize, pinData,
                        i * outputSize);
            }
            i++;
        }
        return pinData;
    } catch (Exception e) {
        logger.error("", e);
    }
    return null;
}

From source file:edu.tamu.tcat.crypto.spongycastle.SecureTokenImpl.java

@Override
public String getToken(ByteBuffer content) throws TokenException {
    try {/*ww w  .j  a v  a2 s.c  om*/
        byte[] token = createToken(content.slice());
        byte[] iv = createIV();
        Cipher cipher = createCipher(Cipher.ENCRYPT_MODE, iv);
        int outputSize = cipher.getOutputSize(token.length);
        byte[] encrypted = new byte[outputSize + (ivSize / 8)];
        System.arraycopy(iv, 0, encrypted, 0, iv.length);
        cipher.doFinal(token, 0, token.length, encrypted, iv.length);
        String encoded = Base64.encodeBase64URLSafeString(encrypted);
        return encoded;
    } catch (NoSuchAlgorithmException e) {
        throw new TokenException("Missing algorithm", e);
    } catch (IllegalBlockSizeException e) {
        throw new TokenException(
                "Should never happen but is thrown because Sun/Oracle doesn't understand that encryption/decryption modes are different and should really have different APIs.  This is a decrypt only problem",
                e);
    } catch (BadPaddingException e) {
        throw new TokenException(
                "Should never happen but is thrown because Sun/Oracle doesn't understand that encryption/decryption modes are different and should really have different APIs.  This is a decrypt only problem",
                e);
    } catch (ShortBufferException e) {
        throw new TokenException("Should never happen", e);
    }
}

From source file:edu.tamu.tcat.crypto.bouncycastle.SecureTokenImpl.java

@Override
public String getToken(ByteBuffer content) throws TokenException {
    try {/*from  w  w  w .  java 2s  . co m*/
        byte[] token = createToken(content.slice());
        byte[] iv = createIV();
        Cipher cipher = createCipher(Cipher.ENCRYPT_MODE, iv);
        int outputSize = cipher.getOutputSize(token.length);
        // The token value returned contains the IV followed by the encrypted payload
        byte[] encrypted = new byte[outputSize + (ivSize / 8)];
        System.arraycopy(iv, 0, encrypted, 0, iv.length);
        cipher.doFinal(token, 0, token.length, encrypted, iv.length);
        String encoded = Base64.encodeBase64URLSafeString(encrypted);
        return encoded;
    } catch (NoSuchAlgorithmException e) {
        throw new TokenException("Missing algorithm", e);
    } catch (IllegalBlockSizeException e) {
        throw new TokenException(
                "Should never happen but is thrown because Sun/Oracle doesn't understand that encryption/decryption modes are different and should really have different APIs.  This is a decrypt only problem",
                e);
    } catch (BadPaddingException e) {
        throw new TokenException(
                "Should never happen but is thrown because Sun/Oracle doesn't understand that encryption/decryption modes are different and should really have different APIs.  This is a decrypt only problem",
                e);
    } catch (ShortBufferException e) {
        throw new TokenException("Should never happen", e);
    }
}

From source file:com.zotoh.core.crypto.JavaOfuscator.java

private String encrypt(String clearText) throws Exception {
    if (isEmpty(clearText)) {
        return clearText;
    }/*from w w  w  .j  a  v a 2  s  .  c o m*/
    Cipher c = getCipher(Cipher.ENCRYPT_MODE);
    ByteOStream baos = new ByteOStream();
    byte[] p = asBytes(clearText);
    byte[] out = new byte[Math.max(4096, c.getOutputSize(p.length))];
    int n = c.update(p, 0, p.length, out, 0);
    if (n > 0) {
        baos.write(out, 0, n);
    }
    n = c.doFinal(out, 0);
    if (n > 0) {
        baos.write(out, 0, n);
    }

    return Base64.encodeBase64URLSafeString(baos.asBytes());
}

From source file:com.zotoh.core.crypto.JavaOfuscator.java

private String decrypt(String encoded) throws Exception {
    if (isEmpty(encoded)) {
        return encoded;
    }// w  w  w.j a va  2s  .c o  m
    Cipher c = getCipher(Cipher.DECRYPT_MODE);
    ByteOStream baos = new ByteOStream();
    byte[] p = Base64.decodeBase64(encoded);
    byte[] out = new byte[Math.max(4096, c.getOutputSize(p.length))];
    int n = c.update(p, 0, p.length, out, 0);
    if (n > 0) {
        baos.write(out, 0, n);
    }
    n = c.doFinal(out, 0);
    if (n > 0) {
        baos.write(out, 0, n);
    }

    return asString(baos.asBytes());
}