@Override public String toString() { return "RsaCipher [privateKey=" + new String(privateKey.getEncoded()) + ", publicKey=" + new String(publicKey.getEncoded()) + "]"; }
priv = (RSAPrivateKey) keys.getPrivate(); pub = (RSAPublicKey) keys.getPublic(); store(priv.getEncoded()); } else { KeyFactory keyFactory = KeyFactory.getInstance("RSA");
/** * <p>getEncodedPrivateKey.</p> * * @return an array of byte. */ public byte[] getEncodedPrivateKey() { return privateKey.getEncoded(); }
public RSAPrivateEncrypt(RSAPrivateKey privateKey) { this.privateKey = Base64.encodeBase64(privateKey.getEncoded()); }
public static String toBase64(final RSAPrivateKey privateKey) { return BaseEncoding.base64().encode(privateKey.getEncoded()); }
@Override protected void serialize(final Type type, final Object object, final DataOutputStream dos) throws IOException { final RSAPrivateKey key = (RSAPrivateKey) object; final byte[] encoded = new PKCS8EncodedKeySpec(key.getEncoded()).getEncoded(); dos.writeInt(encoded.length); dos.write(encoded); }
@Override public String toString() { return "RsaCipher [privateKey=" + new String(privateKey.getEncoded()) + ", publicKey=" + new String(publicKey.getEncoded()) + "]"; }
public static String encodePrivateKey(RSAPrivateKey key) { return new String(java.util.Base64.getMimeEncoder().encode(key.getEncoded())); }
/** * 生成密钥对。注意这里是生成密钥对KeyPair,再由密钥对获取公私钥 * * @return */ public static Map<String, byte[]> generateKeyBytes() { try { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM); keyPairGenerator.initialize(KEY_SIZE); KeyPair keyPair = keyPairGenerator.generateKeyPair(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); Map<String, byte[]> keyMap = new HashMap<String, byte[]>(); keyMap.put(PUBLIC_KEY, publicKey.getEncoded()); keyMap.put(PRIVATE_KEY, privateKey.getEncoded()); return keyMap; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return null; }
public static void genKeyPairFiles(final String filePath) { try { final KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA"); keyPairGen.initialize(1024); final KeyPair keyPair = keyPairGen.generateKeyPair(); final RSAPublicKey publicKey = (RSAPublicKey)keyPair.getPublic(); final RSAPrivateKey privateKey = (RSAPrivateKey)keyPair.getPrivate(); final byte[] publicKeyByte = publicKey.getEncoded(); final byte[] privateKeyByte = privateKey.getEncoded(); final RandomAccessFile publicKeyFile = new RandomAccessFile(filePath + "public.key", "rw"); publicKeyFile.write(KeyUtil.base64Encoder.encodeBuffer(publicKeyByte).getBytes("UTF-8")); publicKeyFile.close(); final RandomAccessFile privateKeyFile = new RandomAccessFile(filePath + "private.key", "rw"); privateKeyFile.write(KeyUtil.base64Encoder.encodeBuffer(privateKeyByte).getBytes("UTF-8")); privateKeyFile.close(); } catch (Exception e) { throw new RuntimeException("\u751f\u6210\u6210\u5bf9\u5bc6\u94a5\u5931\u8d25", e); } }
/** * The entry point of the RsaKeyGenerator. * * @param args The application arguments. */ public static void main(String[] args) throws Exception { Security.addProvider(new BouncyCastleProvider()); KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", "BC"); keyPairGenerator.initialize(BIT_COUNT); KeyPair keyPair = keyPairGenerator.generateKeyPair(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); System.out.println("Place these keys in the client:"); System.out.println("--------------------"); System.out.println("public key: " + publicKey.getPublicExponent()); System.out.println("modulus: " + publicKey.getModulus()); try (PemWriter writer = new PemWriter(new FileWriter(PRIVATE_KEY_FILE))) { writer.writeObject(new PemObject("RSA PRIVATE KEY", privateKey.getEncoded())); } catch (Exception e) { System.err.println("Failed to write private key to " + PRIVATE_KEY_FILE); e.printStackTrace(); } }
/** * <p>getEncodedLocalPrivateKey.</p> * * @return an array of byte. */ public byte[] getEncodedLocalPrivateKey() { if (localCertificate==null) return null; return localCertificate.getPrivateKey().getPrivateKey().getEncoded(); }
/** * 创建公钥私钥 * * @return 返回公私钥对 * @throws Exception */ public static KeyStore createKeys() throws Exception { KeyPairGenerator keyPairGeno = KeyPairGenerator.getInstance(RSA_ALGORITHM); keyPairGeno.initialize(1024); KeyPair keyPair = keyPairGeno.generateKeyPair(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); KeyStore keyStore = new KeyStore(); keyStore.setPublicKey(Base64.encodeBase64String(publicKey.getEncoded())); keyStore.setPrivateKey(Base64.encodeBase64String(privateKey.getEncoded())); return keyStore; }
public String getEncodedRsaPrivateKey() { return CodecUtils.encodeBase64(this.getRsaPrivateKey().getEncoded()); }
/** * 创建公钥私钥 * * @return 返回公私钥对 * @throws Exception */ public static KeyStore createKeys() throws Exception { KeyPairGenerator keyPairGeno = KeyPairGenerator.getInstance(RSA_ALGORITHM); keyPairGeno.initialize(1024); KeyPair keyPair = keyPairGeno.generateKeyPair(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); KeyStore keyStore = new KeyStore(); keyStore.setPublicKey(Base64.encodeBase64String(publicKey.getEncoded())); keyStore.setPrivateKey(Base64.encodeBase64String(privateKey.getEncoded())); return keyStore; }
/** * 初始化密钥 for RSA ALGORITHM * * @return * @throws Exception */ public static String[] initKey() throws Exception { KeyPairGenerator keyPairGen = KeyPairGenerator .getInstance(KEY_ALGORITHM_RSA); keyPairGen.initialize(1024); KeyPair keyPair = keyPairGen.generateKeyPair(); // 公钥 RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); // 私钥 RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); String[] publicAndPrivateKey = { encryptBASE64(publicKey.getEncoded()), encryptBASE64(privateKey.getEncoded())}; return publicAndPrivateKey; }
String privateKeyString = Base64.encodeToString(privateKey.getEncoded(),true);
/** * Save the private key to a PEM file * * @param file the file * @param privateKeyPassword the password used to store the key * @throws java.io.IOException if any. */ public void save(File file, String privateKeyPassword) throws IOException { if (privateKeyPassword == null || privateKeyPassword.length() == 0) { PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(getPrivateKey() .getEncoded()); FileWriter fw = new FileWriter(file); try { fw.append(BEGIN_RSA_PRIVATE_KEY); fw.append(StringUtils.addLineBreaks(CryptoUtil.base64Encode(spec.getEncoded()), 72)); fw.append(END_RSA_PRIVATE_KEY); } finally { fw.close(); } } else { savePemWithBC(file, privateKeyPassword); } }
public static void writeRsaPrivateKey(RSAPrivateKey privateKey, Writer sw) { try { PemWriter writer = new PemWriter(sw); writer.writeObject(new PemObject("RSA PRIVATE KEY", privateKey.getEncoded())); writer.flush(); //outputStream.write(pkcs8EncodedKeySpec.getEncoded()); } catch (IOException e) { throw new RuntimeException("Unable to write a private rsa key in output stream", e); } }
@Override public String generateKey(String projectId, String name) throws IOException, NoSuchAlgorithmException, AlreadyExistingException { log.debug("Generating keypair"); if (keyRepository.findKey(projectId, name) != null) throw new AlreadyExistingException("A key with the name " + name + " exists already."); KeyPair keyPair = KeyHelper.generateRSAKey(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); String publicKeyString = KeyHelper.encodePublicKey(publicKey, name); Key key = new Key(); key.setName(name); key.setProjectId(projectId); key.setFingerprint(KeyHelper.calculateFingerprint(publicKey.getEncoded())); key.setPublicKey(publicKeyString); log.debug(publicKeyString); keyRepository.save(key); log.info("Added new key: " + key); return KeyHelper.parsePrivateKey(privateKey.getEncoded()); }