List of usage examples for javax.crypto Cipher getBlockSize
public final int getBlockSize()
From source file:keywhiz.service.crypto.ContentCryptographer.java
private byte[] gcm(Mode mode, String info, byte[] nonce, byte[] data) { try {/*from w w w. ja v a 2 s . co m*/ Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM, encryptionProvider); SecretKey derivedKey = deriveKey(cipher.getBlockSize(), info); GCMParameterSpec gcmParameters = new GCMParameterSpec(TAG_BITS, nonce); cipher.init(mode.cipherMode, derivedKey, gcmParameters); return cipher.doFinal(data); } catch (IllegalBlockSizeException | InvalidAlgorithmParameterException | NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException | BadPaddingException e) { throw Throwables.propagate(e); } }
From source file:com.vmware.o11n.plugin.crypto.service.CryptoEncryptionService.java
/** * AES Encryption CBC Mode with PKCS5 Padding * * @param dataB64 Data to encrypt Base64 encoded * @param secretB64 Encryption secret Base64 encoded. For AES128 this should be 128 bits (16 bytes) long. For AES256 this should be 256 bits (32 bytes) long. * @param ivB64 Initialization Vector Base64 encoded. 16 bytes long * @return Encrypted data Base64 Encoded * @throws NoSuchAlgorithmException/* w w w . j av a2 s . c om*/ * @throws NoSuchPaddingException * @throws InvalidKeyException * @throws InvalidAlgorithmParameterException * @throws IOException * @throws BadPaddingException * @throws IllegalBlockSizeException */ public String aesEncrypt(String dataB64, String secretB64, String ivB64) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { String encryptedB64 = null; final byte[] dataBytes = Base64.decodeBase64(dataB64); final byte[] secretBytes = Base64.decodeBase64(secretB64); final byte[] ivBytes = Base64.decodeBase64(ivB64); final Cipher cipher = Cipher.getInstance(AES_CIPHER); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(secretBytes, "AES"), new IvParameterSpec(ivBytes, 0, cipher.getBlockSize())); encryptedB64 = Base64.encodeBase64String(cipher.doFinal(dataBytes)); return encryptedB64; }
From source file:com.vmware.o11n.plugin.crypto.service.CryptoEncryptionService.java
/** * AES Decryption CBC Mode with PKCS5 Padding * * @param encryptedB64 Encrypted Data Base64 encoded. * @param secretB64 Encryption secret Base64 encoded. For AES128 this should be 128 bits (16 bytes) long. For AES256 this should be 256 bits (32 bytes) long. * @param ivB64 Initialization Vector Base64 encoded. 16 bytes long * @return Original data Base64 encoded. * @throws NoSuchAlgorithmException/*from www .ja v a 2 s .co m*/ * @throws NoSuchPaddingException * @throws InvalidKeyException * @throws InvalidAlgorithmParameterException * @throws IOException * @throws BadPaddingException * @throws IllegalBlockSizeException */ public String aesDecrypt(String encryptedB64, String secretB64, String ivB64) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { String dataB64 = null; final byte[] encryptedBytes = Base64.decodeBase64(encryptedB64); final byte[] secretBytes = Base64.decodeBase64(secretB64); final byte[] ivBytes = Base64.decodeBase64(ivB64); final Cipher cipher = Cipher.getInstance(AES_CIPHER); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(secretBytes, "AES"), new IvParameterSpec(ivBytes, 0, cipher.getBlockSize())); dataB64 = Base64.encodeBase64String(cipher.doFinal(encryptedBytes)); return dataB64; }
From source file:org.openhab.binding.km200.internal.KM200Cryption.java
/** * This function does the encoding for a new message to the device * *//* w ww . ja v a2s . co m*/ public byte[] encodeMessage(String data) { try { // --- create cipher byte[] bdata = data.getBytes(remoteDevice.getCharSet()); final Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding"); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(remoteDevice.getCryptKeyPriv(), "AES")); int bsize = cipher.getBlockSize(); logger.debug("Add Padding, encrypt AES and B64.."); byte[] encryptedData = cipher.doFinal(addZeroPadding(bdata, bsize, remoteDevice.getCharSet())); try { return (Base64.getMimeEncoder().encode(encryptedData)); } catch (IllegalArgumentException e) { logger.info("Base64encoding not possible: {}", e.getMessage()); } } catch (UnsupportedEncodingException | GeneralSecurityException e) { logger.error("Exception on encoding: {}", e); } return null; }
From source file:adminpassword.Decryption.java
@SuppressWarnings("static-access") public String decrypt(String encryptedText, String idKey) throws Exception { String password = idKey;//from www . ja va2s . c o m Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); //strip off the salt and iv ByteBuffer buffer = ByteBuffer.wrap(new Base64().decode(encryptedText)); byte[] saltBytes = new byte[20]; buffer.get(saltBytes, 0, saltBytes.length); byte[] ivBytes1 = new byte[cipher.getBlockSize()]; buffer.get(ivBytes1, 0, ivBytes1.length); byte[] encryptedTextBytes = new byte[buffer.capacity() - saltBytes.length - ivBytes1.length]; buffer.get(encryptedTextBytes); // Deriving the key SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, 65556, 256); SecretKey secretKey = factory.generateSecret(spec); SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES"); cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytes1)); byte[] decryptedTextBytes = null; try { decryptedTextBytes = cipher.doFinal(encryptedTextBytes); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return new String(decryptedTextBytes); }
From source file:uk.ac.ox.webauth.crypto.DesCbcCrc.java
@Override public byte[] encrypt(ASN1Encodable o) throws IOException, GeneralSecurityException { // setup the Cipher so we can get the block size Cipher cipher = Cipher.getInstance("DES/CBC/NoPadding"); IvParameterSpec iv = new IvParameterSpec(key.getEncoded()); cipher.init(ENCRYPT_MODE, key, iv);/*w w w .ja v a 2 s.c o m*/ int blockSize = cipher.getBlockSize(); // set up the byte array with data ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] confounder = new byte[blockSize]; synchronized (rand) { rand.nextBytes(confounder); } baos.write(confounder); // write empty data in place of the checksum byte[] checksum = new byte[4]; baos.write(checksum); // write the message sequence baos.write(o.getDEREncoded()); // PKCS7 padding byte[] pad = new byte[blockSize - (baos.size() % blockSize)]; Arrays.fill(pad, (byte) pad.length); baos.write(pad); byte[] data = baos.toByteArray(); // calculate the checksum checksum = modifiedCRC32(data); System.arraycopy(checksum, 0, data, cipher.getBlockSize(), checksum.length); // now encrypt and return the data return cipher.doFinal(data); }
From source file:cn.ctyun.amazonaws.services.s3.internal.crypto.EncryptionUtils.java
/** * Calculates the length of the encrypted file given the original plaintext * file length and the cipher that will be used for encryption. * * @return//from w w w. j a v a2 s. c om * The size of the encrypted file in bytes, or -1 if no content length * has been set yet. */ private static long calculateCryptoContentLength(Cipher symmetricCipher, PutObjectRequest request, ObjectMetadata metadata) { long plaintextLength = getUnencryptedContentLength(request, metadata); // If we have a zero length object, return zero as the encrypted size if (plaintextLength == 0) return 0; // If we don't know the unencrypted size, then report -1 if (plaintextLength < 0) return -1; long cipherBlockSize = symmetricCipher.getBlockSize(); long offset = cipherBlockSize - (plaintextLength % cipherBlockSize); return plaintextLength + offset; }
From source file:cn.ctyun.amazonaws.services.s3.internal.crypto.EncryptionUtils.java
public static long calculateCryptoContentLength(Cipher symmetricCipher, UploadPartRequest request) { long plaintextLength; if (request.getFile() != null) { if (request.getPartSize() > 0) plaintextLength = request.getPartSize(); else/*from ww w .j a va2 s. c o m*/ plaintextLength = request.getFile().length(); } else if (request.getInputStream() != null) { plaintextLength = request.getPartSize(); } else { return -1; } long cipherBlockSize = symmetricCipher.getBlockSize(); long offset = cipherBlockSize - (plaintextLength % cipherBlockSize); return plaintextLength + offset; }
From source file:com.vmware.o11n.plugin.crypto.service.CryptoEncryptionService.java
/** * TripleDES (EDE) Encryption CBC Mode with PKCS5 padding * * @param dataB64 Data to encrypt Base64 encoded. * @param secretB64 Encryption secret Base64 encoded. Secret must be at least 24 bytes. Only the first 24 bytes will be used. * @param ivB64 Initialization Vector Base64 encoded. Only first 8 bytes will be used. * @return Encrypted data Base64 encoded. * @throws NoSuchAlgorithmException//from www . ja va2 s . com * @throws NoSuchPaddingException * @throws InvalidKeyException * @throws InvalidAlgorithmParameterException * @throws IOException * @throws BadPaddingException * @throws IllegalBlockSizeException */ public String tripleDesEncrypt(String dataB64, String secretB64, String ivB64) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { String encryptedB64 = null; final byte[] dataBytes = Base64.decodeBase64(dataB64); final byte[] secretBytes = Base64.decodeBase64(secretB64); final byte[] ivBytes = Base64.decodeBase64(ivB64); final Cipher cipher = Cipher.getInstance(DESEDE_CIPHER); DESedeKeySpec keySpec = new DESedeKeySpec(secretBytes); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keySpec.getKey(), "DESede"), new IvParameterSpec(ivBytes, 0, cipher.getBlockSize())); encryptedB64 = Base64.encodeBase64String(cipher.doFinal(dataBytes)); return encryptedB64; }
From source file:com.vmware.o11n.plugin.crypto.service.CryptoEncryptionService.java
/** * TripleDES (EDE) Decryption CBC Mode with PKCS5 padding * * @param encryptedB64 Encrypted data Base64 encoded * @param secretB64 Encryption secret Base64 encoded. Secret must be at least 24 bytes. Only the first 24 bytes will be used. * @param ivB64 Initialization Vector Base64 encoded. Only first 8 bytes will be used. * @return Original data Base64 encoded. * @throws NoSuchAlgorithmException/* w ww . j a v a 2 s .c om*/ * @throws NoSuchPaddingException * @throws InvalidKeyException * @throws InvalidAlgorithmParameterException * @throws IOException * @throws BadPaddingException * @throws IllegalBlockSizeException */ public String tripleDesDecrypt(String encryptedB64, String secretB64, String ivB64) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { String dataB64 = null; final byte[] encryptedBytes = Base64.decodeBase64(encryptedB64); final byte[] secretBytes = Base64.decodeBase64(secretB64); final byte[] ivBytes = Base64.decodeBase64(ivB64); final Cipher cipher = Cipher.getInstance(DESEDE_CIPHER); DESedeKeySpec keySpec = new DESedeKeySpec(secretBytes); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keySpec.getKey(), "DESede"), new IvParameterSpec(ivBytes, 0, cipher.getBlockSize())); dataB64 = Base64.encodeBase64String(cipher.doFinal(encryptedBytes)); return dataB64; }