List of usage examples for java.security KeyPair getPrivate
public PrivateKey getPrivate()
From source file:org.globus.gsi.bc.BouncyCastleOpenSSLKeyTest.java
public void testEcryptedToString() throws Exception { KeyPair keyPair = getKeyPair(); OpenSSLKey inKey = new BouncyCastleOpenSSLKey(keyPair.getPrivate()); assertTrue(!inKey.isEncrypted());//from w w w . java 2 s .c o m inKey.encrypt(pwd); assertTrue(inKey.isEncrypted()); ByteArrayInputStream in = null; in = new ByteArrayInputStream(toString(inKey).getBytes()); OpenSSLKey outKey = new BouncyCastleOpenSSLKey(in); assertTrue(outKey.isEncrypted()); in = new ByteArrayInputStream(toString(outKey).getBytes()); OpenSSLKey outKey2 = new BouncyCastleOpenSSLKey(in); assertTrue(outKey2.isEncrypted()); }
From source file:com.kuzumeji.platform.standard.SecurityServiceTest.java
@Test public void testEncryptDecrypt() { // RSA???//from w w w . j a v a 2 s .c o m final KeyPair keyPair = testee.generateKeyPair(); final RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); final RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); // ??? final char[] password = "hello, world!!".toCharArray(); final byte[] salt = new byte[] { 1 }; final byte[] secretKey = testee.createCommonKey(password, salt); LOG.debug("? : {}", Hex.encodeHexString(secretKey)); // ??? final byte[] encryptedSecretKey = testee.encrypt(publicKey, secretKey); LOG.debug("??? : {}", Hex.encodeHexString(encryptedSecretKey)); // ??? final byte[] decryptedSecretKey = testee.decrypt(privateKey, encryptedSecretKey); assertThat(decryptedSecretKey, is(secretKey)); LOG.debug("??? : {}", Hex.encodeHexString(decryptedSecretKey)); // ??? final String text = "?????????????"; LOG.debug(" : {}", text); final SecuredData context = testee.encrypt(secretKey, text.getBytes()); LOG.debug("??={}", Hex.encodeHexString(context.getEncrypted())); LOG.debug("???(IV)={}", Hex.encodeHexString(context.getVector())); // ??? final byte[] decryptedMessage = testee.decrypt(secretKey, context); assertThat(decryptedMessage, is(text.getBytes())); LOG.debug("? : {}", new String(decryptedMessage)); }
From source file:org.apache.karaf.shell.ssh.keygenerator.OpenSSHGeneratorKeyFileProviderTest.java
@Test public void writeSshKey() throws Exception { File temp = File.createTempFile(this.getClass().getCanonicalName(), ".pem"); temp.deleteOnExit();//from w w w .java2 s . c om KeyPair kp = new OpenSSHKeyPairGenerator(KeyUtils.RSA_ALGORITHM, 1024).generate(); new PemWriter(temp).writeKeyPair(KeyUtils.RSA_ALGORITHM, kp); //File path = new File("/home/cschneider/.ssh/id_rsa"); OpenSSHKeyPairProvider prov = new OpenSSHKeyPairProvider(temp, KeyUtils.RSA_ALGORITHM, 1024); KeyPair keys = prov.loadKeys().iterator().next(); Assert.assertNotNull(keys); Assert.assertTrue("Loaded key is not RSA Key", keys.getPrivate() instanceof RSAPrivateCrtKey); }
From source file:net.solarnetwork.pki.bc.test.BCCertificateServiceTest.java
@Before public void setup() { service = new BCCertificateService(); KeyPairGenerator keyGen;/*www .j av a2 s .co m*/ try { keyGen = KeyPairGenerator.getInstance("RSA"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } keyGen.initialize(2048, new SecureRandom()); KeyPair keypair = keyGen.generateKeyPair(); publicKey = keypair.getPublic(); privateKey = keypair.getPrivate(); }
From source file:net.groupbuy.service.impl.RSAServiceImpl.java
@Transactional(readOnly = true) public RSAPublicKey generateKey(HttpServletRequest request) { Assert.notNull(request);//from w w w .ja v a2s .c o m KeyPair keyPair = RSAUtils.generateKeyPair(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); HttpSession session = request.getSession(); session.setAttribute(PRIVATE_KEY_ATTRIBUTE_NAME, privateKey); return publicKey; }
From source file:com.iterzp.momo.service.impl.RSAServiceImpl.java
@Override @Transactional(readOnly = true)/*w w w . j a v a 2s .c o m*/ public RSAPublicKey generateKey(HttpServletRequest request) { Assert.notNull(request); KeyPair keyPair = RSAUtils.generateKeyPair(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); HttpSession session = request.getSession(); session.setAttribute(PRIVATE_KEY_ATTRIBUTE_NAME, privateKey); return publicKey; }
From source file:org.apache.karaf.shell.ssh.OpenSSHGeneratorFileKeyProvider.java
@Override protected void doWriteKeyPair(String resourceKey, KeyPair kp, OutputStream os) throws IOException, GeneralSecurityException { Collection<Object> items = new ArrayList<>(); items.add(kp.getPrivate()); items.add(kp.getPublic());// w w w .j a va 2 s .com byte[] bytes = PEMUtil.encode(items); os.write(bytes); }
From source file:org.apache.cloudstack.utils.auth.SAMLUtils.java
public static X509Certificate generateRandomX509Certificate(KeyPair keyPair) throws NoSuchAlgorithmException, NoSuchProviderException, CertificateEncodingException, SignatureException, InvalidKeyException { Date validityBeginDate = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000); Date validityEndDate = new Date(System.currentTimeMillis() + 3 * 365 * 24 * 60 * 60 * 1000); X500Principal dnName = new X500Principal("CN=ApacheCloudStack"); X509V1CertificateGenerator certGen = new X509V1CertificateGenerator(); certGen.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis())); certGen.setSubjectDN(dnName);/* w ww .j a va 2 s.c o m*/ certGen.setIssuerDN(dnName); certGen.setNotBefore(validityBeginDate); certGen.setNotAfter(validityEndDate); certGen.setPublicKey(keyPair.getPublic()); certGen.setSignatureAlgorithm("SHA256WithRSAEncryption"); return certGen.generate(keyPair.getPrivate(), "BC"); }
From source file:com.bluepixel.security.manager.Server.java
private void generateKey() { try {//from www.j a v a 2 s .c om KeyPairGenerator keyGen = KeyPairGenerator.getInstance(DEFAULT_ALGORITHM); keyGen.initialize(DEFAULT_KEY_LENGTH); KeyPair keypair = keyGen.generateKeyPair(); PublicKey pbKey = keypair.getPublic(); PrivateKey piKey = keypair.getPrivate(); publicKey = Base64.encodeWebSafe(pbKey.getEncoded(), false); privateKey = Base64.encodeWebSafe(piKey.getEncoded(), false); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.ENCRYPT_MODE, piKey); secretKeys = new ConcurrentHashMap<String, String>(); String[] randomKeys = generateRandomWords(10); for (String key : randomKeys) { String cipherText = Base64.encodeWebSafe(cipher.doFinal(key.getBytes()), false); secretKeys.put(key, cipherText); } } catch (NoSuchAlgorithmException e) { } catch (InvalidKeyException e) { } catch (NoSuchPaddingException e) { } catch (IllegalBlockSizeException e) { } catch (BadPaddingException e) { } }
From source file:org.cesecore.keys.key.management.CertificateKeyAssociationDataTest.java
@Before public void generateCertificate() throws Exception { final KeyPair keys = KeyTools.genKeys("512", AlgorithmConstants.KEYALGORITHM_RSA); final Certificate certificate = CertTools.genSelfCert("C=SE,O=Test,CN=Test", 365, null, keys.getPrivate(), keys.getPublic(), AlgorithmConstants.SIGALG_SHA1_WITH_RSA, true); final List<String> tags = Arrays.asList(tag_array); ckad = new CertificateKeyAssociationData(certificate, tags, keyAlias); }