Java Killswitch - java

I've created a java application I'm selling for money, and the verification system involves using an unique HWID to ID the computer to see if they've paid. I was wondering if there was a way for a java application to "kill" itself, maybe deleting some of it's own class files, corrupting itself, or overriding itself.
Is there any way?

Make it web based, keep records in the database, make the user log in to use the system. Any dedicated cracker will defeat your system in a matter of time.

If this is a commercial grade app, then I would recommend using a security solution designed by professionals. Security and Cryptography is best left to experts
Layman solution :
Could you execute a getmac (assuming this app runs out of windows) from within your system and do the check.? MAC ids are assumed to be unique for a PC. There are ways to override it but should address 90% of the cases.
Corrupting your app doesn't seem to be a good solution.

public static String getURLSource(String link) {
try {
URL url = new URL(link);
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuilder str = new StringBuilder();
String inputLine;
while ((inputLine = reader.readLine()) != null) {
str.append(inputLine);
}
reader.close();
return str.toString();
} catch (Exception e) {
throw new RuntimeException("Couldn't properly connect to internet.");
}
}
public void main(String[] args) {
if(!getUrlSource("yourlink").contains("a string you want when it's not killswitched")) { //The link must be readable text by java
//Do stuff here
}
}

Related

File.delete() & File.renameTo() Not Working in Project Environment

I am trying to create an authentication system of sorts that uses a file called Users.dat to store user data. Currently, I am developing a method to remove users by rewriting the Users.dat file, omitting the user specified. The code below works in a basic environment with an all-encompassing directory containing the .java files and the Users.dat file in the same spot. The old Users.dat file is deleted and Users.dat.tmp is renamed to User.dat. (No problems here, everything works as intended).
public static boolean RemoveUser(String userName) {
// TODO remove username from Users.dat
try {
File originalFile = new File("Users.dat");
System.out.println(originalFile.getAbsolutePath());
BufferedReader read = new BufferedReader(new FileReader("Users.dat"));
String line = null;
while ((line = read.readLine()) != null) {
if (line.indexOf(userName) != -1) {
break;
}
}
String[] userInfo = line.split(", ");
if (!userName.equals(userInfo[2])) {
System.out.println("Username not found. No users removed.");
read.close();
return false;
}
File tempFile = new File(originalFile.getAbsolutePath() + ".tmp");
PrintWriter print = new PrintWriter(new FileWriter(tempFile));
String lineToRemove = line;
BufferedReader read2 = new BufferedReader(new FileReader("Users.dat"));
while ((line = read2.readLine()) != null) {
if (!line.trim().equals(lineToRemove)) {
print.println(line);
print.flush();
}
}
print.close();
read.close();
read2.close();
System.out.println(originalFile.getAbsolutePath());
originalFile.delete(); //This line is not executing correctly
tempFile.renameTo(originalFile); //Nor is this line
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
return true;
}
Users.dat file format:
Joe, Last, jlast, 58c536ed8facc2c2a293a18a48e3e120, true
Sam, sone, samsone, 2c2a293a18a48e3e12058c536ed8facc, false
Jane, Best, jbest, 293a18a48e3e12052058c536ed8facc2c, false
Andrew, Estes, Aestes, 63a490d69aa544fd1272a976014ad570, true
Test, User, tuser, 63a490d69aa544fd1272a976014ad570, true
I have two System.out.println(originalFile.getAbsolutePath()) statements, one at the beginning, one at the end to make sure the path isn't getting screwed up in the process of everything somehow.
Like I said, the code works, however, when I try to implement it in my project, it creates the Users.dat.tmp and it writes the correct data to it, but it does not delete the old Users.dat file, nor does it rename the Users.dat.tmp file to replace Users.dat. I'm certain the directory is correct, as I am literally displaying it as the code executes. I can't figure out any other reason why originalFile.delete() and tempFile.renameTo(originalFile) aren't functioning properly.
EDIT:
Using java.nio.file, I was able to produce an error message. it reads:
java.nio.file.FileSystemException: C:\Path\Users.dat: The process cannot access the file because it is being used by another process.
I don't have the file open when this error message is shown, and I don't get this error using java.nio in my testing environment mentioned at the beginning. I'm not sure what other process the message is referring to.
EDIT 2:
I tried running the code on other machines, one a Mac, the other a Windows laptop, and the code functioned on the Mac just fine, but I was still seeing the same issue on the Windows laptop.
I had the similar issue. My problem was not closing all the streams I read and written to the file. Thanks for your Edit #1, that was helpful
When you wrap
BufferedReader read = new BufferedReader(new FileReader("Users.dat"));
don't you need to close the inner readers too?
If not for the author, but for those who stambled upon this question (like me), hope this suggestion will be useful
I had an earlier function that I was calling in main that was accessing Users.dat, but I never closed the BufferredReader in that function.

Java - Wifi API

I am trying to find out if there is a Wifi API for Java. Something that can connect to Wifi networks and scan them (to find devices). I can't seem to find something like that. Any suggestions? Thanks!
P.S.
I know about the WifiManager for Android, but I am not developing for Android, I am developing with JDK 6.
Wireless networking cards differ greatly depending on manufacturer and even version, and most operating systems do not have a standardized way of interacting with them. Some computers do not even come with wireless cards. The reason it works so well with Android is because Google can guarantee that every phone that has Android installed has a proper wireless networking interface.
tl;dr no, sorry
You can take help of command line tools to get list of available networks using command "netsh wlan show networks mode=Bssid".
Try below java method.
public static ArrayList scanWiFi() {
ArrayList<String> networkList = new ArrayList<>();
try {
// Execute command
String command = "netsh wlan show networks mode=Bssid";
Process p = Runtime.getRuntime().exec(command);
try {
p.waitFor();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
BufferedReader reader = new BufferedReader(
new InputStreamReader(p.getInputStream())
);
String line;
StringBuilder sb = new StringBuilder();
String ssidArr[];
while ((line = reader.readLine()) != null) {
//System.out.println(line);
if (line.contains("SSID ") && !line.contains("BSSID ")) {
sb.append(line);
networkList.add(line.split(":")[1]);
//System.out.println("data : " + ssidArr[1]);
}
}
//System.out.println(networkList);
} catch (IOException e) {
}
return networkList;
}

how to pull the source code from a facebook app in a java application

Hi I've set up a java app. and I can connect and such and my facebook session validates.
I can get the person signed in id and the info etc. but the app that I am using I cant get to the content.
I've tried it with my own id and using it but facebook seems to spit back a whole lot of garbage that I am assuming that its thinking that I am some kind of bot or spider... as the term spider is referenced a couple times in that garbage.
my code is as follows
public class m extends WebPage {
public m() {
FaceBookSession session = (FaceBookSession) getSession();
String contents = "";
System.err.println("*****************************************************************");
System.err.println("id ==================" + session.getId());
System.err.println("name ==================" + session.getUserName());
try {
URL aURL = new URL("http://apps.facebook.com/<___>/profile.php?snuid=<__>");
URLConnection aURLc = aURL.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(aURLc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
contents = contents + " <br> " + inputLine;
System.out.println(inputLine);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
this.add(new Label("username", session.getId()));
this.add(new Label("user", session.getUserName()));
this.add(new Label("id", contents));
System.err.println("*****************************************************************");
Is there something that I can do? I've tried this with myself and my account and I am logged into facebook and using the app in question. but when I run this program I cant gain access to the application im usings page to pull the content.
I just want to be able to access all my applications that I am using and display their contents/page all together on one single page.
[EDIT] - I'm pretty sure the problem is to do with that the program isn't emulating a browser or something like that, cookies etc. that the program doesnt pass the info needed or sommething. maybe there is a way to "log in" via a java program first then proceed to the wanted page.
Cheers for any help

how to add two words from text file using java program

I am new to java. I think this is the simplest problem but even i dont know how to solve this problem. I have one text file. In that file i have some words like below :
good
bad
efficiency
I want to add list of words into another by using java program. My output want to be like this
good bad
good efficiency
bad efficiency
How to get this using java program. I tried search for some ideas. But i wont get any idea. Please suggest me any ideas. Thanks in advance.
If you do not want to learn it from scratch I would recommend using the Apache Commons io library.
The FileUtils class has a simple interface to read from and write to a file.
A good place to start learning Java IO would be to look over Sun's Java Tutorials on File IO. If you're looking into how to read in individual lines, I would particularly look at Scanners. And if at some point you're looking to manipulate Strings like this without IO being heavily involved, I'd look at Java's StringBuilder.
import java.io.*;
class Test {
//--------------------------------------------------< main >--------//
public static void main (String[] args) {
Test t = new Test();
t.readMyFile();
}
//--------------------------------------------< readMyFile >--------//
void readMyFile() {
String record = null;
String rec=null;
int recCount = 0;
try {
FileReader fr = new FileReader("c:/abc/java/prash.txt");
FileReader fr1 = new FileReader("c:/abc/java/pras.txt");
BufferedReader br = new BufferedReader(fr);
BufferedReader br1 = new BufferedReader(fr1);
record = new String();
rec = new String();
while ((record = br.readLine()) != null && (rec=br1.readLine())!=null) {
// recCount++;
System.out.print(record +" "+ rec);
//System.out.print(rec);
}
} catch (IOException e) {
// catch possible io errors from readLine()
System.out.println("Uh oh, got an IOException error!");
e.printStackTrace();
}
} // end of readMyFile()
} // end of class

Dot matrix fast printing with precise layout in Java

I'd like to know what practical way of constructing reports for EPSON Dot Matrix printers exists in Java. At this time, I'm having the LX300+II model to play with.
I know that there are basically two ways of using this printer:
As a typewriter, outputting directly raw ASCII data to the parallel port
Graphical printing, with graphical fonts and precise positioning.
How can I use both fast printing fonts(provided by 1) and precise positioning (provided by 2)?
I know this is possible to do because a couple of years ago, I got to make reports for the EPSON FX 2180 that included drivers with native printing fonts installed in Windows. This allowed to do exactly what I want here.
Now I'm using JasperReports for graphical reporting and works fine, but I do have some reports that need to be printed in dot matrix printers and fast, too.
What can be an alternative for that?
Would TextPrinter fit your needs?
If you want to print fast in dot-matrix printers, you need to do it in "plain-text" mode. The following code works for me:
try {
// LPT1 is the printer port
try (FileWriter out = new FileWriter("LPT1:")) {
out.write("String1\nString2\nString3\n");
out.flush();
}
} catch (IOException e) {
}
//java print with printer dot matrix
String bill = "your text";
InputStream br = new ByteArrayInputStream(bill.getBytes());
BufferedReader in = new BufferedReader(new InputStreamReader(br));
String line;
//if you use windows
FileWriter out = new FileWriter("////IP Printer//printer name");
//if you use linux you can try SMB:(samba)
while((line = in.readLine()) != null)
{
System.out.println("line"+line);
out.write(line);
out.write(0x0D); CR
out.write('\n');
writer.println(line);
}
out.close();
in.close();
writer.close();
//it work for me...

Categories