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


Java IvParameterSpec类代码示例

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


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

示例1: aesEncrypt

import javax.crypto.spec.IvParameterSpec; //导入依赖的package包/类
private static String aesEncrypt(String content, String aesKey, String charset)
                                                                               throws AlipayApiException {

    try {
        Cipher cipher = Cipher.getInstance(AES_CBC_PCK_ALG);

        IvParameterSpec iv = new IvParameterSpec(AES_IV);
        cipher.init(Cipher.ENCRYPT_MODE,
            new SecretKeySpec(Base64.decodeBase64(aesKey.getBytes()), AES_ALG), iv);

        byte[] encryptBytes = cipher.doFinal(content.getBytes(charset));
        return new String(Base64.encodeBase64(encryptBytes));
    } catch (Exception e) {
        throw new AlipayApiException("AES加密失败:Aescontent = " + content + "; charset = "
                                     + charset, e);
    }

}
 
开发者ID:1991wangliang,项目名称:pay,代码行数:19,代码来源:AlipayEncrypt.java

示例2: decipherText

import javax.crypto.spec.IvParameterSpec; //导入依赖的package包/类
/**
 * Decipher a text.
 * 
 * @param textToDecipher
 * @param key
 * @throws GeneralSecurityException
 * @throws IOException
 */
private byte[] decipherText(byte[] textToDecipher, SecretKeySpec key) throws GeneralSecurityException, IOException {

	try {
		Cipher dcipher = Cipher.getInstance(AES_MODE);

		// Read random initialization vector.
		final byte[] iv = Arrays.copyOfRange(textToDecipher, 0, BLOCK_SIZE);
		final IvParameterSpec ivSpec = new IvParameterSpec(iv);

		// Configure the cipher with the key and the iv.
		dcipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
		final byte[] textToDecipherWithoutIv = Arrays.copyOfRange(textToDecipher, BLOCK_SIZE,
				textToDecipher.length);

		final byte[] outputBytes = dcipher.doFinal(textToDecipherWithoutIv);
		return outputBytes;

	} catch (final GeneralSecurityException exc) {
		throw exc;
	}
}
 
开发者ID:Mikiya83,项目名称:hbs_decipher,代码行数:30,代码来源:QNAPFileDecrypterEngine.java

示例3: Encryption

import javax.crypto.spec.IvParameterSpec; //导入依赖的package包/类
public Encryption()
{
	KeyPair rsaKeyPair =
		getRsaKeyPair(WurstFolders.RSA.resolve("wurst_rsa.pub"),
			WurstFolders.RSA.resolve("wurst_rsa"));
	
	SecretKey aesKey =
		getAesKey(WurstFolders.MAIN.resolve("key"), rsaKeyPair);
	
	try
	{
		encryptCipher = Cipher.getInstance("AES/CFB8/NoPadding");
		encryptCipher.init(Cipher.ENCRYPT_MODE, aesKey,
			new IvParameterSpec(aesKey.getEncoded()));
		
		decryptCipher = Cipher.getInstance("AES/CFB8/NoPadding");
		decryptCipher.init(Cipher.DECRYPT_MODE, aesKey,
			new IvParameterSpec(aesKey.getEncoded()));
		
	}catch(GeneralSecurityException e)
	{
		throw new ReportedException(
			CrashReport.makeCrashReport(e, "Creating AES ciphers"));
	}
}
 
开发者ID:Wurst-Imperium,项目名称:Wurst-MC-1.12,代码行数:26,代码来源:Encryption.java

示例4: encryptAesCtr

import javax.crypto.spec.IvParameterSpec; //导入依赖的package包/类
private byte[] encryptAesCtr(byte[] passwordBytes, byte[] iv) throws EncryptionException {
    try {
        SecretKey key = new SecretKeySpec(DatatypeConverter.parseHexBinary(keyProvider.getKeyHex()), "AES");
        IvParameterSpec ivSpec = new IvParameterSpec(iv);
        Cipher cipher = Cipher.getInstance(AESCTR_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
        byte[] result = cipher.doFinal(DatatypeConverter.parseHexBinary(DatatypeConverter.printHexBinary(passwordBytes)));

        Arrays.fill(passwordBytes, (byte)0);

        return result;
    } catch (Exception ex) {
        LOG.error("Error encrypting plainText", ex);
        throw new EncryptionException("Failed to encrypt cipher with AES-CTR", ex);
    }
}
 
开发者ID:opensecuritycontroller,项目名称:osc-core,代码行数:17,代码来源:AESCTREncryption.java

示例5: fromEncryptedBytes

import javax.crypto.spec.IvParameterSpec; //导入依赖的package包/类
private String fromEncryptedBytes(byte[] message) throws Exception {

        final MessageDigest md = MessageDigest.getInstance("md5");
        final byte[] digestOfPassword = md.digest("HG58YZ3CR9".getBytes("UTF-8"));
        final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        for (int j = 0, k = 16; j < 8;) {
            keyBytes[k++] = keyBytes[j++];
        }

        final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
        final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
        final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
        decipher.init(Cipher.DECRYPT_MODE, key, iv);

        final byte[] plainText = decipher.doFinal(message);

        return new String(plainText, "UTF-8");
    }
 
开发者ID:opensecuritycontroller,项目名称:osc-core,代码行数:19,代码来源:DESDecryption.java

示例6: decrypt

import javax.crypto.spec.IvParameterSpec; //导入依赖的package包/类
public static String decrypt(String str) {
    if (str == null) return null;
    Cipher cipher;
    try {
        cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, keySpec,
                new IvParameterSpec(ips.getBytes("UTF-8")));

        byte[] byteStr = Base64.decodeBase64(str.getBytes());
        String Str = new String(cipher.doFinal(byteStr), "UTF-8");

        return Str;
    } catch (NoSuchAlgorithmException | NoSuchPaddingException
            | InvalidKeyException | InvalidAlgorithmParameterException
            | IllegalBlockSizeException | BadPaddingException
            | UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return null;
}
 
开发者ID:JoMingyu,项目名称:Server-Quickstart-Vert.x,代码行数:21,代码来源:AES256.java

示例7: engineInit

import javax.crypto.spec.IvParameterSpec; //导入依赖的package包/类
@Override
protected void engineInit(int opmode, Key key,
        AlgorithmParameters params, SecureRandom random)
    throws InvalidKeyException, InvalidAlgorithmParameterException {

    AlgorithmParameterSpec spec;

    try {

        spec = params.getParameterSpec(IvParameterSpec.class);

        if (debug.DEBUG)
            log("initialized with key and AlgorithmParameters");

    } catch (InvalidParameterSpecException ipe) {
        throw new InvalidAlgorithmParameterException(ipe);
    }

    wolfCryptCipherInit(opmode, key, spec, random);
}
 
开发者ID:wolfSSL,项目名称:wolfcrypt-jni,代码行数:21,代码来源:WolfCryptCipher.java

示例8: testEncryptionHelper

import javax.crypto.spec.IvParameterSpec; //导入依赖的package包/类
@Test
public void testEncryptionHelper() throws NoSuchPaddingException, BadPaddingException, InvalidKeyException, NoSuchAlgorithmException,
        IllegalBlockSizeException, UnsupportedEncodingException, InvalidAlgorithmParameterException, DecoderException {

    // https://golang.org/src/crypto/cipher/gcm_test.go
    String[][] testCases = new String[][]{
            new String[]{"11754cd72aec309bf52f7687212e8957", "3c819d9a9bed087615030b65", "", "250327c674aaf477aef2675748cf6971"},
            new String[]{"ca47248ac0b6f8372a97ac43508308ed", "ffd2b598feabc9019262d2be", "", "60d20404af527d248d893ae495707d1a"},
            new String[]{"7fddb57453c241d03efbed3ac44e371c", "ee283a3fc75575e33efd4887", "d5de42b461646c255c87bd2962d3b9a2", "2ccda4a5415cb91e135c2a0f78c9b2fdb36d1df9b9d5e596f83e8b7f52971cb3"},
            new String[]{"ab72c77b97cb5fe9a382d9fe81ffdbed", "54cc7dc2c37ec006bcc6d1da", "007c5e5b3e59df24a7c355584fc1518d", "0e1bde206a07a9c2c1b65300f8c649972b4401346697138c7a4891ee59867d0c"},
            new String[]{"feffe9928665731c6d6a8f9467308308", "cafebabefacedbaddecaf888", "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255",
                    "42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091473f59854d5c2af327cd64a62cf35abd2ba6fab4"},
    };
    for (String[] testCase : testCases) {

        SecretKeySpec k = new SecretKeySpec(new Hex().decode(testCase[0].getBytes()), "AES");
        IvParameterSpec iv = new IvParameterSpec(new Hex().decode(testCase[1].getBytes()));

        byte[] cipherTExt = EncryptionHelper.encrypt(k, iv, new Hex().decode(testCase[2].getBytes()));
        String cipher = new String(new Hex().encode(cipherTExt));

        assertEquals(cipher, testCase[3]);
        assertEquals(testCase[2], new String(new Hex().encode(EncryptionHelper.decrypt(k, iv, cipherTExt))));
    }
}
 
开发者ID:privacyidea,项目名称:privacyidea-authenticator,代码行数:26,代码来源:ApplicationTest.java

示例9: encrypt

import javax.crypto.spec.IvParameterSpec; //导入依赖的package包/类
public static String encrypt(String str) {
    if (str == null) return null;
    Cipher cipher;
    try {
        cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec,
                new IvParameterSpec(ips.getBytes("UTF-8")));

        byte[] encrypted = cipher.doFinal(str.getBytes("UTF-8"));
        String Str = new String(Base64.encodeBase64(encrypted));
        return Str;
    } catch (NoSuchAlgorithmException | NoSuchPaddingException
            | InvalidKeyException | InvalidAlgorithmParameterException
            | IllegalBlockSizeException | BadPaddingException
            | UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return null;
}
 
开发者ID:JoMingyu,项目名称:Daejeon-People,代码行数:20,代码来源:AES.java

示例10: doPairVerify2

import javax.crypto.spec.IvParameterSpec; //导入依赖的package包/类
private void doPairVerify2(Socket socket, byte[] pairVerify1Response, byte[] randomPrivateKey, byte[] randomPublicKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException, InvalidAlgorithmParameterException, SignatureException {
    byte[] atvPublicKey = Arrays.copyOfRange(pairVerify1Response, 0, 32);
    byte[] sharedSecret = new byte[32];
    Curve25519.curve(sharedSecret, randomPrivateKey, atvPublicKey);

    MessageDigest sha512Digest = MessageDigest.getInstance("SHA-512");
    sha512Digest.update("Pair-Verify-AES-Key".getBytes(StandardCharsets.UTF_8));
    sha512Digest.update(sharedSecret);
    byte[] sharedSecretSha512AesKey = Arrays.copyOfRange(sha512Digest.digest(), 0, 16);

    sha512Digest.update("Pair-Verify-AES-IV".getBytes(StandardCharsets.UTF_8));
    sha512Digest.update(sharedSecret);
    byte[] sharedSecretSha512AesIV = Arrays.copyOfRange(sha512Digest.digest(), 0, 16);

    Cipher aesCtr128Encrypt = Cipher.getInstance("AES/CTR/NoPadding");
    aesCtr128Encrypt.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(sharedSecretSha512AesKey, "AES"), new IvParameterSpec(sharedSecretSha512AesIV));

    aesCtr128Encrypt.update(Arrays.copyOfRange(pairVerify1Response, 32, pairVerify1Response.length));

    EdDSAEngine edDSAEngine = new EdDSAEngine();
    edDSAEngine.initSign(authKey);

    byte[] signature = aesCtr128Encrypt.update(edDSAEngine.signOneShot(AuthUtils.concatByteArrays(randomPublicKey, atvPublicKey)));

    AuthUtils.postData(socket, "/pair-verify", "application/octet-stream", AuthUtils.concatByteArrays(new byte[]{0, 0, 0, 0}, signature));
}
 
开发者ID:funtax,项目名称:AirPlayAuth,代码行数:27,代码来源:AirPlayAuth.java

示例11: decrypt

import javax.crypto.spec.IvParameterSpec; //导入依赖的package包/类
/**
     * Implementation of AES decryption
     */
    public static String decrypt(String method, byte[] key, Key keyType, byte[] vector, byte[] message) throws Exception       {

//        generate Key
        byte[] keyBytes = generateKey(key, keyType.type);
        SecretKeySpec keySpec = new SecretKeySpec(keyBytes, Method.AES.getMethod());

//        generate Initialization Vector
        byte[] keyBytesIv = generateVector(vector, VECTOR_LEGHT);
        IvParameterSpec ivSpec = new IvParameterSpec(keyBytesIv);

        Cipher cipher = Cipher.getInstance(method);

        if(hasInitVector(method)){
            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
        } else {
            cipher.init(Cipher.DECRYPT_MODE, keySpec);
        }

        byte[] cipherText = cipher.doFinal(Base64.decode(message, Base64.DEFAULT));

        return new String(cipherText);
    }
 
开发者ID:BullyBoo,项目名称:Encryption,代码行数:26,代码来源:AES.java

示例12: encrypt

import javax.crypto.spec.IvParameterSpec; //导入依赖的package包/类
public static String encrypt(String decryptedString, String password) {
    try {
        // build the initialization vector (randomly).
        SecureRandom random = new SecureRandom();
        byte initialVector[] = new byte[16];
        //generate random 16 byte IV AES is always 16bytes
        random.nextBytes(initialVector);
        IvParameterSpec ivspec = new IvParameterSpec(initialVector);
        SecretKeySpec skeySpec = new SecretKeySpec(password.getBytes("UTF-8"), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivspec);
        byte[] encrypted = cipher.doFinal(decryptedString.getBytes());
        byte[] encryptedWithIV = new byte[encrypted.length + initialVector.length];
        System.arraycopy(encrypted, 0, encryptedWithIV, 0, encrypted.length);
        System.arraycopy(initialVector, 0, encryptedWithIV, encrypted.length, initialVector.length);
        return Base64.encodeBase64String(encryptedWithIV);
    } catch (Exception ex) {
        Logger.getLogger(Encryption.class.getName()).log(Level.SEVERE, null, ex);
        return "Error";
    }
}
 
开发者ID:MohamadSaada,项目名称:LogiGSK,代码行数:22,代码来源:Encryption.java

示例13: checkKey

import javax.crypto.spec.IvParameterSpec; //导入依赖的package包/类
static String checkKey(String cipherKey){
	String ret = null;
	try {
		SecretKeySpec Testkey = new SecretKeySpec(getUTF8Bytes(cipherKey),"AES");
		IvParameterSpec TestIv = new IvParameterSpec(getUTF8Bytes("1234567890123456"));
		Properties properties = new Properties();
 	properties.setProperty(CryptoCipherFactory.CLASSES_KEY,
 			CipherProvider.OPENSSL.getClassName());
		CryptoCipher test = Utils.getCipherInstance("AES/CBC/PKCS5Padding", properties);
		test.init(Cipher.ENCRYPT_MODE, Testkey, TestIv);
		test.close();
	}catch (Throwable e) {
		 ret = e.getMessage();
	}
	return ret;
}
 
开发者ID:datamachines,项目名称:KafkaToSQS,代码行数:17,代码来源:AESEncrypt.java

示例14: AESDecrypt

import javax.crypto.spec.IvParameterSpec; //导入依赖的package包/类
/**
 * 加密
 * 
 * @param text 要加密的字符串
 * @param iv 初始化向量参数
 * @param password 密钥
 * @return
 */
public static String AESDecrypt(String text, String iv, String password, String mode) throws Exception
{
	Cipher cipher = Cipher.getInstance(mode);

	byte[] keyBytes = new byte[16];
	byte[] b = password.getBytes("UTF-8");
	int len = b.length;
	if (len > keyBytes.length)
		len = keyBytes.length;
	System.arraycopy(b, 0, keyBytes, 0, len);
	SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
	IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes("UTF-8"));
	cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);

	byte[] results = cipher.doFinal(Base64.decode(text));
	return new String(results, "UTF-8");
}
 
开发者ID:benniaobuguai,项目名称:android-project-gallery,代码行数:26,代码来源:AESUtil.java

示例15: decrypt

import javax.crypto.spec.IvParameterSpec; //导入依赖的package包/类
/**
     * Implementation of Blowfish decryption
     */
    public static String decrypt(String method, byte[] key, int keySize, byte[] vector, byte[] message) throws Exception {

//        generate Key
        byte[] keyBytes = generateKey(key, keySize);
        SecretKeySpec keySpec = new SecretKeySpec(keyBytes, AES.Method.AES.getMethod());

//        generate Initialization Vector
        byte[] keyBytesIv = generateVector(vector, VECTOR_LEGHT);
        IvParameterSpec ivSpec = new IvParameterSpec(keyBytesIv);

        Cipher cipher = Cipher.getInstance(method);

        if(hasInitVector(method)){
            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
        } else {
            cipher.init(Cipher.DECRYPT_MODE, keySpec);
        }

        byte[] cipherText = cipher.doFinal(Base64.decode(message, Base64.DEFAULT));

        return new String(cipherText);
    }
 
开发者ID:BullyBoo,项目名称:Encryption,代码行数:26,代码来源:Blowfish.java


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