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


Java Cipher.getInstance方法代碼示例

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


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

示例1: getCipherFromPassphrase

import javax.crypto.Cipher; //導入方法依賴的package包/類
private static Cipher getCipherFromPassphrase(String passphrase, byte[] salt, int iterations, int opMode)
    throws GeneralSecurityException
{
  SecretKey key    = getKeyFromPassphrase(passphrase, salt, iterations);
  Cipher    cipher = Cipher.getInstance(key.getAlgorithm());
  cipher.init(opMode, key, new PBEParameterSpec(salt, iterations));

  return cipher;
}
 
開發者ID:CableIM,項目名稱:Cable-Android,代碼行數:10,代碼來源:MasterSecretUtil.java

示例2: encryptByPublicKey

import javax.crypto.Cipher; //導入方法依賴的package包/類
/**
 * 用公鑰加密
 *
 * @param data
 * @param key
 * @return
 * @throws Exception
 */
public static byte[] encryptByPublicKey(byte[] data, String key)
        throws Exception {
    byte[] keyBytes = BASE64.decode(key);   // 對公鑰解密

    // 取得公鑰
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance(Algorithm.RSA.getType());
    Key publicKey = keyFactory.generatePublic(x509KeySpec);

    // 對數據加密
    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);

    return cipher.doFinal(data);
}
 
開發者ID:ruiqiao2017,項目名稱:Renrentou,代碼行數:24,代碼來源:Codec.java

示例3: decryptData

import javax.crypto.Cipher; //導入方法依賴的package包/類
/**
 * Decrypt key (does not use salting, so the encryption result is the same for the same input)
 *
 * @param password      the secret key to use
 * @param encryptedData the data to decrypt
 * @return the decrypted data (the first four bytes is real data length in Big Endian)
 */
static byte[] decryptData(byte[] password, byte[] encryptedData) {
    try {
        Cipher c = Cipher.getInstance(ENCRYPT_DATA_ALGORITHM);
        c.init(Cipher.DECRYPT_MODE, new SecretKeySpec(password, SECRET_KEY_ALGORITHM), CBC_SALT_DATA);
        return c.doFinal(encryptedData);
    }
    catch (Exception e) {
        throw new IllegalStateException(ENCRYPT_DATA_ALGORITHM + " is not available", e);
    }
}
 
開發者ID:tranleduy2000,項目名稱:javaide,代碼行數:18,代碼來源:EncryptionUtils.java

示例4: encryptData

import javax.crypto.Cipher; //導入方法依賴的package包/類
/**
 * 使用私鑰加密數據
 * @param key 私鑰
 * @param data 待加密數據
 * @return
 */
public byte[] encryptData(PrivateKey key,byte[] data)
{
	try {
   		if(data==null || data.length<=0)
   		{
   			return null;
   		}
		Cipher cipher=Cipher.getInstance(algorithm);
		cipher.init(Cipher.ENCRYPT_MODE, key);
		return cipher.doFinal(data);
	} catch (Exception e) {
		log.error(e.getMessage(),e);
	}
   	return null;
}
 
開發者ID:juebanlin,項目名稱:util4j,代碼行數:22,代碼來源:RsaUtil.java

示例5: encryptIvNotInitialize1

import javax.crypto.Cipher; //導入方法依賴的package包/類
public static void encryptIvNotInitialize1(String message) throws Exception {

        byte[] iv = new byte[] {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; //Oups. Static

        //IV
        IvParameterSpec ivSpec = new IvParameterSpec(iv);

        //Key
        KeyGenerator generator = KeyGenerator.getInstance("AES");
        generator.init(128);
        SecretKey secretKey = generator.generateKey();

        //Encrypt
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);
        cipher.update(message.getBytes());

        byte[] data = cipher.doFinal();
        System.out.println(HexUtil.toString(data));
    }
 
開發者ID:blackarbiter,項目名稱:Android_Code_Arbiter,代碼行數:21,代碼來源:ConstantIv.java

示例6: cipherPassword

import javax.crypto.Cipher; //導入方法依賴的package包/類
public static String cipherPassword(String password, String hash, RSAPublicKey rsaPublicKey) throws InvalidKeyException {
    try {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, rsaPublicKey);
        return new String(
                Base64.getEncoder().encode(
                        cipher.doFinal((hash + password).getBytes())
                )
        );
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
        throw new Error(e);
    }
}
 
開發者ID:czp3009,項目名稱:bilibili-api,代碼行數:14,代碼來源:Utils.java

示例7: initCiphers

import javax.crypto.Cipher; //導入方法依賴的package包/類
private void initCiphers(String algo, SecretKey key,
        AlgorithmParameterSpec aps) throws NoSuchAlgorithmException,
        NoSuchPaddingException, InvalidKeyException,
        InvalidAlgorithmParameterException {
    Provider provider = Security.getProvider("SunJCE");
    if (provider == null) {
        throw new RuntimeException("SunJCE provider does not exist.");
    }
    Cipher ci1 = Cipher.getInstance(algo, provider);
    ci1.init(Cipher.ENCRYPT_MODE, key, aps);
    pair[0] = ci1;
    Cipher ci2 = Cipher.getInstance(algo, provider);
    ci2.init(Cipher.DECRYPT_MODE, key, aps);
    pair[1] = ci2;
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:16,代碼來源:CICOSkipTest.java

示例8: decrypt

import javax.crypto.Cipher; //導入方法依賴的package包/類
public static String decrypt(byte[] encryptedValue , String passPhrase) throws Exception {
    Key key = generateKey(passPhrase);
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.DECRYPT_MODE, key);
    byte[] decrypted = c.doFinal(encryptedValue);
    return new String(decrypted);
}
 
開發者ID:gagoyal01,項目名稱:mongodb-rdbms-sync,代碼行數:8,代碼來源:EncryptorDecryptor.java

示例9: decrypt

import javax.crypto.Cipher; //導入方法依賴的package包/類
private static byte[] decrypt(byte[] iv, byte[] data,int sortNum) throws Exception {
	byte[] pass = findkeyBySortNum(sortNum);
	Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
	IvParameterSpec dps = new IvParameterSpec(iv);
	SecretKeySpec skeySpec = new SecretKeySpec(pass, "AES");
	cipher.init(Cipher.DECRYPT_MODE, skeySpec, dps);
	byte[] buf = cipher.doFinal(data);
	return buf;
}
 
開發者ID:microshow,項目名稱:fastokhttp,代碼行數:10,代碼來源:DynamicEncryptUtil.java

示例10: encryptByPublicKey

import javax.crypto.Cipher; //導入方法依賴的package包/類
/**
 * 公鑰加密
 * 
 * @param data 待加密數據
 * @param key 公鑰
 * @return byte[] 加密數據
 * @throws Exception
 */
public static byte[] encryptByPublicKey(byte[] data, byte[] key) throws Exception {
	// 取得公鑰
	X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);
	KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
	PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);
	// 對數據加密
	Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
	cipher.init(Cipher.ENCRYPT_MODE, publicKey);
	return cipher.doFinal(data);
}
 
開發者ID:tb544731152,項目名稱:iBase4J,代碼行數:19,代碼來源:RSACoder.java

示例11: aesDecrypt

import javax.crypto.Cipher; //導入方法依賴的package包/類
public static String aesDecrypt(String key, String sSrc) throws Exception {
    try {
        if (key == null) {
            throw new ExceptionInInitializerError("key can't be null");
        }
        if (key.length() != 16) {
            throw new ExceptionInInitializerError("key length must be 16!");
        }

        byte[] raw = key.getBytes("ASCII");
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(2, skeySpec);
        byte[] encrypted1 = hex2byte(sSrc);
        try {
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original);
            return originalString;
        } catch (Exception e) {
            System.out.println(e.toString());
            return null;
        }
    } catch (Exception ex) {
        System.out.println(ex.toString());
    }
    return null;
}
 
開發者ID:wolfboys,項目名稱:opencron,代碼行數:28,代碼來源:DigestUtils.java

示例12: encrypt

import javax.crypto.Cipher; //導入方法依賴的package包/類
public static byte[] encrypt(byte[] data, String key) throws Exception {
    Key k = toKey(decryptBASE64(key));
    Cipher cipher = Cipher.getInstance(ALGORITHM);
    cipher.init(Cipher.ENCRYPT_MODE, k);

    return cipher.doFinal(data);
}
 
開發者ID:Paleozoic,項目名稱:storm_spring_boot_demo,代碼行數:8,代碼來源:EncryptorUtils.java

示例13: loadAesKey

import javax.crypto.Cipher; //導入方法依賴的package包/類
private SecretKey loadAesKey(Path path, KeyPair pair)
	throws GeneralSecurityException, IOException
{
	Cipher cipher = Cipher.getInstance("RSA");
	cipher.init(Cipher.DECRYPT_MODE, pair.getPrivate());
	
	return new SecretKeySpec(cipher.doFinal(Files.readAllBytes(path)),
		"AES");
}
 
開發者ID:Wurst-Imperium,項目名稱:Wurst-MC-1.12,代碼行數:10,代碼來源:Encryption.java

示例14: createNetCipherInstance

import javax.crypto.Cipher; //導入方法依賴的package包/類
/**
 * Creates an Cipher instance using the AES/CFB8/NoPadding algorithm. Used for protocol encryption.
 */
public static Cipher createNetCipherInstance(int opMode, Key key)
{
    try
    {
        Cipher cipher = Cipher.getInstance("AES/CFB8/NoPadding");
        cipher.init(opMode, (Key)key, (AlgorithmParameterSpec)(new IvParameterSpec(key.getEncoded())));
        return cipher;
    }
    catch (GeneralSecurityException generalsecurityexception)
    {
        throw new RuntimeException(generalsecurityexception);
    }
}
 
開發者ID:SkidJava,項目名稱:BaseClient,代碼行數:17,代碼來源:CryptManager.java

示例15: onCreate

import javax.crypto.Cipher; //導入方法依賴的package包/類
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_exported);

    Log.d("redis", "Initialising jedis...");
    Jedis jedis = new Jedis("localhost");

    try {
        Cipher.getInstance("DES/ECB/ZeroBytePadding", "BC");
    } catch (NoSuchAlgorithmException|NoSuchProviderException|NoSuchPaddingException e) {
        // pass
    }
}
 
開發者ID:viren-nadkarni,項目名稱:mfva,代碼行數:15,代碼來源:ExportedActivity.java


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