当前位置: 首页>>代码示例>>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;未经允许,请勿转载。