List of usage examples for javax.crypto Cipher getBlockSize
public final int getBlockSize()
From source file:org.apache.shindig.common.crypto.Crypto.java
/** * AES-128-CBC encryption. The IV is returned as the first 16 bytes * of the cipher text./*from w ww . j a va 2 s. c o m*/ * * @param key * @param plain * * @return the IV and cipher text * * @throws GeneralSecurityException */ public static byte[] aes128cbcEncrypt(byte[] key, byte[] plain) throws GeneralSecurityException { Cipher cipher = Cipher.getInstance(CIPHER_TYPE); byte iv[] = getRandomBytes(cipher.getBlockSize()); return concat(iv, aes128cbcEncryptWithIV(key, iv, plain)); }
From source file:Main.java
public static byte[] getDecCode(byte[] byteD, String seed) { byte[] byteFina = null; Cipher cipher = null; try {/*from ww w . ja v a 2s . co m*/ SecretKeySpec skeySpec = new SecretKeySpec(getRawKey(seed.getBytes()), "AES"); cipher = Cipher.getInstance("AES/CFB/NoPadding"); cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()])); byteFina = cipher.doFinal(byteD); } catch (Exception e) { e.printStackTrace(); } finally { cipher = null; } return byteFina; }
From source file:Main.java
public static byte[] getEncCode(byte[] byteE, String seed) { byte[] byteFina = null; Cipher cipher = null; try {/* w w w .j a v a 2s.com*/ SecretKeySpec skeySpec = new SecretKeySpec(getRawKey(seed.getBytes()), "AES"); cipher = Cipher.getInstance("AES/CFB/NoPadding"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()])); //cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()])); byteFina = cipher.doFinal(byteE); } catch (Exception e) { e.printStackTrace(); } finally { cipher = null; } return byteFina; }
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 . java 2s . c o m*/ 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:uk.ac.ox.webauth.crypto.Des3CbcSha1Kd.java
/** * From RFC 3961:/*from w w w.ja va2 s . com*/ * DR(Key, Constant) = k-truncate(E(Key, Constant, initial-cipher-state)) * * Here DR is the random-octet generation function described below, and * DK is the key-derivation function produced from it. In this * construction, E(Key, Plaintext, CipherState) is a cipher, Constant is * a well-known constant determined by the specific usage of this * function, and k-truncate truncates its argument by taking the first k * bits. Here, k is the key generation seed length needed for the * encryption system. * * The output of the DR function is a string of bits; the actual key is * produced by applying the cryptosystem's random-to-key operation on * this bitstring. * * If the Constant is smaller than the cipher block size of E, then it * must be expanded with n-fold() so it can be encrypted. If the output * of E is shorter than k bits, it is fed back into the encryption as * many times as necessary. The construct is as follows (where | * indicates concatentation): * * K1 = E(Key, n-fold(Constant), initial-cipher-state) * K2 = E(Key, K1, initial-cipher-state) * K3 = E(Key, K2, initial-cipher-state) * K4 = ... * * DR(Key, Constant) = k-truncate(K1 | K2 | K3 | K4 ...) */ private static byte[] dr(byte[] key, byte[] constant) throws GeneralSecurityException { // first make a DES3 key SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede"); KeySpec spec = new DESedeKeySpec(key); SecretKey secretKey = factory.generateSecret(spec); // initialise the cipher to use Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding"); cipher.init(ENCRYPT_MODE, secretKey, IV); // ensure the constant is not smaller than the blocksize if (constant.length < cipher.getBlockSize()) { constant = nFold(constant, cipher.getBlockSize() * 8); } // now encrypt until we have at least 21 bytes, the length of a DES3 key byte[] input = constant; byte[] kn = new byte[0]; do { byte[] newKn = cipher.doFinal(input); byte[] oldKn = kn; kn = new byte[oldKn.length + newKn.length]; System.arraycopy(oldKn, 0, kn, 0, oldKn.length); System.arraycopy(newKn, 0, kn, oldKn.length, newKn.length); input = newKn; } while (kn.length < 21); // make sure we are returning exactly 21 bytes if (kn.length != 21) { byte[] newKn = new byte[21]; System.arraycopy(kn, 0, newKn, 0, 21); kn = newKn; } return kn; }
From source file:com.forsrc.utils.MyRsa2Utils.java
/** * Encrypt string./*from w w w .j a v a 2 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[]/*from w ww.ja va2s.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.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[]/* w ww . jav a2 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.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[]//from ww w .ja va2 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.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:acp.sdk.SecureUtil.java
/** * /*from ww w. j a va 2 s. com*/ * @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; }