當前位置: 首頁>>代碼示例>>Java>>正文


Java Cipher.getIV方法代碼示例

本文整理匯總了Java中javax.crypto.Cipher.getIV方法的典型用法代碼示例。如果您正苦於以下問題:Java Cipher.getIV方法的具體用法?Java Cipher.getIV怎麽用?Java Cipher.getIV使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.crypto.Cipher的用法示例。


在下文中一共展示了Cipher.getIV方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: encrypt

import javax.crypto.Cipher; //導入方法依賴的package包/類
/**
 * Generates a random IV and encrypts this plain text with the given key. Then attaches
 * a hashed MAC, which is contained in the CipherTextIvMac class.
 *
 * @param plaintext The text that will be encrypted
 * @param secretKeys The combined AES & HMAC keys with which to encrypt
 * @return a tuple of the IV, ciphertext, mac
 * @throws GeneralSecurityException if AES is not implemented on this system
 */
public static CipherTextIvMac encrypt(byte[] plaintext, SecretKeys secretKeys)
        throws GeneralSecurityException {
    byte[] iv = generateIv();
    Cipher aesCipherForEncryption = Cipher.getInstance(CIPHER_TRANSFORMATION);
    aesCipherForEncryption.init(Cipher.ENCRYPT_MODE, secretKeys.getConfidentialityKey(), new IvParameterSpec(iv));

    /*
     * Now we get back the IV that will actually be used. Some Android
     * versions do funny stuff w/ the IV, so this is to work around bugs:
     */
    iv = aesCipherForEncryption.getIV();
    byte[] byteCipherText = aesCipherForEncryption.doFinal(plaintext);
    byte[] ivCipherConcat = CipherTextIvMac.ivCipherConcat(iv, byteCipherText);

    byte[] integrityMac = generateMac(ivCipherConcat, secretKeys.getIntegrityKey());
    return new CipherTextIvMac(byteCipherText, iv, integrityMac);
}
 
開發者ID:YoeriNijs,項目名稱:NoteBuddy,代碼行數:27,代碼來源:AesCbcWithIntegrity.java

示例2: getEncryptedBody

import javax.crypto.Cipher; //導入方法依賴的package包/類
private byte[] getEncryptedBody(Cipher cipher, byte[] body) throws IllegalBlockSizeException, BadPaddingException {
  byte[] encrypted = cipher.doFinal(body);
  byte[] iv        = cipher.getIV();

  byte[] ivAndBody = new byte[iv.length + encrypted.length];
  System.arraycopy(iv, 0, ivAndBody, 0, iv.length);
  System.arraycopy(encrypted, 0, ivAndBody, iv.length, encrypted.length);

  return ivAndBody;
}
 
開發者ID:XecureIT,項目名稱:PeSanKita-android,代碼行數:11,代碼來源:MasterCipher.java

示例3: initializeCipher

import javax.crypto.Cipher; //導入方法依賴的package包/類
private Cipher initializeCipher(Mac mac, SecretKeySpec key) throws IOException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException {
  Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  cipher.init(Cipher.ENCRYPT_MODE, key);

  byte[] ivBytes = cipher.getIV();
  mac.update(ivBytes);
  super.write(ivBytes, 0, ivBytes.length);

  return cipher;
}
 
開發者ID:XecureIT,項目名稱:PeSanKita-android,代碼行數:11,代碼來源:EncryptingPartOutputStream.java

示例4: encrypt

import javax.crypto.Cipher; //導入方法依賴的package包/類
/**
 * Encrypts the message using password based encryption.
 * @param algo
 *        the encryption algorithm
 * @param plaintext
 *        the message to be encrypted
 * @param password
 *        the password
 * @param iterationCount
 *        the iteration count
 * @param salt
 *        the salt
 * @return iv and the cipher text in form of
 *         len(iv) of 1 byte | iv of len(iv) bytes | cipher text.
 * @throws GeneralSecurityException
 *         if error occurs.
 */
public static byte[] encrypt(PBEAlgo algo, byte[] plaintext, char[] password,
        int iterationCount, byte[] salt) throws GeneralSecurityException {
    ParamUtil.requireNonNull("plaintext", plaintext);
    ParamUtil.requireNonNull("password", password);
    ParamUtil.requireMin("iterationCount", iterationCount, 1);
    ParamUtil.requireNonNull("salt", salt);

    SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(algo.algoName());

    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    SecretKey pbeKey = secretKeyFactory.generateSecret(pbeKeySpec);

    Cipher cipher = Cipher.getInstance(algo.algoName());
    PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt, iterationCount);
    cipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParameterSpec);
    pbeKeySpec.clearPassword();

    byte[] iv = cipher.getIV();
    int ivLen = (iv == null) ? 0 : iv.length;
    if (ivLen > 255) {
        throw new GeneralSecurityException("IV too long: " + ivLen);
    }

    byte[] cipherText = cipher.doFinal(plaintext);
    byte[] ret = new byte[1 + ivLen + cipherText.length];
    // length of IV
    ret[0] = (byte) (ivLen & 0xFF);
    if (ivLen > 0) {
        System.arraycopy(iv, 0, ret, 1, ivLen);
    }

    System.arraycopy(cipherText, 0, ret, 1 + ivLen, cipherText.length);
    return ret;
}
 
開發者ID:xipki,項目名稱:xitk,代碼行數:52,代碼來源:PasswordBasedEncryption.java

示例5: encrypt

import javax.crypto.Cipher; //導入方法依賴的package包/類
private byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    byte[] data = cipher.doFinal(clear);
    byte[] iv = cipher.getIV();
    byte[] packet = new byte[data.length+iv.length];
    for (int i = 0; i < iv.length; i++)
        packet[i] = iv[i];
    for (int i = 0; i < data.length; i++)
        packet[iv.length+i] = data[i];
    return data;
}
 
開發者ID:rctl,項目名稱:CryptoVoice,代碼行數:14,代碼來源:Call.java

示例6: decrypt

import javax.crypto.Cipher; //導入方法依賴的package包/類
/**
 * Decrypt a CryptoInfo in-place.
 *
 * @throws CryptoException
 */
public void decrypt() throws CryptoException {

  // Check HMAC.
  try {
    if (!generatedHMACIsHMAC()) {
      throw new HMACVerificationException();
    }
  } catch (NoSuchAlgorithmException | InvalidKeyException e) {
    throw new CryptoException(e);
  }

  Cipher cipher = CryptoInfo.getCipher(TRANSFORMATION);
  try {
    byte[] encryptionKey = getKeys().getEncryptionKey();
    SecretKeySpec spec = new SecretKeySpec(encryptionKey, KEY_ALGORITHM_SPEC);
    cipher.init(Cipher.DECRYPT_MODE, spec, new IvParameterSpec(getIV()));
  } catch (GeneralSecurityException ex) {
    throw new CryptoException(ex);
  }
  byte[] decryptedBytes = commonCrypto(cipher, getMessage());
  byte[] iv = cipher.getIV();

  // Update in place.  keys is already set.
  this.setHMAC(null);
  this.setIV(iv);
  this.setMessage(decryptedBytes);
}
 
開發者ID:mozilla-mobile,項目名稱:FirefoxData-android,代碼行數:33,代碼來源:CryptoInfo.java

示例7: a

import javax.crypto.Cipher; //導入方法依賴的package包/類
public static byte[] a(SecretKeySpec secretKeySpec, byte[] bArr, byte[] bArr2) {
    Cipher instance = Cipher.getInstance("AES/CBC/PKCS7Padding");
    IvParameterSpec ivParameterSpec = new IvParameterSpec(bArr);
    instance.init(1, secretKeySpec);
    byte[] iv = instance.getIV();
    byte[] doFinal = instance.doFinal(bArr2);
    a("cipherText", doFinal);
    return a(iv, doFinal);
}
 
開發者ID:JackChan1999,項目名稱:boohee_v5.6,代碼行數:10,代碼來源:a.java

示例8: testParams

import javax.crypto.Cipher; //導入方法依賴的package包/類
private static byte[] testParams(AlgorithmParameters rc2Params,
    RC2ParameterSpec rc2Spec) throws Exception {

    // test getParameterSpec returns object equal to input
    rc2Params.init(rc2Spec);
    RC2ParameterSpec rc2OtherSpec = (RC2ParameterSpec)
        rc2Params.getParameterSpec(RC2ParameterSpec.class);
    if (!rc2Spec.equals(rc2OtherSpec)) {
        throw new Exception("AlgorithmParameterSpecs should be equal");
    }

    // test RC2ParameterSpec with RC2 Cipher
    Cipher rc2Cipher = Cipher.getInstance("RC2/CBC/PKCS5PADDING", "SunJCE");
    rc2Cipher.init(Cipher.ENCRYPT_MODE,
        new SecretKeySpec("secret".getBytes("ASCII"), "RC2"), rc2Spec);

    // get IV
    byte[] iv = rc2Cipher.getIV();
    if (!Arrays.equals(iv, rc2Spec.getIV())) {
        throw new Exception("ivs should be equal");
    }

    // test encoding and decoding
    byte[] encoded = rc2Params.getEncoded();
    AlgorithmParameters params = AlgorithmParameters.getInstance("RC2");
    params.init(encoded);

    // test RC2 AlgorithmParameters with RC2 Cipher
    rc2Cipher.init(Cipher.ENCRYPT_MODE,
        new SecretKeySpec("secret".getBytes("ASCII"), "RC2"), params);

    // get IV
    iv = rc2Cipher.getIV();
    if (!Arrays.equals(iv, rc2Spec.getIV())) {
        throw new Exception("ivs should be equal");
    }
    return encoded;
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:39,代碼來源:RC2AlgorithmParameters.java

示例9: encrypt

import javax.crypto.Cipher; //導入方法依賴的package包/類
/**
 * Encrypt a CryptoInfo in-place.
 *
 * @throws CryptoException
 */
public void encrypt() throws CryptoException {

  Cipher cipher = CryptoInfo.getCipher(TRANSFORMATION);
  try {
    byte[] encryptionKey = getKeys().getEncryptionKey();
    SecretKeySpec spec = new SecretKeySpec(encryptionKey, KEY_ALGORITHM_SPEC);

    // If no IV is provided, use a shared SecureRandom to generate one.
    if (getIV() == null || getIV().length == 0) {
      cipher.init(
              Cipher.ENCRYPT_MODE,
              spec,
              new IvParameterSpec(
                      Utils.generateRandomBytes(cipher.getBlockSize())
              )
      );
    } else {
      cipher.init(Cipher.ENCRYPT_MODE, spec, new IvParameterSpec(getIV()));
    }
  } catch (GeneralSecurityException ex) {
    throw new CryptoException(ex);
  }

  // Encrypt.
  byte[] encryptedBytes = commonCrypto(cipher, getMessage());
  byte[] iv = cipher.getIV();

  byte[] hmac;
  // Generate HMAC.
  try {
    hmac = generatedHMACFor(encryptedBytes, keys);
  } catch (NoSuchAlgorithmException | InvalidKeyException e) {
    throw new CryptoException(e);
  }

  // Update in place.  keys is already set.
  this.setHMAC(hmac);
  this.setIV(iv);
  this.setMessage(encryptedBytes);
}
 
開發者ID:mozilla-mobile,項目名稱:FirefoxData-android,代碼行數:46,代碼來源:CryptoInfo.java

示例10: encrypt

import javax.crypto.Cipher; //導入方法依賴的package包/類
public static CipherText encrypt(byte[] plainText) throws Exception {
	Cipher cipher = getEncryptionCipher();
	return new CipherText(cipher.getIV(), encrypt(cipher, plainText));
}
 
開發者ID:phoenixctms,項目名稱:ctsms,代碼行數:5,代碼來源:CryptoUtil.java

示例11: encryptValue

import javax.crypto.Cipher; //導入方法依賴的package包/類
public static CipherText encryptValue(byte[] salt, String password, Serializable value) throws Exception {
	Cipher cipher = getEncryptionCipher(salt, password);
	return new CipherText(cipher.getIV(), encrypt(cipher, serialize(value)));
}
 
開發者ID:phoenixctms,項目名稱:ctsms,代碼行數:5,代碼來源:CryptoUtil.java

示例12: CipherStream

import javax.crypto.Cipher; //導入方法依賴的package包/類
public CipherStream(OutputStream os, Cipher c) {
	super(os, c);
	iv = c.getIV();
}
 
開發者ID:phoenixctms,項目名稱:ctsms,代碼行數:5,代碼來源:CipherStream.java


注:本文中的javax.crypto.Cipher.getIV方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。