List of usage examples for javax.crypto Cipher getOutputSize
public final int getOutputSize(int inputLen)
From source file:org.apache.niolex.commons.codec.CipherUtil.java
/** * For some kind of cipher, e.g. RSA, can not handle bytes larger than a fixed block size. * So, this method is just for this kind of cipher to handle large bytes. * * @param cipher//from ww w . j a va 2 s . c o m * @param blockSize * @param input * @return the processed bytes * @throws IllegalBlockSizeException * @throws BadPaddingException * @throws ShortBufferException */ public static byte[] process(Cipher cipher, int blockSize, byte[] input) throws IllegalBlockSizeException, BadPaddingException, ShortBufferException { if (input.length <= blockSize) { return cipher.doFinal(input); } final int OUTPUT_SIZE = (input.length + blockSize - 1) / blockSize * cipher.getOutputSize(blockSize); byte[] output = new byte[OUTPUT_SIZE]; int outputIndex = 0; for (int i = 0;; i += blockSize) { if (i + blockSize < input.length) outputIndex += cipher.doFinal(input, i, blockSize, output, outputIndex); else { outputIndex += cipher.doFinal(input, i, input.length - i, output, outputIndex); break; } } if (outputIndex != OUTPUT_SIZE) return ArrayUtils.subarray(output, 0, outputIndex); return output; }
From source file:net.firejack.platform.web.security.x509.KeyUtils.java
public static byte[] decrypt(PrivateKey key, byte[] bytes) throws Exception { byte[] result = new byte[0]; Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, key); int length = cipher.getOutputSize(bytes.length); for (int i = 0; i < bytes.length; i += length) { byte[] bytes1 = cipher.doFinal(bytes, i, Math.min(bytes.length - i, length)); result = ArrayUtils.addAll(result, bytes1); }/*from w w w . j av a 2s . c o m*/ return result; }
From source file:net.firejack.platform.web.security.x509.KeyUtils.java
public static byte[] encrypt(PublicKey key, byte[] bytes) throws Exception { byte[] result = new byte[0]; Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, key); int length = cipher.getOutputSize(bytes.length) - 11; for (int i = 0; i < bytes.length; i += length) { byte[] buffer = cipher.doFinal(bytes, i, Math.min(bytes.length - i, length)); result = ArrayUtils.addAll(result, buffer); }//from w ww .j ava2 s .co m return result; }
From source file:Main.java
public static byte[] encryptMsg(String msg, RSAPublicKeySpec pubKeySpec) { if (msg != null && pubKeySpec != null && !msg.isEmpty()) { try {/*from w w w. ja va2 s. com*/ Log.w(TAG, "msg is: " + msg + " with length " + msg.length()); KeyFactory fact = KeyFactory.getInstance("RSA"); PublicKey pubKey = fact.generatePublic(pubKeySpec); // TODO encrypt the message and send it // Cipher cipher = Cipher.getInstance("RSA/None/NoPadding"); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); // Cipher cipher = // Cipher.getInstance("RSA/None/OAEPWithSHA1AndMGF1Padding", // "BC"); cipher.init(Cipher.ENCRYPT_MODE, pubKey); Log.d(TAG, "cipher block size is " + cipher.getBlockSize()); byte[] msgByteArray = msg.getBytes(); byte[] cipherData = new byte[cipher.getOutputSize(msgByteArray.length)]; cipherData = cipher.doFinal(msgByteArray); Log.d(TAG, "output size is " + cipher.getOutputSize(msgByteArray.length)); Log.d(TAG, "is the measurement already broken into chunks here? " + (new String(cipherData))); return cipherData; } catch (NoSuchAlgorithmException e) { Log.e(TAG, "RSA algorithm not available", e); } catch (InvalidKeySpecException e) { Log.e(TAG, "", e); } catch (NoSuchPaddingException e) { Log.e(TAG, "", e); } catch (InvalidKeyException e) { Log.e(TAG, "", e); } catch (BadPaddingException e) { Log.e(TAG, "", e); } catch (IllegalBlockSizeException e) { Log.e(TAG, "", e); } catch (Exception e) { Log.e(TAG, "", e); } /* * catch (NoSuchProviderException e) { Log.e(TAG, "", e); } */ } return null; }
From source file:hjow.hgtable.util.SecurityUtil.java
/** * <p>? ? ?? ? .</p>// www. jav a 2 s . c om * * @param text : ? ? * @param key : ? ? * @param algorithm : ? ? (null AES ) * @return ?? ? */ public static String decrypt(String text, String key, String algorithm) { try { String passwords = hash(key, null); String methods = algorithm; if (methods == null) methods = "AES"; String paddings; int need_keySize = -1; boolean useIv = false; byte[] befores = text.getBytes("UTF-8"); byte[] keyByte = passwords.getBytes("UTF-8"); if (methods.equalsIgnoreCase("DES")) { paddings = "DES/CBC/PKCS5Padding"; need_keySize = 8; useIv = true; } else if (methods.equalsIgnoreCase("DESede")) { paddings = "TripleDES/ECB/PKCS5Padding"; need_keySize = 168; useIv = true; } else if (methods.equalsIgnoreCase("AES")) { paddings = "AES"; need_keySize = 16; useIv = false; } else return null; befores = Base64.decodeBase64(befores); byte[] checkKeyByte = new byte[need_keySize]; byte[] ivBytes = new byte[checkKeyByte.length]; for (int i = 0; i < checkKeyByte.length; i++) { if (i < keyByte.length) { checkKeyByte[i] = keyByte[i]; } else { checkKeyByte[i] = 0; } } keyByte = checkKeyByte; SecretKeySpec keySpec = new SecretKeySpec(keyByte, methods); IvParameterSpec ivSpec = null; if (useIv) ivSpec = new IvParameterSpec(ivBytes); Cipher cipher = null; try { cipher = Cipher.getInstance(paddings); if (useIv) { cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); } else { cipher.init(Cipher.DECRYPT_MODE, keySpec); } byte[] outputs = new byte[cipher.getOutputSize(befores.length)]; for (int i = 0; i < outputs.length; i++) { outputs[i] = 0; } int enc_len = cipher.update(befores, 0, befores.length, outputs, 0); enc_len = enc_len + cipher.doFinal(outputs, enc_len); return new String(outputs, "UTF-8").trim(); } catch (Throwable e) { Main.logError(e, Manager.applyStringTable("On decryption")); return text; } } catch (Throwable e) { } return null; }
From source file:hjow.hgtable.util.SecurityUtil.java
/** * <p>? .</p>// w w w. ja va 2s .c om * * @param text : ?? ? ? * @param key : ? ? * @param algorithm : (null AES ) * @return ? ? */ public static String encrypt(String text, String key, String algorithm) { try { String passwords = hash(key, null); String methods = algorithm; if (methods == null) methods = "AES"; String paddings; int need_keySize = -1; boolean useIv = false; byte[] befores = text.trim().getBytes("UTF-8"); byte[] keyByte = passwords.getBytes("UTF-8"); if (methods.equalsIgnoreCase("DES")) { paddings = "DES/CBC/PKCS5Padding"; need_keySize = 8; useIv = true; } else if (methods.equalsIgnoreCase("DESede")) { paddings = "TripleDES/ECB/PKCS5Padding"; need_keySize = 24; useIv = true; } else if (methods.equalsIgnoreCase("AES")) { paddings = "AES"; need_keySize = 16; useIv = false; } else return null; byte[] checkKeyByte = new byte[need_keySize]; byte[] ivBytes = new byte[checkKeyByte.length]; for (int i = 0; i < checkKeyByte.length; i++) { if (i < keyByte.length) { checkKeyByte[i] = keyByte[i]; } else { checkKeyByte[i] = 0; } } keyByte = checkKeyByte; SecretKeySpec keySpec = new SecretKeySpec(keyByte, algorithm); IvParameterSpec ivSpec = null; if (useIv) ivSpec = new IvParameterSpec(ivBytes); Cipher cipher = null; byte[] outputs; try { cipher = Cipher.getInstance(paddings); if (useIv) { cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); } else { cipher.init(Cipher.ENCRYPT_MODE, keySpec); } outputs = new byte[cipher.getOutputSize(befores.length)]; for (int i = 0; i < outputs.length; i++) { outputs[i] = 0; } int enc_len = cipher.update(befores, 0, befores.length, outputs, 0); enc_len = enc_len + cipher.doFinal(outputs, enc_len); return new String(Base64.encodeBase64(outputs), "UTF-8"); } catch (Throwable e) { Main.logError(e, Manager.applyStringTable("On encrypting")); return null; } } catch (Throwable e) { } return null; }
From source file:com.forsrc.utils.MyRsa2Utils.java
/** * Encrypt string.// w ww . ja va2 s. c o m * * @param publicKey the public key * @param plaintext the plaintext * @return the string * @throws RsaException the rsa exception */ public static String encrypt(PublicKey publicKey, String plaintext) 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.ENCRYPT_MODE, publicKey); } catch (InvalidKeyException e) { throw new RsaException(e); } byte[] data = plaintext.getBytes(); int blockSize = cipher.getBlockSize(); blockSize = blockSize == 0 ? 117 : blockSize; int outputSize = cipher.getOutputSize(data.length); int count = (int) Math.ceil(data.length / blockSize) + 1; byte[] output = new byte[outputSize * count]; try { int i = 0; int start = 0; int outputStart = 0; do { start = i * blockSize; outputStart = i * outputSize; if (data.length - start >= blockSize) { cipher.doFinal(data, start, blockSize, output, outputStart); } else { cipher.doFinal(data, start, data.length - start, output, outputStart); } i++; } while (data.length - start - blockSize >= 0); } catch (IllegalBlockSizeException e) { throw new RsaException(e); } catch (BadPaddingException e) { throw new RsaException(e); } catch (ShortBufferException e) { throw new RsaException(e); } return new String(new Base64().encode(output)); }
From source file:cn.usually.common.pay.union.sdk.SecureUtil.java
/** * ???byte[]/*ww w .j a va 2s .com*/ * * @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.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; } catch (Exception e) { throw new Exception(e.getMessage()); } }
From source file:unionpayUtil.sdk.SecureUtil.java
/** * ???byte[]//from ww w .ja v a2 s . 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.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; } catch (Exception e) { throw new Exception(e.getMessage()); } }
From source file:cn.usually.common.pay.union.sdknew.SecureUtil.java
/** * ???byte[]//w ww.ja v a 2s .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.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()); } }