List of usage examples for javax.crypto KeyGenerator getInstance
public static final KeyGenerator getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:wssec.TestWSSecurityNew14.java
/** * Setup method/* w w w .j a v a 2 s . c o m*/ * <p/> * * @throws java.lang.Exception Thrown when there is a problem in setup */ protected void setUp() throws Exception { AxisClient tmpEngine = new AxisClient(new NullProvider()); msgContext = new MessageContext(tmpEngine); unsignedEnvelope = getSOAPEnvelope(); KeyGenerator keyGen = KeyGenerator.getInstance("AES"); keyGen.init(128); key = keyGen.generateKey(); keyData = key.getEncoded(); }
From source file:com.kk.dic.action.Upload.java
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); out = response.getWriter();//from www.j ava2 s .c o m Connection con; PreparedStatement pstm = null; String fname = ""; String keyword = ""; String cd = ""; String a = (String) request.getSession().getAttribute("email"); System.out.println("User Name : " + a); try { boolean isMultipartContent = ServletFileUpload.isMultipartContent(request); if (!isMultipartContent) { return; } FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); out.print("one"); try { List<FileItem> fields = upload.parseRequest(request); Iterator<FileItem> it = fields.iterator(); if (!it.hasNext()) { return; } while (it.hasNext()) { FileItem fileItem = it.next(); if (fileItem.getFieldName().equals("name")) { fname = fileItem.getString(); System.out.println("File Name" + fname); } else if (fileItem.getFieldName().equals("keyword")) { keyword = fileItem.getString(); System.out.println("File Keyword" + keyword); } else { } boolean isFormField = fileItem.isFormField(); if (isFormField) { } else { out.print("one"); try { con = Dbconnection.getConnection(); pstm = con.prepareStatement( "insert into files (file, keyword, filetype, filename, CDate, owner, size, data, frank, file_key)values(?,?,?,?,?,?,?,?,?,?)"); out.println("getD " + fileItem.getName()); String str = getStringFromInputStream(fileItem.getInputStream()); // secretkey generating KeyGenerator keyGen = KeyGenerator.getInstance("AES"); keyGen.init(128); SecretKey secretKey = keyGen.generateKey(); System.out.println("secret key:" + secretKey); //converting secretkey to String byte[] be = secretKey.getEncoded();//encoding secretkey String skey = Base64.encode(be); System.out.println("converted secretkey to string:" + skey); String cipher = new encryption().encrypt(str, secretKey); System.out.println(str); //for get extension from given file String b = fileItem.getName().substring(fileItem.getName().lastIndexOf('.')); System.out.println("File Extension" + b); pstm.setBinaryStream(1, fileItem.getInputStream()); pstm.setString(2, keyword); pstm.setString(3, b); pstm.setString(4, fname); pstm.setDate(5, getCurrentDate()); pstm.setString(6, a); pstm.setLong(7, fileItem.getSize()); pstm.setString(8, cipher); pstm.setString(9, "0"); pstm.setString(10, skey); /*Cloud Start*/ File f = new File("D:/" + fileItem.getName()); out.print("<br/>" + f.getName()); FileWriter fw = new FileWriter(f); fw.write(cipher); fw.close(); Ftpcon ftpcon = new Ftpcon(); ftpcon.upload(f, fname); /*Cloud End*/ int i = pstm.executeUpdate(); if (i == 1) { response.sendRedirect("upload.jsp?msg=success"); } else { response.sendRedirect("upload.jsp?msgg=failed"); } con.close(); } catch (Exception e) { out.println(e); } } } } catch (Exception ex) { out.print(ex); Logger.getLogger(Upload.class.getName()).log(Level.SEVERE, null, ex); } } finally { out.close(); } }
From source file:org.sonar.process.AesCipher.java
String generateRandomSecretKey() { try {//from ww w . j a va2s. c o m KeyGenerator keyGen = KeyGenerator.getInstance(CRYPTO_KEY); keyGen.init(KEY_SIZE_IN_BITS, new SecureRandom()); SecretKey secretKey = keyGen.generateKey(); return Base64.encodeBase64String(secretKey.getEncoded()); } catch (Exception e) { throw new IllegalStateException("Fail to generate secret key", e); } }
From source file:org.lightjason.agentspeak.action.builtin.TestCActionCrypto.java
/** * test wrong algorithm//from w w w.j ava 2 s . com * * @throws NoSuchAlgorithmException is thrown on key generator error */ @Test public final void wrongalgorithm() throws NoSuchAlgorithmException { final Key l_key = KeyGenerator.getInstance("HmacSHA1").generateKey(); Assert.assertFalse(new CEncrypt() .execute(false, IContext.EMPTYPLAN, Stream.of(l_key).map(CRawTerm::from).collect(Collectors.toList()), Collections.emptyList()) .value()); Assert.assertFalse(new CDecrypt() .execute(false, IContext.EMPTYPLAN, Stream.of(l_key).map(CRawTerm::from).collect(Collectors.toList()), Collections.emptyList()) .value()); }
From source file:com.muk.services.commerce.CryptoServiceImpl.java
@PostConstruct public void postConstruct() { try {//w w w . j ava 2 s . c om final KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); temporaryKey = new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"); final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); final byte[] iv = new byte[cipher.getBlockSize()]; new SecureRandom().nextBytes(iv); ivSpec = new IvParameterSpec(iv); } catch (final NoSuchAlgorithmException ex) { LOG.error("Failed to initalize encryption key", ex); } catch (final NoSuchPaddingException padEx) { LOG.error("Failed to get cipher.", padEx); } }
From source file:de.taimos.dvalin.interconnect.model.MessageCryptoUtil.java
private static void generateKey() { try {//w ww . j a va 2 s . c o m final KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); final SecretKey skey = kgen.generateKey(); System.out.println("Key: " + Hex.encodeHexString(skey.getEncoded())); } catch (final NoSuchAlgorithmException e) { e.printStackTrace(); } }
From source file:org.silverpeas.core.security.encryption.ContentEncryptionServiceTest.java
/** * Generates a key for an AES enciphering. * @return the key in hexadecimal.//w ww.j a v a2 s. c o m * @throws Exception if the key cannot be generated. */ public static String generateAESKey() throws Exception { KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); keyGenerator.init(256); SecretKey skey = keyGenerator.generateKey(); return EncodingUtil.asHex(skey.getEncoded()); }
From source file:com.miyue.util.Cryptos.java
/** * ?AES,?128,192,256?./*from w w w. j av a 2 s . co m*/ */ public static byte[] generateAesKey(int keysize) { try { KeyGenerator keyGenerator = KeyGenerator.getInstance(AES); keyGenerator.init(keysize); SecretKey secretKey = keyGenerator.generateKey(); return secretKey.getEncoded(); } catch (GeneralSecurityException e) { throw Exceptions.unchecked(e); } }
From source file:com.ironchain.common.kits.DigestKit.java
/** * ?HMAC-SHA1,,160?(20). HMAC-SHA1?,//from www . j a va 2s .c o m * RFC2401160?(20). */ public static byte[] generateHmacSha1Key() { try { KeyGenerator keyGenerator = KeyGenerator.getInstance(HMACSHA1); keyGenerator.init(DEFAULT_HMACSHA1_KEYSIZE); SecretKey secretKey = keyGenerator.generateKey(); return secretKey.getEncoded(); } catch (GeneralSecurityException e) { throw new RuntimeException(e); } }
From source file:org.opensafety.hishare.util.implementation.EncryptionImpl.java
public String createPassword() throws CryptographyException { KeyGenerator kgen;/*from w w w .j a va 2s. c o m*/ try { kgen = KeyGenerator.getInstance(keyGenerator); } catch (NoSuchAlgorithmException e) { throw new CryptographyException(e.getMessage()); } kgen.init(passwordLength); SecretKey skey = kgen.generateKey(); byte[] raw = skey.getEncoded(); return new String(Hex.encodeHex(raw)); }