IT干货网

javascript函数转java

lxf 2024年02月25日 编程设计 455 0

我正在尝试将 js 函数转换为 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')

上面的代码返回 214930807,这是正确的数字

但是在java中我做到了

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; 
 
 
      } 

但是我收到了这个错误

(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)

另外,如果我将 7043-1667-03f9-f6 更改为 0043-1667-03f9-f6 错误消失,但 ofc 会给出错误的号码

<小时 />

另外,我尝试在java代码中调用js文件,但它返回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; 
 
 
      } 

感谢任何帮助

请您参考如下方法:

您需要使用Long.parseLongcodeserial

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)); 
    } 
} 

run code


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!