public static PublicKeyRecord createNew(KeyPair kp)
      throws NoSuchAlgorithmException, InvalidKeySpecException {
    KeyFactory fact = KeyFactory.getInstance("RSA");
    RSAPublicKeySpec pub = fact.getKeySpec(kp.getPublic(), RSAPublicKeySpec.class);

    int id = 0;

    try {
      Connection conn = DatabaseConnection.getConn();
      String sql = "insert into publickey modulus = ?, exponent = ?";
      PreparedStatement stmt = conn.prepareStatement(sql);

      stmt.setString(1, pub.getModulus().toString());

      stmt.setString(2, pub.getPublicExponent().toString());
      stmt.executeUpdate();
      ResultSet generatedKeys = stmt.getGeneratedKeys();
      if (generatedKeys.next()) {
        id = generatedKeys.getInt(1);
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }

    return get(id);
  }
Esempio n. 2
0
 public static void savePublicKeyInFile(String path, PublicKey key) {
   try {
     KeyFactory fac = KeyFactory.getInstance("RSA");
     RSAPublicKeySpec RSAspec = fac.getKeySpec(key, RSAPublicKeySpec.class);
     FileOutputStream file3 = new FileOutputStream(path);
     ObjectOutputStream obj_stream = new ObjectOutputStream(file3);
     obj_stream.writeObject(RSAspec.getModulus());
     obj_stream.writeObject(RSAspec.getPublicExponent());
   } catch (Exception ex) {
     System.err.println("Probeleme de sauvegarde de la cle public dans un fichier: " + ex);
   }
 }
Esempio n. 3
0
 // internal implementation of generatePublic. See JCA doc
 private PublicKey generatePublic(KeySpec keySpec) throws GeneralSecurityException {
   if (keySpec instanceof X509EncodedKeySpec) {
     X509EncodedKeySpec x509Spec = (X509EncodedKeySpec) keySpec;
     return new RSAPublicKeyImpl(x509Spec.getEncoded());
   } else if (keySpec instanceof RSAPublicKeySpec) {
     RSAPublicKeySpec rsaSpec = (RSAPublicKeySpec) keySpec;
     return new RSAPublicKeyImpl(rsaSpec.getModulus(), rsaSpec.getPublicExponent());
   } else {
     throw new InvalidKeySpecException(
         "Only RSAPublicKeySpec " + "and X509EncodedKeySpec supported for RSA public keys");
   }
 }
Esempio n. 4
0
 OpenSSLRSAPublicKey(RSAPublicKeySpec spec) throws InvalidKeySpecException {
   try {
     key =
         new OpenSSLKey(
             NativeCrypto.EVP_PKEY_new_RSA(
                 spec.getModulus().toByteArray(),
                 spec.getPublicExponent().toByteArray(),
                 null,
                 null,
                 null,
                 null,
                 null,
                 null));
   } catch (Exception e) {
     throw new InvalidKeySpecException(e);
   }
 }
  public void run() {

    boolean loginSuccessfull = false;
    int loginAttempts = 0;

    try {

      KeyFactory fact = KeyFactory.getInstance("RSA");
      RSAPublicKeySpec pub = fact.getKeySpec(a.getPublicKey(), RSAPublicKeySpec.class);

      socketWrapper.write(pub.getModulus().toString());
      if (!socketWrapper.read().equals("got")) return;
      socketWrapper.write(pub.getPublicExponent().toString());

      RSAEncryption rsa = new RSAEncryption();
      String msg = rsa.encrypt("[ENCRYPTED HELLO]", a.getPrivateKey());
      socketWrapper.write(msg);

      String user = null, pass = null;

      socketWrapper.write(rsa.encrypt("login as: ", a.getPrivateKey()));
      user = rsa.decrypt(socketWrapper.read(), a.getPrivateKey());
      System.out.println("[FROM CLIENT] " + user);

      while (!loginSuccessfull && loginAttempts < 1) {

        if (loginAttempts > 0)
          socketWrapper.write(rsa.encrypt("login failed.\npassword: "******"password: "******"[FROM CLIENT] " + pass);

        if (accessManager.isPasswordCorrect(user, pass)) {
          loginSuccessfull = true;
          break;
        } else {
          loginAttempts++;
        }
      }

      if (!loginSuccessfull) {
        if (!user.equals("admin")) accessManager.lockUserAccount(user);

        socketWrapper.write(
            rsa.encrypt(
                "maximum login attempts exeeded. Account is locked now. Disconnect\n",
                a.getPrivateKey()));
        clientSocket.close();
        return;
      } else {
        if (!accessManager.isAccountUnlocked(user)) {
          socketWrapper.write(
              rsa.encrypt("account locked. Contact administrator\n", a.getPrivateKey()));
          clientSocket.close();
          return;
        }
      }

      socketWrapper.write(
          rsa.encrypt("logged in as \"" + user + "\" successfully\n", a.getPrivateKey()));

      while (true) {
        String userInput = rsa.decrypt(socketWrapper.read(), a.getPrivateKey());

        if (userInput.equals("quit")) {
          System.out.println("[GOT \"quit\" FROM USER]");
          socketWrapper.write(rsa.encrypt("[quit]", a.getPrivateKey()));
          break;
        }

        if (userInput.equals("ok")) {
          socketWrapper.write(rsa.encrypt("ok", a.getPrivateKey()));
          continue;
        }

        socketWrapper.write(
            rsa.encrypt(commandProcessor.processCommand(user, userInput), a.getPrivateKey()));
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 public BigInteger getModulus() {
   return spec.getModulus();
 }