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


Java Cipher.init方法代碼示例

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


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

示例1: initCipher

import javax.crypto.Cipher; //導入方法依賴的package包/類
/**
 * Initiate the Cipher object for PBKDF2 algorithm using given "mode".
 *
 * @param mode Cipher mode: encrypt or decrypt
 * @return Cipher object for PBKDF2 algorithm
 * @throws GeneralSecurityException all security exceptions are thrown.
 */
@Override
protected Cipher initCipher(int mode) throws GeneralSecurityException {
    Provider provider = Security.getProvider("SunJCE");
    if (provider == null) {
        throw new RuntimeException("SunJCE provider does not exist.");
    }
    // Generate secret key
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(),
            salt, DEFAULT_ITERATION, PKDF2_DEFAULT_KEY_LEN);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(baseAlgo);
    SecretKey key = keyFactory.generateSecret(pbeKeySpec);

    // get Cipher instance
    Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORMATION, provider);
    cipher.init(mode,
            new SecretKeySpec(key.getEncoded(),KEY_ALGORITHM),
            new IvParameterSpec(iv));
    return cipher;
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:27,代碼來源:PBKDF2Wrapper.java

示例2: decrypt

import javax.crypto.Cipher; //導入方法依賴的package包/類
private String decrypt(String data) {
    try {
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(2, this.key);
        return new String(cipher.doFinal(Hex.decodeHex(data.toCharArray())));
    } catch (Exception e) {
        e.printStackTrace();
        return "";
    }
}
 
開發者ID:JackChan1999,項目名稱:boohee_v5.6,代碼行數:11,代碼來源:DnspodEnterprise.java

示例3: decryptByPrivateKey

import javax.crypto.Cipher; //導入方法依賴的package包/類
/**
 * 私鑰解密
 * 
 * @param data
 * @param privateKey
 * @return
 * @throws Exception
 */
public static String decryptByPrivateKey(String data,
		RSAPrivateKey privateKey) throws Exception {
	Cipher cipher = Cipher.getInstance("RSA");
	cipher.init(Cipher.DECRYPT_MODE, privateKey);
	// 模長
	int key_len = privateKey.getModulus().bitLength() / 8;
	byte[] bytes = data.getBytes();
	byte[] bcd = ASCII_To_BCD(bytes, bytes.length);
	System.err.println("bcd.length:"+bcd.length);
	// 如果密文長度大於模長則要分組解密
	String ming = "";
	byte[][] arrays = splitArray(bcd, key_len);
	for (byte[] arr : arrays) {
		ming += new String(cipher.doFinal(arr));
	}
	return ming;
}
 
開發者ID:GroupControlDroid,項目名稱:GroupControlDroidClient,代碼行數:26,代碼來源:RSAUtil.java

示例4: seal

import javax.crypto.Cipher; //導入方法依賴的package包/類
/**
 * Seals the given cleartext key, using the password provided at
 * construction time
 */
SealedObject seal(Key key)
    throws Exception
{
    // create a random salt (8 bytes)
    byte[] salt = new byte[8];
    SunJCE.getRandom().nextBytes(salt);

    // create PBE parameters from salt and iteration count
    PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, 20);

    // create PBE key from password
    PBEKeySpec pbeKeySpec = new PBEKeySpec(this.password);
    SecretKey sKey = new PBEKey(pbeKeySpec, "PBEWithMD5AndTripleDES");
    pbeKeySpec.clearPassword();

    // seal key
    Cipher cipher;

    PBEWithMD5AndTripleDESCipher cipherSpi;
    cipherSpi = new PBEWithMD5AndTripleDESCipher();
    cipher = new CipherForKeyProtector(cipherSpi, SunJCE.getInstance(),
                                       "PBEWithMD5AndTripleDES");
    cipher.init(Cipher.ENCRYPT_MODE, sKey, pbeSpec);
    return new SealedObjectForKeyProtector(key, cipher);
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:30,代碼來源:KeyProtector.java

示例5: createCipher

import javax.crypto.Cipher; //導入方法依賴的package包/類
/**
 * Create a Cipher object for the requested encryption/decryption mode.
 *
 * @param mode encryption or decryption mode
 * @return Cipher object initiated to perform requested mode operation
 */
private Cipher createCipher(int mode, AlgorithmParameters params)
        throws Exception {
    Cipher ci;
    if (Cipher.ENCRYPT_MODE == mode) {
        // create a new Cipher object for encryption
        ci = Cipher.getInstance(transformation, provider);

        // initiate it with the saved parameters
        if (params != null) {
            ci.init(Cipher.ENCRYPT_MODE, key, params);
        } else {
            // initiate the cipher without parameters
            ci.init(Cipher.ENCRYPT_MODE, key);
        }
    } else {
        // it is expected that parameters already generated
        // before decryption
        ci = Cipher.getInstance(transformation, provider);
        ci.init(Cipher.DECRYPT_MODE, key, params);
    }

    return ci;
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:30,代碼來源:Encrypt.java

示例6: decrypt

import javax.crypto.Cipher; //導入方法依賴的package包/類
public static String decrypt(PublicKey publicKey, String cipherText)
        throws Exception {
    Cipher cipher = Cipher.getInstance("RSA");
    try {
        cipher.init(Cipher.DECRYPT_MODE, publicKey);
    } catch (InvalidKeyException e) {
        //  IBM JDK not support Private key encryption, public key decryption
        // so fake an PrivateKey for it
        RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;
        RSAPrivateKeySpec spec = new RSAPrivateKeySpec(rsaPublicKey.getModulus(), rsaPublicKey.getPublicExponent());
        Key fakePrivateKey = KeyFactory.getInstance("RSA").generatePrivate(spec);
        cipher = Cipher.getInstance("RSA"); //It is a stateful object. so we need to get new one.
        cipher.init(Cipher.DECRYPT_MODE, fakePrivateKey);
    }

    if (cipherText == null || cipherText.length() == 0) {
        return cipherText;
    }

    byte[] cipherBytes = Base64.base64ToByteArray(cipherText);
    byte[] plainBytes = cipher.doFinal(cipherBytes);

    return new String(plainBytes);
}
 
開發者ID:actiontech,項目名稱:dble,代碼行數:25,代碼來源:DecryptUtil.java

示例7: deCrypt

import javax.crypto.Cipher; //導入方法依賴的package包/類
/**
 *
 * 進行3-DES解密(密鑰匙等同於加密的密鑰匙)。
 *
 * @param byte[]
 *          src 要進行3-DES解密byte[]
 * @param String
 *          spkey分配的SPKEY
 * @return String 3-DES解密後的String
 */
public static String deCrypt(byte[] debase64, String spKey) {
  String strDe = null;
  Cipher cipher = null;
  try {
    cipher = Cipher.getInstance("DESede");
    byte[] key = getEnKey(spKey);
    DESedeKeySpec dks = new DESedeKeySpec(key);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
    SecretKey sKey = keyFactory.generateSecret(dks);
    cipher.init(Cipher.DECRYPT_MODE, sKey);
    byte ciphertext[] = cipher.doFinal(debase64);
    strDe = new String(ciphertext, "UTF-16LE");
  } catch (Exception ex) {
    strDe = "";
    ex.printStackTrace();
  }
  return strDe;
}
 
開發者ID:Lingzh0ng,項目名稱:ITSM,代碼行數:29,代碼來源:Endecrypt.java

示例8: encode

import javax.crypto.Cipher; //導入方法依賴的package包/類
public static String encode(String input) {
	try {
		LOG.debug("intput string:" + input);
		SecretKey deskey = new SecretKeySpec(getKey(), Algorithm);
		Cipher c1 = Cipher.getInstance(Algorithm);
		c1.init(Cipher.ENCRYPT_MODE, deskey);
		byte[] cipherByte = c1.doFinal(input.getBytes("utf-8"));
		LOG.debug("ouput string:" + byte2hex(cipherByte));
		return byte2hex(cipherByte);
	} catch (Exception e) {
		LOG.error("CryptoUtil.encode error!", e);
		return null;
	}
}
 
開發者ID:DataAgg,項目名稱:DaUtil,代碼行數:15,代碼來源:CryptoUtil.java

示例9: AESDecrypt

import javax.crypto.Cipher; //導入方法依賴的package包/類
/**
 * @param keyStr
 *         密鑰
 * @param encryptData
 *         解密數據
 *
 * @return String
 *
 * @throws
 * @Description: 解密
 */
public static String AESDecrypt(final String keyStr, final String encryptData) throws Exception {
    byte[] decrypt = null;
    try {
        Key key = generateKey(keyStr);
        Cipher cipher = Cipher.getInstance(AES_TYPE);
        cipher.init(Cipher.DECRYPT_MODE, key);
        decrypt = cipher.doFinal(Base64.decodeBase64(encryptData.getBytes()));
        return new String(decrypt).trim();
    } catch (Exception e) {
        LOGGER.error("aes decrypt failure : {}", e);
        throw e;
    }
}
 
開發者ID:AsuraTeam,項目名稱:asura,代碼行數:25,代碼來源:AESEncrypt.java

示例10: encrypt

import javax.crypto.Cipher; //導入方法依賴的package包/類
public static String encrypt(String plainText, String key){
    try{
        PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(Base64.decode(key, Base64.DEFAULT)));
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        return Base64.encodeToString(cipher.doFinal(plainText.getBytes("UTF-8")),Base64.DEFAULT);
    }catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
開發者ID:theapache64,項目名稱:CCAvenue-Android-Example-Advanced,代碼行數:12,代碼來源:RSAUtility.java

示例11: decrypt

import javax.crypto.Cipher; //導入方法依賴的package包/類
public static byte[] decrypt(Key key, byte[] value) {
    try {
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, key);
        return cipher.doFinal(value);
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}
 
開發者ID:NexusByte,項目名稱:LotusCloud,代碼行數:11,代碼來源:Crypter.java

示例12: decrypt

import javax.crypto.Cipher; //導入方法依賴的package包/類
/**
 * 解密
 *
 * @param content       密文
 * @param private_key   商戶私鑰
 * @param input_charset 編碼格式
 * @return 解密後的字符串
 */
public static String decrypt(String content, String private_key, String input_charset) throws Exception {
	PrivateKey prikey = getPrivateKey(private_key);

	Cipher cipher = Cipher.getInstance("RSA");
	cipher.init(Cipher.DECRYPT_MODE, prikey);

	InputStream ins = new ByteArrayInputStream(Base64.decodeBase64(content));
	ByteArrayOutputStream writer = new ByteArrayOutputStream();
	//rsa解密的字節大小最多是128,將需要解密的內容,按128位拆開解密
	byte[] buf = new byte[128];
	int bufl;

	while ((bufl = ins.read(buf)) != -1) {
		byte[] block = null;

		if (buf.length == bufl) {
			block = buf;
		} else {
			block = new byte[bufl];
			for (int i = 0; i < bufl; i++) {
				block[i] = buf[i];
			}
		}

		writer.write(cipher.doFinal(block));
	}

	return new String(writer.toByteArray(), input_charset);
}
 
開發者ID:funtl,項目名稱:framework,代碼行數:38,代碼來源:RSASign.java

示例13: encryptAES256PKCS5Padding

import javax.crypto.Cipher; //導入方法依賴的package包/類
public static String encryptAES256PKCS5Padding(String key, String src)
        throws NoSuchPaddingException, NoSuchAlgorithmException,
        UnsupportedEncodingException, BadPaddingException,
        IllegalBlockSizeException, InvalidAlgorithmParameterException,
        InvalidKeyException {

    Cipher cipher = Cipher.getInstance(ALGORITHM_ENCODING);
    cipher.init(Cipher.ENCRYPT_MODE, generateKeyAES256(key),
            new IvParameterSpec(new byte[cipher.getBlockSize()]));
    return Base64.encodeBytes(cipher.doFinal(src.getBytes()));
}
 
開發者ID:prosikito,項目名稱:SecureUtils,代碼行數:12,代碼來源:AES256PKCS5Padding.java

示例14: decrypt

import javax.crypto.Cipher; //導入方法依賴的package包/類
/**
 * 解密
 *
 * @param key  解密的密鑰
 * @param data 已經加密的數據
 * @return 解密後的明文
 */
private static byte[] decrypt(Key key, byte[] data, PADDING padding) {
    try {
        Cipher cipher = Cipher.getInstance(CHIPER_ALGORITHM + (padding == null ? PADDING.NoPadding : padding));
        cipher.init(Cipher.DECRYPT_MODE, key);
        return cipher.doFinal(data);
    } catch (Exception e) {
        throw new RuntimeException("Error when decrypt data, errmsg: " + e.getMessage(), e);
    }
}
 
開發者ID:TIIEHenry,項目名稱:TIIEHenry-Android-SDK,代碼行數:17,代碼來源:RSAUtils.java

示例15: decrypt

import javax.crypto.Cipher; //導入方法依賴的package包/類
public static String decrypt(String data) throws Exception {
    Key deskey = keyGenerator(desKey);
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    //初始化Cipher對象,設置為解密模式
    IvParameterSpec iv = new IvParameterSpec(DES_IV);
    AlgorithmParameterSpec paramSpec = iv;        
    cipher.init(Cipher.DECRYPT_MODE, deskey,iv);
    // 執行解密操作
    return new String(cipher.doFinal(Base64.decodeBase64(data)));
}
 
開發者ID:jeffreyning,項目名稱:nh-micro,代碼行數:11,代碼來源:DESUtil.java


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