List of usage examples for java.security Key getAlgorithm
public String getAlgorithm();
From source file:Main.java
public static void main(String[] args) throws Exception { // Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); byte[] input = "input".getBytes(); byte[] ivBytes = "1234567812345678".getBytes(); Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding", "BC"); KeyGenerator generator = KeyGenerator.getInstance("AES", "BC"); generator.init(128);//www . ja va 2 s . co m Key encryptionKey = generator.generateKey(); System.out.println("key : " + new String(encryptionKey.getEncoded())); cipher.init(Cipher.ENCRYPT_MODE, encryptionKey, new IvParameterSpec(ivBytes)); byte[] cipherText = new byte[cipher.getOutputSize(input.length)]; int ctLength = cipher.update(input, 0, input.length, cipherText, 0); ctLength += cipher.doFinal(cipherText, ctLength); Key decryptionKey = new SecretKeySpec(encryptionKey.getEncoded(), encryptionKey.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, decryptionKey, new IvParameterSpec(ivBytes)); byte[] plainText = new byte[cipher.getOutputSize(ctLength)]; int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0); ptLength += cipher.doFinal(plainText, ptLength); System.out.println("plain : " + new String(plainText)); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); byte[] input = "www.java2s.com".getBytes(); byte[] ivBytes = new byte[] { 0x00, 0x00, 0x00, 0x01, 0x04, 0x05, 0x06, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };/*ww w . j a va2s.c o m*/ Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding", "BC"); KeyGenerator generator = KeyGenerator.getInstance("AES", "BC"); generator.init(192); Key encryptionKey = generator.generateKey(); System.out.println("key : " + Utils.toHex(encryptionKey.getEncoded())); System.out.println("input : " + new String(input)); // encryption pass cipher.init(Cipher.ENCRYPT_MODE, encryptionKey, new IvParameterSpec(ivBytes)); byte[] cipherText = new byte[cipher.getOutputSize(input.length)]; int ctLength = cipher.update(input, 0, input.length, cipherText, 0); ctLength += cipher.doFinal(cipherText, ctLength); // decryption pass Key decryptionKey = new SecretKeySpec(encryptionKey.getEncoded(), encryptionKey.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, decryptionKey, new IvParameterSpec(ivBytes)); byte[] plainText = new byte[cipher.getOutputSize(ctLength)]; int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0); ptLength += cipher.doFinal(plainText, ptLength); System.out.println("plain : " + new String(plainText) + " bytes: " + ptLength); }
From source file:Main.java
/** * Returns true if the key is an elliptic curve public or private key. *//*from w ww. j a v a2 s . c o m*/ public static boolean isEcKey(Key key) { return "EC".equals(key.getAlgorithm()); }
From source file:Main.java
/** * Returns true if the key is an RSA public or private key. *//*w ww.j a v a2s .c o m*/ public static boolean isRsaKey(Key key) { return "RSA".equals(key.getAlgorithm()); }
From source file:Main.java
public static String formatKey(Key key) { String algo = key.getAlgorithm(); String fmt = key.getFormat(); byte[] encoded = key.getEncoded(); return "Key[algorithm=" + algo + ", format=" + fmt + ", bytes=" + encoded.length + "]"; }
From source file:Main.java
private static String getSignatureAlgorithm(Key key) throws Exception { if ("EC".equals(key.getAlgorithm())) { int curveSize; KeyFactory factory = KeyFactory.getInstance("EC"); if (key instanceof PublicKey) { ECPublicKeySpec spec = factory.getKeySpec(key, ECPublicKeySpec.class); curveSize = spec.getParams().getCurve().getField().getFieldSize(); } else if (key instanceof PrivateKey) { ECPrivateKeySpec spec = factory.getKeySpec(key, ECPrivateKeySpec.class); curveSize = spec.getParams().getCurve().getField().getFieldSize(); } else {//from w w w. j a v a2 s. c o m throw new InvalidKeySpecException(); } if (curveSize <= 256) { return "SHA256withECDSA"; } else if (curveSize <= 384) { return "SHA384withECDSA"; } else { return "SHA512withECDSA"; } } else if ("RSA".equals(key.getAlgorithm())) { return "SHA256withRSA"; } else { throw new IllegalArgumentException("Unsupported key type " + key.getAlgorithm()); } }
From source file:com.kuzumeji.platform.standard.SecurityServiceTest.java
private static String dumpKeyPair(final Key key) { return MessageFormat.format("?:{0} ?:{1} ?:{2}", key.getAlgorithm(), key.getFormat(), Hex.encodeHexString(key.getEncoded())); }
From source file:com.google.api.auth.DefaultJwksSupplierTest.java
private static void assertKeysEqual(Key expected, Key actual) { assertEquals(expected.getAlgorithm(), actual.getAlgorithm()); assertEquals(new String(Hex.encode(expected.getEncoded())), new String(Hex.encode(actual.getEncoded()))); assertEquals(expected.getFormat(), actual.getFormat()); }
From source file:net.fender.crypto.CryptoUtil.java
/** * NB: Ciphers are not thread safe. Don't reuse the instance returned by * this method in multiple threads.// ww w.j ava2s. c o m * * @param keyName * @param mode * @return * @throws GeneralSecurityException */ public static Cipher getSystemPropertyCipher(String keyName, int mode) throws GeneralSecurityException { Key key = getSystemPropertyKey(keyName); Cipher cipher = Cipher.getInstance(key.getAlgorithm()); cipher.init(mode, key); return cipher; }
From source file:net.fender.crypto.CryptoUtil.java
/** * @param keyName// w w w .j a va 2s .c om * @param plaintext * @return * @throws GeneralSecurityException */ public static String encryptUsingSystemPropertyKey(String keyName, String plaintext) throws GeneralSecurityException { Key key = getSystemPropertyKey(keyName); Cipher cipher = Cipher.getInstance(key.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] encryptedByes = cipher.doFinal(plaintext.getBytes()); String encrypted = new String(Base64.encodeBase64(encryptedByes)); return encrypted; }