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