本文整理汇总了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;
}
示例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);
}
示例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);
}
}
示例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;
}
示例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));
}
示例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);
}
}
示例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;
}
示例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);
}
示例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;
}
示例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);
}
示例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;
}
示例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);
}
示例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");
}
示例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);
}
}
示例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
}
}