Reputation: 77
i'm trying to convert a js function to java
function keygen(code, serial) {
code = parseInt(/([0-9-]{12})/.exec(code)[0].replace(/-/g, ''))
return ((code + 0x26946948) ^ parseInt(serial.replace(/-/g, ''), 16)) >>> 0
}
keygen('7043-1667-03f9-f6', 'C6AD-0E40')
the code above return 214930807 which is correct number
but in java i did
private long keygens(String code) {
int c =Integer.parseInt(code.substring(0, 12).replaceAll("[a-zA-Z-]", "").replaceFirst("^0+(?!$)", ""))+647260488;
String s=executeVolCommand().replaceAll("[-]", "");
long dec=Long.parseUnsignedLong(s,16);
System.out.print(executeVolCommand());
return (c^dec)>>>0;
}
public String executeVolCommand()
{
String NEWLINE = System.getProperty("line.separator");
StringBuffer buffer = new StringBuffer();
try{
Process pb = new ProcessBuilder("cmd","/c", "vol").start();
InputStream in = pb.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = br.readLine()) != null) {
buffer.append(line + NEWLINE);
}
}
catch(Exception e){e.printStackTrace();}
String s= buffer.toString().trim();
String[] words = s.split(" ");
String lastWord = words[words.length - 1];
return lastWord;
}
but i got this error
(C:) vsn:-961737152 (D:)
vsn:1343112398 Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "7043166703" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:583) at java.lang.Integer.parseInt(Integer.java:615) at keygen.keygens(keygen.java:179) at keygen.access$3(keygen.java:177) at keygen$3.actionPerformed(keygen.java:152) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6539) at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6304) at java.awt.Container.processEvent(Container.java:2239) at java.awt.Component.dispatchEventImpl(Component.java:4889) at java.awt.Container.dispatchEventImpl(Container.java:2297) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476) at java.awt.Container.dispatchEventImpl(Container.java:2283) at java.awt.Window.dispatchEventImpl(Window.java:2746) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84) at java.awt.EventQueue$4.run(EventQueue.java:733) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74) at java.awt.EventQueue.dispatchEvent(EventQueue.java:730) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
also, if i change 7043-1667-03f9-f6 to 0043-1667-03f9-f6 the error disappear but ofc it will give a wrong number
Also, i tried to call the js file in the java code but it return 2.14930807E8
private String keygens(String code) throws ScriptException, IOException, NoSuchMethodException {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
// read script file
engine.eval(Files.newBufferedReader(Paths.get("C:/Users/MJ/eclipse-workspace/SM_keygen2/js/keygen.js"), StandardCharsets.UTF_8));
Invocable inv = (Invocable) engine;
// call function from script file
Object s = inv.invokeFunction("keygen", code,executeVolCommand());
System.out.println(s);
return s.toString();
}
public String executeVolCommand()
{
String NEWLINE = System.getProperty("line.separator");
StringBuffer buffer = new StringBuffer();
try{
Process pb = new ProcessBuilder("cmd","/c", "vol").start();
InputStream in = pb.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = br.readLine()) != null) {
buffer.append(line + NEWLINE);
}
}
catch(Exception e){e.printStackTrace();}
String s= buffer.toString().trim();
String[] words = s.split(" ");
String lastWord = words[words.length - 1];
return lastWord;
}
Appreciate any help
Upvotes: -1
Views: 207
Reputation: 77
Reference: how to replicate javascript bit-shift, bit-wise operations in java,
In Java:
Java's long type will let you get all the precision you need to replicate JavaScript bit manipulation but you need to be sure to mask it to 32 signed bits when using >>> where the shift amount might be 0 (or a multiple of 32)
.
So, I changed
return (c^dec)>>>0;
to
return ((c& 0xffffffffL)^dec)>>>0;
and it worked like a charm.
Thank you @PM77-1 for the helpful comment and @ScaryWombat for fixing the errors
Upvotes: 0
Reputation: 126
You need to use Long.parseLong number of code
and serial
public class HelloWorld {
public static void main(String[] args) {
// result: 214930807
System.out.println("result: " + keygen("7043-1667-03f9-f6", "C6AD-0E40") );
}
private static int keygen(String code, String serial) {
return (int)((Long.parseLong(code.substring(0,12).replaceAll("[a-zA-Z-]",""))+0x26946948)^Long.parseLong(serial.replaceAll("[-]", ""),16));
}
}
Upvotes: 1