当前位置: 首页>>代码示例>>Java>>正文


Java IvParameterSpec.getIV方法代码示例

本文整理汇总了Java中javax.crypto.spec.IvParameterSpec.getIV方法的典型用法代码示例。如果您正苦于以下问题:Java IvParameterSpec.getIV方法的具体用法?Java IvParameterSpec.getIV怎么用?Java IvParameterSpec.getIV使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.crypto.spec.IvParameterSpec的用法示例。


在下文中一共展示了IvParameterSpec.getIV方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: Token

import javax.crypto.spec.IvParameterSpec; //导入方法依赖的package包/类
protected Token(final byte version, final Instant timestamp, final IvParameterSpec initializationVector,
        final byte[] cipherText, final byte[] hmac) {
    if (version != supportedVersion) {
        throw new IllegalTokenException("Unsupported version: " + version);
    }
    if (timestamp == null) {
        throw new IllegalTokenException("timestamp cannot be null");
    }
    if (initializationVector == null || initializationVector.getIV().length != initializationVectorBytes) {
        throw new IllegalTokenException("Initialization Vector must be 128 bits");
    }
    if (cipherText == null || cipherText.length % cipherTextBlockSize != 0) {
        throw new IllegalTokenException("Ciphertext must be a multiple of 128 bits");
    }
    if (hmac == null || hmac.length != signatureBytes) {
        throw new IllegalTokenException("hmac must be 256 bits");
    }
    this.version = version;
    this.timestamp = timestamp;
    this.initializationVector = initializationVector;
    this.cipherText = cipherText;
    this.hmac = hmac;
}
 
开发者ID:l0s,项目名称:fernet-java8,代码行数:24,代码来源:Token.java

示例2: init

import javax.crypto.spec.IvParameterSpec; //导入方法依赖的package包/类
private void init(int opmode, Key key, IvParameterSpec params) {
	if ( mIsInited ) {
		// Do not allow multiple inits
		assert(true);
		throw new RuntimeException("Don't allow multiple inits");
	} else {
		NativeLib.init();
		mIsInited = true;
	}
	
	mIV = params.getIV();
	mEncrypting = opmode == Cipher.ENCRYPT_MODE;
	mCtxPtr = nInit(mEncrypting, key.getEncoded(), mIV);
	addToCleanupQueue(this, mCtxPtr);
}
 
开发者ID:PhilippC,项目名称:keepass2android,代码行数:16,代码来源:NativeAESCipherSpi.java

示例3: encrypt

import javax.crypto.spec.IvParameterSpec; //导入方法依赖的package包/类
public String encrypt(String rawPassword, String salt) throws CodecOperationException {
	try {
		SecretKeySpec keySpec = createSecretKey(WAGSTAFF_PRIME, salt.getBytes());
		Cipher cipher = fetchCipher();
		cipher.init(Cipher.ENCRYPT_MODE, keySpec);
		AlgorithmParameters parameters = cipher.getParameters();
		IvParameterSpec ivParameterSpec = parameters.getParameterSpec(IvParameterSpec.class);
		byte[] cryptoText = cipher.doFinal(rawPassword.getBytes("UTF-8"));
		byte[] iv = ivParameterSpec.getIV();
		return base64Encode(iv) + SEPARATOR + base64Encode(cryptoText);
	} catch (GeneralSecurityException | UnsupportedEncodingException e) {
		throw new CodecOperationException(e);
	}
}
 
开发者ID:kana0011,项目名称:symmetrical-memory,代码行数:15,代码来源:PasswordCodec.java

示例4: wolfCryptSetIV

import javax.crypto.spec.IvParameterSpec; //导入方法依赖的package包/类
private void wolfCryptSetIV(AlgorithmParameterSpec spec,
        SecureRandom random) throws InvalidAlgorithmParameterException {

    /* store AlgorithmParameterSpec for class reset */
    this.storedSpec = spec;

    /* RSA doesn't need an IV */
    if (this.cipherType == CipherType.WC_RSA)
        return;

    /* store IV, or generate random IV if not available */
    if (spec == null) {
        this.iv = new byte[this.blockSize];

        if (random != null) {
            random.nextBytes(this.iv);
        } else {
            SecureRandom rand = new SecureRandom();
            rand.nextBytes(this.iv);
        }

    } else {
        if (!(spec instanceof IvParameterSpec)) {
            throw new InvalidAlgorithmParameterException(
                "AlgorithmParameterSpec must be of type IvParameterSpec");
        }

        IvParameterSpec ivSpec = (IvParameterSpec)spec;

        /* IV should be of block size length */
        if (ivSpec.getIV().length != this.blockSize) {
            throw new InvalidAlgorithmParameterException(
                    "Bad IV length (" + ivSpec.getIV().length +
                    "), must be " + blockSize + " bytes long");
        }

        this.iv = ivSpec.getIV();
    }
}
 
开发者ID:wolfSSL,项目名称:wolfcrypt-jni,代码行数:40,代码来源:WolfCryptCipher.java

示例5: encryptPrivateKey

import javax.crypto.spec.IvParameterSpec; //导入方法依赖的package包/类
/**
 * Applies the algorithm AES-CBC-128 according to NIST Special Publication 800-38A.
 * The initialization vector IV shall be randomly generated before encryption and shall have a 
 * length of 128 bit and never be reused.
 * The IV shall be transmitted in the 16 most significant bytes of the 
 * ContractSignatureEncryptedPrivateKey field.
 * 
 * @param sessionKey The symmetric session key with which the private key will be encrypted
 * @param contractCertPrivateKey The private key which is to be encrypted
 * @return The encrypted private key of the contract certificate given as a byte array
 */
private static byte[] encryptPrivateKey(SecretKey sessionKey, ECPrivateKey contractCertPrivateKey) {
	try {
		/*
		 * Padding of the plain text (private key) is not required as its length (256 bit) is a 
		 * multiple of the block size (128 bit) of the used encryption algorithm (AES)
		 */
		Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
		IvParameterSpec ivParamSpec = new IvParameterSpec(generateRandomNumber(16));
		cipher.init(Cipher.ENCRYPT_MODE, sessionKey, ivParamSpec);
		
		/*
		 * Not the complete ECPrivateKey container, but the private value s represents the 256 bit 
		 * private key which must be encoded. 
		 * The private key is stored as an ASN.1 integer which may need to have zero padding 
		 * in the most significant bits removed (if 33 bytes)
		 */
		byte[] encryptedKey;
		if (contractCertPrivateKey.getS().toByteArray().length == 33) {
			byte[] temp = new byte[32];
			System.arraycopy(contractCertPrivateKey.getS().toByteArray(), 1, temp, 0, contractCertPrivateKey.getS().toByteArray().length-1);
			encryptedKey = cipher.doFinal(temp);
		} else {
			encryptedKey = cipher.doFinal(contractCertPrivateKey.getS().toByteArray());
		}
		
		/*
		 * The IV must be transmitted in the 16 most significant bytes of the
		 * ContractSignatureEncryptedPrivateKey
		 */
		byte[] encryptedKeyWithIV = new byte[ivParamSpec.getIV().length + encryptedKey.length];
		System.arraycopy(ivParamSpec.getIV(), 0, encryptedKeyWithIV, 0, ivParamSpec.getIV().length);
		System.arraycopy(encryptedKey, 0, encryptedKeyWithIV, ivParamSpec.getIV().length, encryptedKey.length);
		getLogger().debug("Encrypted private key: " + ByteUtils.toHexString(encryptedKeyWithIV));
		
		return encryptedKeyWithIV;
	} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | 
			 InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) {
		getLogger().error(e.getClass().getSimpleName() + " occurred while trying to encrypt private key." +
						  "\nSession key (" + sessionKey.getEncoded().length + " bytes): " +
						  ByteUtils.toHexString(sessionKey.getEncoded()) +
						  "\nContract certificate private key (" + contractCertPrivateKey.getS().toByteArray().length + " bytes): " +
						  ByteUtils.toHexString(contractCertPrivateKey.getS().toByteArray()), e);
	} 
	
	return null;
}
 
开发者ID:V2GClarity,项目名称:RISE-V2G,代码行数:58,代码来源:SecurityUtils.java


注:本文中的javax.crypto.spec.IvParameterSpec.getIV方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。