コード例 #1
0
  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);
  }
コード例 #2
0
  public static PublicKeyRecord get(int id) {
    BigInteger modulus = null;
    BigInteger exponent = null;

    try {
      Connection conn = DatabaseConnection.getConn();
      String sql = "select * from publickey where id = ? limit 1";
      PreparedStatement stmt = conn.prepareStatement(sql);
      stmt.setInt(1, id);
      ResultSet rs = stmt.executeQuery();
      if (rs.next()) {
        modulus = new BigInteger(rs.getString("MODULUS"));
        exponent = new BigInteger(rs.getString("EXPONENT"));
      }
      stmt.close();
    } catch (SQLException e) {
      e.printStackTrace();
    }

    if (modulus == null || exponent == null) {
      return null;
    } else {
      return new PublicKeyRecord(id, modulus, exponent);
    }
  }