javax.crypto.Cipher.getInstance()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(10.6k)|赞(0)|评价(0)|浏览(325)

本文整理了Java中javax.crypto.Cipher.getInstance()方法的一些代码示例,展示了Cipher.getInstance()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Cipher.getInstance()方法的具体详情如下:
包路径:javax.crypto.Cipher
类名称:Cipher
方法名:getInstance

Cipher.getInstance介绍

[英]Creates a new Cipher for the specified transformation. The installed providers are searched in order for an implementation of the specified transformation. The first found provider providing the transformation is used to create the cipher. If no provider is found an exception is thrown.
[中]为指定的转换创建新密码。将搜索已安装的提供程序,以查找指定转换的实现。第一个提供转换的提供程序用于创建密码。如果未找到提供程序,则引发异常。

代码示例

代码示例来源:origin: jenkinsci/jenkins

private static Cipher toCipher(RSAKey key, int mode) throws GeneralSecurityException {
  Cipher cipher = Cipher.getInstance("RSA");
  cipher.init(mode, (Key)key);
  return cipher;
}

代码示例来源:origin: jenkinsci/jenkins

/**
 * Upgrades a connection with transport encryption by the specified symmetric cipher.
 *
 * @return
 *      A new {@link Connection} object that includes the transport encryption.
 */
public Connection encryptConnection(SecretKey sessionKey, String algorithm) throws IOException, GeneralSecurityException {
  Cipher cout = Cipher.getInstance(algorithm);
  cout.init(Cipher.ENCRYPT_MODE, sessionKey, new IvParameterSpec(sessionKey.getEncoded()));
  CipherOutputStream o = new CipherOutputStream(out, cout);
  Cipher cin = Cipher.getInstance(algorithm);
  cin.init(Cipher.DECRYPT_MODE, sessionKey, new IvParameterSpec(sessionKey.getEncoded()));
  CipherInputStream i = new CipherInputStream(in, cin);
  return new Connection(i,o);
}

代码示例来源:origin: gocd/gocd

public static String encryptUsingAES(SecretKey secretKey, String dataToEncrypt) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
  Cipher aesCipher = Cipher.getInstance("AES");
  aesCipher.init(Cipher.ENCRYPT_MODE, secretKey);
  byte[] byteCipherText = aesCipher.doFinal(dataToEncrypt.getBytes());
  return Base64.getEncoder().encodeToString(byteCipherText);
}

代码示例来源:origin: rapidoid/rapidoid

private byte[] aes(byte[] data, byte[] key, byte[] iv, int mode) throws Exception {
  Cipher cipher = Cipher.getInstance(AES_MODE);
  cipher.init(mode, new SecretKeySpec(key, "AES"), new IvParameterSpec(iv));
  return cipher.doFinal(data);
}

代码示例来源:origin: oracle/helidon

private static Cipher cipher(char[] masterPassword, byte[] salt, int cipherMode) throws ConfigEncryptionException {
  try {
    SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    KeySpec keySpec = new PBEKeySpec(masterPassword, salt, HASH_ITERATIONS, KEY_LENGTH);
    SecretKeySpec spec = new SecretKeySpec(secretKeyFactory.generateSecret(keySpec).getEncoded(), "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(cipherMode, spec, new IvParameterSpec(salt));
    return cipher;
  } catch (Exception e) {
    throw new ConfigEncryptionException("Failed to prepare a cipher instance", e);
  }
}

代码示例来源:origin: pentaho/pentaho-kettle

public static Cipher initDecryptionCipher( Key unwrappedKey, byte[] unencryptedKey ) throws NoSuchAlgorithmException,
 NoSuchPaddingException, InvalidKeyException {
 Cipher decryptionCip = Cipher.getInstance( SINGLE_KEY_ALGORITHM );
 if ( unwrappedKey != null ) {
  decryptionCip.init( Cipher.ENCRYPT_MODE, unwrappedKey );
 } else {
  SecretKeySpec sks = new SecretKeySpec( unencryptedKey, SINGLE_KEY_ALGORITHM );
  decryptionCip.init( Cipher.ENCRYPT_MODE, sks );
 }
 return decryptionCip;
}

代码示例来源:origin: Graylog2/graylog2-server

@Nullable
public static String encrypt(String plainText, String encryptionKey, String salt) {
  try {
    @SuppressWarnings("CIPHER_INTEGRITY")
    Cipher cipher = Cipher.getInstance("AES/CBC/ISO10126Padding", "SunJCE");
    SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
    cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(salt.getBytes("UTF-8")));
    return Hex.encodeToString(cipher.doFinal(plainText.getBytes("UTF-8")));
  } catch (Exception e) {
    LOG.error("Could not encrypt value.", e);
  }
  return null;
}

代码示例来源:origin: oracle/helidon

static Cipher cipher(char[] masterPassword, byte[] salt, int cipherMode) throws HttpAuthException {
  try {
    SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    KeySpec keySpec = new PBEKeySpec(masterPassword, salt, 10000, 128);
    SecretKeySpec spec = new SecretKeySpec(secretKeyFactory.generateSecret(keySpec).getEncoded(), "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(cipherMode, spec, new IvParameterSpec(salt));
    return cipher;
  } catch (Exception e) {
    throw new HttpAuthException("Failed to prepare a cipher instance", e);
  }
}

代码示例来源:origin: gocd/gocd

public static String decryptUsingAES(SecretKey secretKey, String dataToDecrypt) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    Cipher aesCipher = Cipher.getInstance("AES");
    aesCipher.init(Cipher.DECRYPT_MODE, secretKey);
    byte[] bytePlainText = aesCipher.doFinal(Base64.getDecoder().decode(dataToDecrypt));
    return new String(bytePlainText);
  }
}

代码示例来源:origin: GlowstoneMC/Glowstone

private CryptBuf(int mode, SecretKey sharedSecret) throws GeneralSecurityException {
  cipher = Cipher.getInstance("AES/CFB8/NoPadding"); // NON-NLS
  cipher.init(mode, sharedSecret, new IvParameterSpec(sharedSecret.getEncoded()));
}

代码示例来源:origin: Graylog2/graylog2-server

@Nullable
  public static String decrypt(String cipherText, String encryptionKey, String salt) {
    try {
      @SuppressWarnings("CIPHER_INTEGRITY")
      Cipher cipher = Cipher.getInstance("AES/CBC/ISO10126Padding", "SunJCE");
      SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
      cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(salt.getBytes("UTF-8")));
      return new String(cipher.doFinal(Hex.decode(cipherText)), "UTF-8");
    } catch (Exception e) {
      LOG.error("Could not decrypt value.", e);
    }
    return null;
  }
}

代码示例来源:origin: oblac/jodd

public PBKDF2Encryptor(final String passPhrase, final byte[] salt, final int iterationCount, final int i1) {
  this.iterationCount = iterationCount;
  try {
    // create the key
    KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount, i1);
    SecretKey tmp = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256").generateSecret(keySpec);
    SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
    // encryptor
    ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    ecipher.init(Cipher.ENCRYPT_MODE, secret);
    AlgorithmParameters params = ecipher.getParameters();
    byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
    // decryptor
    dcipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    dcipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));
  }
  catch (Exception ex) {
    throw new IllegalArgumentException(ex);
  }
}

代码示例来源:origin: gocd/gocd

public static String decryptUsingRSA(String cipherText, String privateKeyContent) throws NoSuchPaddingException, NoSuchAlgorithmException, IOException, InvalidKeySpecException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException {
  Cipher decryptCipher = Cipher.getInstance("RSA");
  decryptCipher.init(Cipher.DECRYPT_MODE, getRSAPrivateKeyFrom(privateKeyContent));
  return new String(decryptCipher.doFinal(Base64.getDecoder().decode(cipherText)), UTF_8);
}

代码示例来源:origin: AsyncHttpClient/async-http-client

/** Calculates RC4 */
private static byte[] RC4(final byte[] value, final byte[] key) throws NtlmEngineException {
  try {
    final Cipher rc4 = Cipher.getInstance("RC4");
    rc4.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "RC4"));
    return rc4.doFinal(value);
  } catch (final Exception e) {
    throw new NtlmEngineException(e.getMessage(), e);
  }
}

代码示例来源:origin: bwssytems/ha-bridge

private String encrypt(String property) throws GeneralSecurityException, UnsupportedEncodingException {
  SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
  SecretKey key = keyFactory.generateSecret(new PBEKeySpec(habridgeKey));
  Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
  pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
  return base64Encode(pbeCipher.doFinal(property.getBytes("UTF-8")));
}

代码示例来源:origin: gocd/gocd

public static String encryptUsingRSA(String plainText, String publicKeyContent) throws InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException, IOException, BadPaddingException, IllegalBlockSizeException, InvalidKeySpecException {
  Cipher encryptCipher = Cipher.getInstance("RSA");
  encryptCipher.init(Cipher.ENCRYPT_MODE, getRSAPublicKeyFrom(publicKeyContent));
  return Base64.getEncoder().encodeToString(encryptCipher.doFinal(plainText.getBytes(UTF_8)));
}

代码示例来源:origin: wildfly/wildfly

@Override
 public String encode(String secret) throws Exception {
   SecretKeySpec key = new SecretKeySpec(internalKey, "Blowfish");
   Cipher cipher = Cipher.getInstance("Blowfish");
   cipher.init(Cipher.ENCRYPT_MODE, key);
   byte[] encoding = cipher.doFinal(secret.getBytes());
   BigInteger n = new BigInteger(encoding);
   return n.toString(16);
 }
}

代码示例来源:origin: bwssytems/ha-bridge

private String decrypt(String property) throws GeneralSecurityException, IOException {
  SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
  SecretKey key = keyFactory.generateSecret(new PBEKeySpec(habridgeKey));
  Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
  pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
  return new String(pbeCipher.doFinal(base64Decode(property)), "UTF-8");
}

代码示例来源:origin: alibaba/druid

public static String decrypt(PublicKey publicKey, String cipherText)
    throws Exception {
  Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
  try {
    cipher.init(Cipher.DECRYPT_MODE, publicKey);
  } catch (InvalidKeyException e) {
    // 因为 IBM JDK 不支持私钥加密, 公钥解密, 所以要反转公私钥
    // 也就是说对于解密, 可以通过公钥的参数伪造一个私钥对象欺骗 IBM JDK
    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);
}

代码示例来源:origin: apache/kylin

public static String encrypt(String strToEncrypt) {
  try {
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    final String encryptedString = Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes(
      StandardCharsets.UTF_8)));
    return encryptedString;
  } catch (Exception e) {
    throw new RuntimeException(e.getMessage(), e);
  }
}

相关文章