use of java.security.spec.AlgorithmParameterSpec in project i2p.i2p by i2p.
the class SigUtil method fromJavaKey.
/**
* Use if SigType is unknown.
* For efficiency, use fromJavakey(pk, type) if type is known.
*
* @param pk JAVA key!
* @throws IllegalArgumentException on unknown type
* @since 0.9.18
*/
public static SigningPrivateKey fromJavaKey(PrivateKey pk) throws GeneralSecurityException {
if (pk instanceof DSAPrivateKey) {
return fromJavaKey((DSAPrivateKey) pk);
}
if (pk instanceof ECPrivateKey) {
ECPrivateKey k = (ECPrivateKey) pk;
AlgorithmParameterSpec spec = k.getParams();
SigType type;
if (spec.equals(SigType.ECDSA_SHA256_P256.getParams()))
type = SigType.ECDSA_SHA256_P256;
else if (spec.equals(SigType.ECDSA_SHA384_P384.getParams()))
type = SigType.ECDSA_SHA384_P384;
else if (spec.equals(SigType.ECDSA_SHA512_P521.getParams()))
type = SigType.ECDSA_SHA512_P521;
else
throw new IllegalArgumentException("Unknown EC type");
return fromJavaKey(k, type);
}
if (pk instanceof EdDSAPrivateKey) {
return fromJavaKey((EdDSAPrivateKey) pk, SigType.EdDSA_SHA512_Ed25519);
}
if (pk instanceof RSAPrivateKey) {
RSAPrivateKey k = (RSAPrivateKey) pk;
int sz = k.getModulus().bitLength();
SigType type;
if (sz <= ((RSAKeyGenParameterSpec) SigType.RSA_SHA256_2048.getParams()).getKeysize())
type = SigType.RSA_SHA256_2048;
else if (sz <= ((RSAKeyGenParameterSpec) SigType.RSA_SHA384_3072.getParams()).getKeysize())
type = SigType.RSA_SHA384_3072;
else if (sz <= ((RSAKeyGenParameterSpec) SigType.RSA_SHA512_4096.getParams()).getKeysize())
type = SigType.RSA_SHA512_4096;
else
throw new IllegalArgumentException("Unknown RSA type");
return fromJavaKey(k, type);
}
throw new IllegalArgumentException("Unknown type: " + pk.getClass());
}
use of java.security.spec.AlgorithmParameterSpec in project XUtil by xuexiangjys.
the class EncryptUtils method desTemplate.
/**
* DES 加密模板
*
* @param data 数据
* @param key 秘钥
* @param algorithm 加密算法
* @param transformation 转变
* @param isEncrypt {@code true}: 加密 {@code false}: 解密
* @return 密文或者明文,适用于 DES,3DES,AES
*/
private static byte[] desTemplate(final byte[] data, final byte[] key, final String algorithm, final String transformation, final byte[] iv, final boolean isEncrypt) {
if (data == null || data.length == 0 || key == null || key.length == 0)
return null;
try {
SecretKeySpec keySpec = new SecretKeySpec(key, algorithm);
Cipher cipher = Cipher.getInstance(transformation);
if (iv == null || iv.length == 0) {
cipher.init(isEncrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, keySpec);
} else {
AlgorithmParameterSpec params = new IvParameterSpec(iv);
cipher.init(isEncrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, keySpec, params);
}
return cipher.doFinal(data);
} catch (Throwable e) {
e.printStackTrace();
return null;
}
}
use of java.security.spec.AlgorithmParameterSpec in project credhub by cloudfoundry-incubator.
the class EncryptionService method encrypt.
public EncryptedValue encrypt(UUID canaryUuid, Key key, String value) throws Exception {
byte[] nonce = generateNonce();
AlgorithmParameterSpec parameterSpec = generateParameterSpec(nonce);
CipherWrapper encryptionCipher = getCipher();
encryptionCipher.init(Cipher.ENCRYPT_MODE, key, parameterSpec);
byte[] encrypted = encryptionCipher.doFinal(value.getBytes(CHARSET));
return new EncryptedValue(canaryUuid, encrypted, nonce);
}
use of java.security.spec.AlgorithmParameterSpec in project jruby-openssl by jruby.
the class Cipher method doInitCipher.
private void doInitCipher(final Ruby runtime) {
if (isDebug(runtime)) {
dumpVars(runtime.getOut(), "doInitCipher()");
}
checkCipherNotNull(runtime);
if (key == null) {
// key = emptyKey(keyLength);
throw newCipherError(runtime, "key not specified");
}
try {
// ECB mode is the only mode that does not require an IV
if ("ECB".equalsIgnoreCase(cryptoMode)) {
cipher.init(encryptMode ? ENCRYPT_MODE : DECRYPT_MODE, new SimpleSecretKey(getCipherAlgorithm(), this.key));
} else {
// if no IV yet, start out with all \0s
if (realIV == null)
realIV = new byte[ivLength];
if ("RC2".equalsIgnoreCase(cryptoBase)) {
cipher.init(encryptMode ? ENCRYPT_MODE : DECRYPT_MODE, new SimpleSecretKey("RC2", this.key), new RC2ParameterSpec(this.key.length * 8, this.realIV));
} else if ("RC4".equalsIgnoreCase(cryptoBase)) {
cipher.init(encryptMode ? ENCRYPT_MODE : DECRYPT_MODE, new SimpleSecretKey("RC4", this.key));
} else {
final AlgorithmParameterSpec ivSpec;
if ("GCM".equalsIgnoreCase(cryptoMode)) {
// e.g. 'aes-128-gcm'
ivSpec = new GCMParameterSpec(getAuthTagLength() * 8, this.realIV);
} else {
ivSpec = new IvParameterSpec(this.realIV);
}
cipher.init(encryptMode ? ENCRYPT_MODE : DECRYPT_MODE, new SimpleSecretKey(getCipherAlgorithm(), this.key), ivSpec);
}
}
} catch (InvalidKeyException e) {
throw newCipherError(runtime, e + "\n possibly you need to install Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files for your JRE");
} catch (Exception e) {
debugStackTrace(runtime, e);
throw newCipherError(runtime, e);
}
cipherInited = true;
processedDataBytes = 0;
// outBuffer = new ByteList(keyLength);
}
use of java.security.spec.AlgorithmParameterSpec in project mule by mulesoft.
the class AbstractJCEEncryptionStrategy method createAndInitCiphers.
protected void createAndInitCiphers() throws GeneralSecurityException {
encryptCipher = Cipher.getInstance(getAlgorithm());
decryptCipher = Cipher.getInstance(getAlgorithm());
AlgorithmParameterSpec paramSpec = createAlgorithmParameterSpec();
if (paramSpec != null) {
encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
decryptCipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
} else {
encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey);
decryptCipher.init(Cipher.DECRYPT_MODE, secretKey);
}
}
Aggregations