當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。