List of usage examples for javax.crypto Cipher getBlockSize
public final int getBlockSize()
From source file:acp.sdk.SecureUtil.java
/** * ???byte[]//w w w . j a v a 2 s . c o 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.forsrc.utils.MyRsa2Utils.java
/** * Decrypt string.//from w ww.j av a 2 s . co m * * @param privateKey the private key * @param cipherText the cipher text * @return the string * @throws RsaException the rsa exception */ public static String decrypt(PrivateKey privateKey, String cipherText) throws RsaException { Cipher cipher = null; try { cipher = Cipher.getInstance(RsaKey.ALGORITHM, new org.bouncycastle.jce.provider.BouncyCastleProvider()); } catch (NoSuchAlgorithmException e) { throw new RsaException(e); } catch (NoSuchPaddingException e) { throw new RsaException(e); } try { cipher.init(Cipher.DECRYPT_MODE, privateKey); } catch (InvalidKeyException e) { throw new RsaException(e); } byte[] input = null; try { input = new Base64().decode(cipherText); } catch (Exception e) { throw new RsaException(e); } ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); try { int blockSize = cipher.getBlockSize(); blockSize = blockSize == 0 ? 117 : blockSize; int i = 0; int start = 0; do { start = i++ * blockSize; baos.write(cipher.doFinal(input, start, blockSize)); } while (input.length - start - blockSize > 0); } catch (IllegalBlockSizeException e) { throw new RsaException(e); } catch (BadPaddingException e) { throw new RsaException(e); } catch (IOException e) { throw new RsaException(e); } return new String(baos.toByteArray()); }
From source file:com.puyuntech.flowerToHome.plugin.unionpayPayment.SecureUtil.java
/** * //from ww w . j a va2 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) { } return null; }
From source file:cn.usually.common.pay.union.sdk.SecureUtil.java
/** * /*from w ww . j av a 2 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) { LogUtil.writeErrorLog("", e); } return null; }
From source file:unionpayUtil.sdk.SecureUtil.java
/** * /*from w w w . ja v a 2 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) { LogUtil.writeErrorLog("", e); } return null; }
From source file:com.wxsoft.framework.util.unionpay.SecureUtil.java
/** * /*w w w. j ava 2 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) { logger.error("", e); } return null; }
From source file:com.rr.familyPlanning.ui.security.encryptObject.java
/** * Encrypts and encodes the Object and IV for url inclusion * * @param input//w w w. jav a 2s . c om * @return * @throws Exception */ public String[] encryptObject(Object obj) throws Exception { ByteArrayOutputStream stream = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(stream); try { // Serialize the object out.writeObject(obj); byte[] serialized = stream.toByteArray(); // Setup the cipher and Init Vector Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); byte[] iv = new byte[cipher.getBlockSize()]; new SecureRandom().nextBytes(iv); IvParameterSpec ivSpec = new IvParameterSpec(iv); // Hash the key with SHA-256 and trim the output to 128-bit for the key MessageDigest digest = MessageDigest.getInstance("SHA-256"); digest.update(keyString.getBytes()); byte[] key = new byte[16]; System.arraycopy(digest.digest(), 0, key, 0, key.length); SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); // encrypt cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); // Encrypt & Encode the input byte[] encrypted = cipher.doFinal(serialized); byte[] base64Encoded = Base64.encodeBase64(encrypted); String base64String = new String(base64Encoded); String urlEncodedData = URLEncoder.encode(base64String, "UTF-8"); // Encode the Init Vector byte[] base64IV = Base64.encodeBase64(iv); String base64IVString = new String(base64IV); String urlEncodedIV = URLEncoder.encode(base64IVString, "UTF-8"); return new String[] { urlEncodedData, urlEncodedIV }; } finally { stream.close(); out.close(); } }
From source file:CipherSocket.java
public OutputStream getOutputStream() throws IOException { OutputStream os = delegate == null ? super.getOutputStream() : delegate.getOutputStream(); Cipher cipher = null; try {//from ww w . j a v a 2 s . com cipher = Cipher.getInstance(algorithm); int size = cipher.getBlockSize(); byte[] tmp = new byte[size]; Arrays.fill(tmp, (byte) 15); IvParameterSpec iv = new IvParameterSpec(tmp); cipher.init(Cipher.ENCRYPT_MODE, key, iv); } catch (Exception e) { throw new IOException("Failed to init cipher: " + e.getMessage()); } CipherOutputStream cos = new CipherOutputStream(os, cipher); return cos; }
From source file:CipherSocket.java
public InputStream getInputStream() throws IOException { InputStream is = delegate == null ? super.getInputStream() : delegate.getInputStream(); Cipher cipher = null; try {//from ww w.j av a2s .c o m cipher = Cipher.getInstance(algorithm); int size = cipher.getBlockSize(); byte[] tmp = new byte[size]; Arrays.fill(tmp, (byte) 15); IvParameterSpec iv = new IvParameterSpec(tmp); cipher.init(Cipher.DECRYPT_MODE, key, iv); } catch (Exception e) { e.printStackTrace(); throw new IOException("Failed to init cipher: " + e.getMessage()); } CipherInputStream cis = new CipherInputStream(is, cipher); return cis; }
From source file:com.muk.services.commerce.CryptoServiceImpl.java
@PostConstruct public void postConstruct() { try {/* ww w .ja v a 2 s. c o m*/ final KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); temporaryKey = new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"); final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); final byte[] iv = new byte[cipher.getBlockSize()]; new SecureRandom().nextBytes(iv); ivSpec = new IvParameterSpec(iv); } catch (final NoSuchAlgorithmException ex) { LOG.error("Failed to initalize encryption key", ex); } catch (final NoSuchPaddingException padEx) { LOG.error("Failed to get cipher.", padEx); } }