当前位置: 首页>>代码示例>>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;未经允许,请勿转载。