/** * Returns the algorithm name of the underlying cipher. */ final String getCipherAlgorithm() { return cipher.getAlgorithm(); }
public AesFlushingCipher(int mode, byte[] secretKey, long nonce, long offset) { try { cipher = Cipher.getInstance("AES/CTR/NoPadding"); blockSize = cipher.getBlockSize(); zerosBlock = new byte[blockSize]; flushedBlock = new byte[blockSize]; long counter = offset / blockSize; int startPadding = (int) (offset % blockSize); cipher.init( mode, new SecretKeySpec(secretKey, Util.splitAtFirst(cipher.getAlgorithm(), "/")[0]), new IvParameterSpec(getInitializationVector(nonce, counter))); if (startPadding != 0) { updateInPlace(new byte[startPadding], 0, startPadding); } } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException e) { // Should never happen. throw new RuntimeException(e); } }
private void saveSecretKey(String ksAlias, ObjectOutputStream oos, KeyStore.SecretKeyEntry entry) throws IOException, GeneralSecurityException { ByteArrayOutputStream entryData = new ByteArrayOutputStream(1024); ObjectOutputStream entryOos = new ObjectOutputStream(entryData); entryOos.writeUTF(ksAlias); writeBytes(entry.getSecretKey().getEncoded(), entryOos); entryOos.flush(); encrypt.init(Cipher.ENCRYPT_MODE, storageSecretKey, (AlgorithmParameterSpec) null); // ELY-1308: third param need to workaround BouncyCastle bug int blockSize = encrypt.getBlockSize(); if (blockSize == 0) throw log.algorithmNotBlockBased(encrypt.getAlgorithm()); Assert.checkMaximumParameter("cipher block size", 256, blockSize); byte[] padded = pkcs7Pad(entryData.toByteArray(), blockSize); byte[] encrypted = encrypt.doFinal(padded); byte[] iv = encrypt.getIV(); if (iv == null) throw log.algorithmNotIV(encrypt.getAlgorithm()); oos.writeInt(SECRET_KEY_ENTRY_TYPE); writeBytes(encrypted, oos); writeBytes(iv, oos); }
this.encodedParams = (ap == null) ? null : ap.getEncoded(); this.paramsAlg = (ap == null) ? null : ap.getAlgorithm(); this.sealAlg = c.getAlgorithm(); this.encryptedContent = c.doFinal(bos.toByteArray()); } catch (BadPaddingException e) {
int slash = ciph.getAlgorithm().indexOf('/'); String alg = (slash > -1 ? ciph.getAlgorithm().substring(0, slash) : ciph.getAlgorithm());
protected static Cipher initCipherForBlock(Cipher existing, int block, boolean lastChunk, EncryptionInfo encryptionInfo, SecretKey skey, int encryptionMode) throws GeneralSecurityException { EncryptionHeader header = encryptionInfo.getHeader(); String padding = (lastChunk ? "PKCS5Padding" : "NoPadding"); if (existing == null || !existing.getAlgorithm().endsWith(padding)) { existing = getCipher(skey, header.getCipherAlgorithm(), header.getChainingMode(), header.getKeySalt(), encryptionMode, padding); } byte[] blockKey = new byte[4]; LittleEndian.putInt(blockKey, 0, block); byte[] iv = generateIv(header.getHashAlgorithm(), header.getKeySalt(), blockKey, header.getBlockSize()); AlgorithmParameterSpec aps; if (header.getCipherAlgorithm() == CipherAlgorithm.rc2) { aps = new RC2ParameterSpec(skey.getEncoded().length*8, iv); } else { aps = new IvParameterSpec(iv); } existing.init(encryptionMode, skey, aps); return existing; }
/** * Returns the algorithm name of the underlying cipher. */ final String getCipherAlgorithm() { return cipher.getAlgorithm(); }
/** * Returns the algorithm name of the underlying cipher. */ final String getCipherAlgorithm() { return cipher.getAlgorithm(); }
/** * Returns the algorithm name of the underlying cipher. */ final String getCipherAlgorithm() { return cipher.getAlgorithm(); }
/** * Returns the algorithm name of the underlying cipher. */ final String getCipherAlgorithm() { return cipher.getAlgorithm(); }
/** * Returns the algorithm name of the underlying cipher. */ final String getCipherAlgorithm() { return cipher.getAlgorithm(); }
/** * Returns the algorithm name of the underlying cipher. */ final String getCipherAlgorithm() { return cipher.getAlgorithm(); }
public static String getAsymmetricEncryptionDesc() { try { return GpsTrailerCrypt.RSA_KEY_SIZE+" bit "+Cipher.getInstance(GpsTrailerCrypt.INTERNAL_ASYMMETRIC_ENCRYPTION_ALGORITHM).getAlgorithm(); } catch (Exception e) { throw new IllegalStateException(e); } }
public static String getEncryptDesc() { try { return GpsTrailerCrypt.prefs.aesKeySize+" bit "+Cipher.getInstance(GpsTrailerCrypt.INTERNAL_SYMMETRIC_ENCRYPTION_ALGORITHM). getAlgorithm(); } catch(Exception e) { throw new IllegalStateException(e); } }
@Override public void init(CryptMode cryptMode, byte[] bytes) throws SecurityException { try { if (CryptMode.DECRYPT == cryptMode) { cipher.init(javax.crypto.Cipher.DECRYPT_MODE, new SecretKeySpec(bytes, cipher.getAlgorithm().split("/")[0])); } else { cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, new SecretKeySpec(bytes, cipher.getAlgorithm().split("/")[0])); } } catch (InvalidKeyException e) { throw new SecurityException(e); } }
@Override public void init(CryptMode cryptMode, byte[] bytes) throws SecurityException { try { if (CryptMode.DECRYPT == cryptMode) { cipher.init(javax.crypto.Cipher.DECRYPT_MODE, new SecretKeySpec(bytes, cipher.getAlgorithm().split("/")[0])); } else { cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, new SecretKeySpec(bytes, cipher.getAlgorithm().split("/")[0])); } } catch (InvalidKeyException e) { throw new SecurityException(e); } }
private void initializeCipher() throws IOException { AlgorithmParameterSpec iv = XMLCipherUtil.constructBlockCipherParameters(cipher.getAlgorithm().toUpperCase().contains("GCM"), this.getIv(), this.getClass()); try { cipher.init(Cipher.DECRYPT_MODE, secretKey, iv); } catch (InvalidKeyException e) { throw new IOException(e); } catch (InvalidAlgorithmParameterException e) { throw new IOException(e); } }
protected Cipher getCipher(int mode) throws Exception { if (secretKey == null) { secretKey = getSecretKey(); } Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm()); initializeCipher(cipher, mode); log.debug("Using {} algorithm provided by {}.", cipher.getAlgorithm(), cipher.getProvider() .getName()); return cipher; }
/** CTOR that sets everything except for the cipher key size and possibly * the IV. (IV may not be applicable--e.g., with ECB--or may not have * been specified yet. */ public CipherSpec(final Cipher cipher) { setCipherTransformation(cipher.getAlgorithm(), true); setBlockSize(cipher.getBlockSize()); if ( cipher.getIV() != null ) { setIV(cipher.getIV()); } }
/** CTOR that sets everything except for the cipher key size and possibly * the IV. (IV may not be applicable--e.g., with ECB--or may not have * been specified yet. */ public CipherSpec(final Cipher cipher) { setCipherTransformation(cipher.getAlgorithm(), true); setBlockSize(cipher.getBlockSize()); if ( cipher.getIV() != null ) { setIV(cipher.getIV()); } }