public void generateLocalKeyPair(String fullUserId) {

    String userId = Address.stripResource(fullUserId);

    OtrDebugLogger.log("generating local key pair for: " + userId);

    KeyPair keyPair;
    try {

      KeyPairGenerator kpg = KeyPairGenerator.getInstance(KEY_ALG);
      kpg.initialize(KEY_SIZE);

      keyPair = kpg.genKeyPair();
    } catch (NoSuchAlgorithmException e) {
      OtrDebugLogger.log("no such algorithm", e);
      return;
    }

    OtrDebugLogger.log("SUCCESS! generating local key pair for: " + userId);

    // Store Private Key.
    PrivateKey privKey = keyPair.getPrivate();
    PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privKey.getEncoded());

    this.store.setProperty(userId + ".privateKey", pkcs8EncodedKeySpec.getEncoded());

    // Store Public Key.
    PublicKey pubKey = keyPair.getPublic();
    storeLocalPublicKey(userId, pubKey);

    store.save();
  }
Exemplo n.º 2
0
  public static void SaveKeyPair(String path, KeyPair keyPair) throws IOException {
    PrivateKey privateKey = keyPair.getPrivate();
    PublicKey publicKey = keyPair.getPublic();

    // Store Public Key.
    X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded());
    FileOutputStream fos = new FileOutputStream(path + "/public.key");
    fos.write(x509EncodedKeySpec.getEncoded());
    fos.close();

    // Store Private Key.
    PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded());
    fos = new FileOutputStream(path + "/private.key");
    fos.write(pkcs8EncodedKeySpec.getEncoded());
    fos.close();
  }
  /**
   * Generate a local key pair. Be careful. If there is already an key pair, it will override it
   *
   * @param sessionID the sessionID that is identified with the local machine
   */
  public void generateLocalKeyPair(SessionID sessionID) {
    if (sessionID == null) return;

    String accountID = sessionID.getAccountID();
    KeyPair keyPair;
    try {
      keyPair = KeyPairGenerator.getInstance("DSA").genKeyPair();
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
      return;
    }

    // Store Public Key.
    PublicKey pubKey = keyPair.getPublic();
    X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(pubKey.getEncoded());

    this.store.setProperty(accountID + ".publicKey", x509EncodedKeySpec.getEncoded());

    // Store Private Key.
    PrivateKey privKey = keyPair.getPrivate();
    PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privKey.getEncoded());

    this.store.setProperty(accountID + ".privateKey", pkcs8EncodedKeySpec.getEncoded());
  }
Exemplo n.º 4
0
 // internal implementation of generatePrivate. See JCA doc
 private PrivateKey generatePrivate(KeySpec keySpec) throws GeneralSecurityException {
   if (keySpec instanceof PKCS8EncodedKeySpec) {
     PKCS8EncodedKeySpec pkcsSpec = (PKCS8EncodedKeySpec) keySpec;
     return RSAPrivateCrtKeyImpl.newKey(pkcsSpec.getEncoded());
   } else if (keySpec instanceof RSAPrivateCrtKeySpec) {
     RSAPrivateCrtKeySpec rsaSpec = (RSAPrivateCrtKeySpec) keySpec;
     return new RSAPrivateCrtKeyImpl(
         rsaSpec.getModulus(),
         rsaSpec.getPublicExponent(),
         rsaSpec.getPrivateExponent(),
         rsaSpec.getPrimeP(),
         rsaSpec.getPrimeQ(),
         rsaSpec.getPrimeExponentP(),
         rsaSpec.getPrimeExponentQ(),
         rsaSpec.getCrtCoefficient());
   } else if (keySpec instanceof RSAPrivateKeySpec) {
     RSAPrivateKeySpec rsaSpec = (RSAPrivateKeySpec) keySpec;
     return new RSAPrivateKeyImpl(rsaSpec.getModulus(), rsaSpec.getPrivateExponent());
   } else {
     throw new InvalidKeySpecException(
         "Only RSAPrivate(Crt)KeySpec "
             + "and PKCS8EncodedKeySpec supported for RSA private keys");
   }
 }
Exemplo n.º 5
0
  public static void main(String[] args) {
    if (args.length < 1) {
      System.err.println("Usage: java CreateKeyStore <file> <JKS|PKCS12> <crt> <entry> <key>");
      return;
    }

    String inFile = args[0];
    String outFile = args[0];
    String storeType = args.length < 2 ? "JKS" : args[1];
    String certificate = args.length < 3 ? null : args[2];
    String entry = args.length < 4 ? "mykey" : args[3];
    String keyFile = args.length < 5 ? null : args[4];
    FileInputStream inStream = null;
    FileOutputStream outStream = null;

    try {

      KeyStore store = KeyStore.getInstance(storeType);
      if (null != certificate) {
        store.load(null, null);

        // Read certificate
        inStream = new FileInputStream(certificate);
        CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
        Certificate cert = certFactory.generateCertificate(inStream);
        inStream.close();

        // Import certificate
        outStream = new FileOutputStream(outFile);
        if (null == keyFile) {
          store.setCertificateEntry(entry, cert);
        }
        // Read key
        else {
          inStream = new FileInputStream(keyFile);

          byte[] data = new byte[636];
          int read = inStream.read(data);
          PKCS8EncodedKeySpec pkcs8 = new PKCS8EncodedKeySpec(data);

          System.err.println(pkcs8.getFormat());

          KeyFactory keyFactory = KeyFactory.getInstance("RSA");
          Key key = keyFactory.generatePrivate(pkcs8);

          inStream.close();
        }

        store.store(outStream, "changeit".toCharArray());
        outStream.close();
      } else {
        store.load(null, null);

        outStream = new FileOutputStream(outFile);
        store.store(outStream, "changeit".toCharArray());
        outStream.close();
      }
    } catch (Exception exception) {
      System.err.println("[ERROR]: " + exception.getMessage());
      exception.printStackTrace();
    } finally {
      try {
        if (null != inStream) inStream.close();
        if (null != outStream) outStream.close();
      } catch (IOException exception) {
        System.err.println("Error closing files: " + exception.getMessage());
      }
    }
  }