Page 1 of 3 123 LastLast
Results 1 to 20 of 51
  1. #1
    funstein is offline Member
    Join Date
    Aug 2011
    Posts
    24
    Rep Power
    0

    Default Encryption Problem, Can someone do this for me?

    Hello everyone,
    So I have en encryption application in PHP that returns an encrypted string. What I want is a java application which can decrypt the string. The PHP encryption method I use is below but I have failed to convert it to java, so I'd appreciate if someone could help me.


    PHP Code:
    function decrypt($sData, $sKey='payment'){ 
        $sResult = ''; 
        $sData   = decode_base64($sData); 
        for($i=0;$i<strlen($sData);$i++){ 
            $sChar    = substr($sData, $i, 1); 
            $sKeyChar = substr($sKey, ($i % strlen($sKey)) - 1, 1); 
            $sChar    = chr(ord($sChar) - ord($sKeyChar)); 
            $sResult .= $sChar; 
        } 
        return $sResult; 
    }
    Last edited by funstein; 08-17-2011 at 11:45 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I have failed to convert it to Java
    What have you come up with so far? I'm sure you realise that no-one here is going to write your code, though many are happy to help you do so.

    First, though, what does that php function do? Describing the function is the first step to understanding it. And that understanding is necessary if you are to implement it (or its inverse) in Java.

  3. #3
    funstein is offline Member
    Join Date
    Aug 2011
    Posts
    24
    Rep Power
    0

    Default Progress.

    OK, so here is my progress. But it still doesnt work, can someone cross check it with the PHP?

    Java Code:
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
            String gottext = Anahtarfield.getText();
            gottext.replaceAll("-_","+/") ;
            String anahtar = gottext;
            String key = "payment" ;
            String sResult = "";
                for(int i = 0; i < anahtar.length(); i++) {
                    String sChar = anahtar.substring(i,(i+1)) ;
                    String sKeyChar = key.substring((i % key.length()),((i % key.length())+1));
                    char cKeyChar = sKeyChar.charAt(1);
                    char cChar = sChar.charAt(1) ;
                    int diff = Character.getNumericValue(cChar) - Character.getNumericValue(cKeyChar) ;
                    char cSonChar = (char)diff ;
                    String cConcat = Character.toString(cSonChar) ;
                    sResult = sResult + cConcat ;
                    
                }
            sResult = sResult.replaceAll("-_", "+/");
    
            String[] temp ;
            temp = sResult.split("/");
            orderid.setText(temp[0]);
            var1.setText(temp[1]);
            var2.setText(temp[2]);
            var3.setText(temp[3]);
            var4.setText(temp[4]);
        }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    it still doesnt work,
    Can you explain?
    Can you show the input to your program and the output from it and also show the output from the PHP program.

    Rewrite your code so it is in a method that works like the PHP function: 2 Strings input and returns encrypted String.

  5. #5
    funstein is offline Member
    Join Date
    Aug 2011
    Posts
    24
    Rep Power
    0

    Default Alright, method is here.

    Java Code:
        public static String decrypt(String anahtar, String key) {     
            anahtar = anahtar.replaceAll("-_","+/") ;
            String sResult = "";
                for(int i = 0; i < anahtar.length(); i++) {
                    String sChar = anahtar.substring(i,(i+1)) ;
                    String sKeyChar = key.substring((i % key.length()),((i % key.length())+1));
                    char cKeyChar = sKeyChar.charAt(1);
                    char cChar = sChar.charAt(1) ;
                    int diff = Character.getNumericValue(cChar) - Character.getNumericValue(cKeyChar) ;
                    char cSonChar = (char)diff ;
                    String cConcat = Character.toString(cSonChar) ;
                    sResult = sResult + cConcat ;
                }
            return sResult ;
        }
    So here is what happens in PHP :

    PHP Code:
    function decrypt($sData, $sKey='funstein'){ 
        $sResult = ''; 
        $sData   = decode_base64($sData); 
        for($i=0;$i<strlen($sData);$i++){ 
            $sChar    = substr($sData, $i, 1); 
            $sKeyChar = substr($sKey, ($i % strlen($sKey)) - 1, 1); 
            $sChar    = chr(ord($sChar) - ord($sKeyChar)); 
            $sResult .= $sChar; 
        } 
    
        return $sResult; 
    } 
    
    function decode_base64($sData){ 
        $sBase64 = strtr($sData, '-_', '+/'); 
        return base64_decode($sBase64);
    But it throws me the exception :

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
    	at java.lang.String.charAt(String.java:695)
    	at funstein.desifre.DesifreApp.decrypt(DesifreApp.java:51)
    	at funstein.desifre.DesifreView.jButton1ActionPerformed(DesifreView.java:316)
    	at funstein.desifre.DesifreView.access$800(DesifreView.java:22)
    	at funstein.desifre.DesifreView$4.actionPerformed(DesifreView.java:152)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    	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:6504)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    	at java.awt.Component.processEvent(Component.java:6269)
    	at java.awt.Container.processEvent(Container.java:2229)
    	at java.awt.Component.dispatchEventImpl(Component.java:4860)
    	at java.awt.Container.dispatchEventImpl(Container.java:2287)
    	at java.awt.Component.dispatchEvent(Component.java:4686)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    	at java.awt.Container.dispatchEventImpl(Container.java:2273)
    	at java.awt.Window.dispatchEventImpl(Window.java:2713)
    	at java.awt.Component.dispatchEvent(Component.java:4686)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    	at java.awt.EventQueue.access$000(EventQueue.java:101)
    	at java.awt.EventQueue$3.run(EventQueue.java:666)
    	at java.awt.EventQueue$3.run(EventQueue.java:664)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    	at java.awt.EventQueue$4.run(EventQueue.java:680)
    	at java.awt.EventQueue$4.run(EventQueue.java:678)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
    Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
    	at java.lang.String.charAt(String.java:695)
    	at funstein.desifre.DesifreApp.decrypt(DesifreApp.java:51)
    	at funstein.desifre.DesifreView.jButton1ActionPerformed(DesifreView.java:316)
    	at funstein.desifre.DesifreView.access$800(DesifreView.java:22)
    	at funstein.desifre.DesifreView$4.actionPerformed(DesifreView.java:152)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    	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:6504)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    	at java.awt.Component.processEvent(Component.java:6269)
    	at java.awt.Container.processEvent(Container.java:2229)
    	at java.awt.Component.dispatchEventImpl(Component.java:4860)
    	at java.awt.Container.dispatchEventImpl(Container.java:2287)
    	at java.awt.Component.dispatchEvent(Component.java:4686)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    	at java.awt.Container.dispatchEventImpl(Container.java:2273)
    	at java.awt.Window.dispatchEventImpl(Window.java:2713)
    	at java.awt.Component.dispatchEvent(Component.java:4686)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    	at java.awt.EventQueue.access$000(EventQueue.java:101)
    	at java.awt.EventQueue$3.run(EventQueue.java:666)
    	at java.awt.EventQueue$3.run(EventQueue.java:664)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    	at java.awt.EventQueue$4.run(EventQueue.java:680)
    	at java.awt.EventQueue$4.run(EventQueue.java:678)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
    Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
    	at java.lang.String.charAt(String.java:695)
    	at funstein.desifre.DesifreApp.decrypt(DesifreApp.java:51)
    	at funstein.desifre.DesifreView.jButton1ActionPerformed(DesifreView.java:316)
    	at funstein.desifre.DesifreView.access$800(DesifreView.java:22)
    	at funstein.desifre.DesifreView$4.actionPerformed(DesifreView.java:152)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    	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:6504)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    	at java.awt.Component.processEvent(Component.java:6269)
    	at java.awt.Container.processEvent(Container.java:2229)
    	at java.awt.Component.dispatchEventImpl(Component.java:4860)
    	at java.awt.Container.dispatchEventImpl(Container.java:2287)
    	at java.awt.Component.dispatchEvent(Component.java:4686)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    	at java.awt.Container.dispatchEventImpl(Container.java:2273)
    	at java.awt.Window.dispatchEventImpl(Window.java:2713)
    	at java.awt.Component.dispatchEvent(Component.java:4686)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    	at java.awt.EventQueue.access$000(EventQueue.java:101)
    	at java.awt.EventQueue$3.run(EventQueue.java:666)
    	at java.awt.EventQueue$3.run(EventQueue.java:664)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    	at java.awt.EventQueue$4.run(EventQueue.java:680)
    	at java.awt.EventQueue$4.run(EventQueue.java:678)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
    Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
    	at java.lang.String.charAt(String.java:695)
    	at funstein.desifre.DesifreApp.decrypt(DesifreApp.java:51)
    	at funstein.desifre.DesifreView.jButton1ActionPerformed(DesifreView.java:316)
    	at funstein.desifre.DesifreView.access$800(DesifreView.java:22)
    	at funstein.desifre.DesifreView$4.actionPerformed(DesifreView.java:152)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    	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:6504)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    	at java.awt.Component.processEvent(Component.java:6269)
    	at java.awt.Container.processEvent(Container.java:2229)
    	at java.awt.Component.dispatchEventImpl(Component.java:4860)
    	at java.awt.Container.dispatchEventImpl(Container.java:2287)
    	at java.awt.Component.dispatchEvent(Component.java:4686)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    	at java.awt.Container.dispatchEventImpl(Container.java:2273)
    	at java.awt.Window.dispatchEventImpl(Window.java:2713)
    	at java.awt.Component.dispatchEvent(Component.java:4686)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    	at java.awt.EventQueue.access$000(EventQueue.java:101)
    	at java.awt.EventQueue$3.run(EventQueue.java:666)
    	at java.awt.EventQueue$3.run(EventQueue.java:664)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    	at java.awt.EventQueue$4.run(EventQueue.java:680)
    	at java.awt.EventQueue$4.run(EventQueue.java:678)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
    Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
    	at java.lang.String.charAt(String.java:695)
    	at funstein.desifre.DesifreApp.decrypt(DesifreApp.java:51)
    	at funstein.desifre.DesifreView.jButton1ActionPerformed(DesifreView.java:316)
    	at funstein.desifre.DesifreView.access$800(DesifreView.java:22)
    	at funstein.desifre.DesifreView$4.actionPerformed(DesifreView.java:152)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    	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:6504)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    	at java.awt.Component.processEvent(Component.java:6269)
    	at java.awt.Container.processEvent(Container.java:2229)
    	at java.awt.Component.dispatchEventImpl(Component.java:4860)
    	at java.awt.Container.dispatchEventImpl(Container.java:2287)
    	at java.awt.Component.dispatchEvent(Component.java:4686)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    	at java.awt.Container.dispatchEventImpl(Container.java:2273)
    	at java.awt.Window.dispatchEventImpl(Window.java:2713)
    	at java.awt.Component.dispatchEvent(Component.java:4686)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    	at java.awt.EventQueue.access$000(EventQueue.java:101)
    	at java.awt.EventQueue$3.run(EventQueue.java:666)
    	at java.awt.EventQueue$3.run(EventQueue.java:664)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    	at java.awt.EventQueue$4.run(EventQueue.java:680)
    	at java.awt.EventQueue$4.run(EventQueue.java:678)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
    Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
    	at java.lang.String.charAt(String.java:695)
    	at funstein.desifre.DesifreApp.decrypt(DesifreApp.java:51)
    	at funstein.desifre.DesifreView.jButton1ActionPerformed(DesifreView.java:316)
    	at funstein.desifre.DesifreView.access$800(DesifreView.java:22)
    	at funstein.desifre.DesifreView$4.actionPerformed(DesifreView.java:152)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    	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:6504)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    	at java.awt.Component.processEvent(Component.java:6269)
    	at java.awt.Container.processEvent(Container.java:2229)
    	at java.awt.Component.dispatchEventImpl(Component.java:4860)
    	at java.awt.Container.dispatchEventImpl(Container.java:2287)
    	at java.awt.Component.dispatchEvent(Component.java:4686)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    	at java.awt.Container.dispatchEventImpl(Container.java:2273)
    	at java.awt.Window.dispatchEventImpl(Window.java:2713)
    	at java.awt.Component.dispatchEvent(Component.java:4686)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    	at java.awt.EventQueue.access$000(EventQueue.java:101)
    	at java.awt.EventQueue$3.run(EventQueue.java:666)
    	at java.awt.EventQueue$3.run(EventQueue.java:664)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    	at java.awt.EventQueue$4.run(EventQueue.java:680)
    	at java.awt.EventQueue$4.run(EventQueue.java:678)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
    Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
    	at java.lang.String.charAt(String.java:695)
    	at funstein.desifre.DesifreApp.decrypt(DesifreApp.java:51)
    	at funstein.desifre.DesifreView.jButton1ActionPerformed(DesifreView.java:316)
    	at funstein.desifre.DesifreView.access$800(DesifreView.java:22)
    	at funstein.desifre.DesifreView$4.actionPerformed(DesifreView.java:152)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    	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:6504)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    	at java.awt.Component.processEvent(Component.java:6269)
    	at java.awt.Container.processEvent(Container.java:2229)
    	at java.awt.Component.dispatchEventImpl(Component.java:4860)
    	at java.awt.Container.dispatchEventImpl(Container.java:2287)
    	at java.awt.Component.dispatchEvent(Component.java:4686)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    	at java.awt.Container.dispatchEventImpl(Container.java:2273)
    	at java.awt.Window.dispatchEventImpl(Window.java:2713)
    	at java.awt.Component.dispatchEvent(Component.java:4686)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    	at java.awt.EventQueue.access$000(EventQueue.java:101)
    	at java.awt.EventQueue$3.run(EventQueue.java:666)
    	at java.awt.EventQueue$3.run(EventQueue.java:664)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    	at java.awt.EventQueue$4.run(EventQueue.java:680)
    	at java.awt.EventQueue$4.run(EventQueue.java:678)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    String index out of range: 1
    ..
    at funstein.desifre.DesifreApp.decrypt(DesifreApp.jav a:51)
    Look at line 51 in your program. The index on that line is past the length of the String.
    You need to keep the index from going past the end of the String. Get the String's length to test against.

    Add some printlns to the code to show the String that is being processed and the values of the variables that the code is using.

  7. #7
    funstein is offline Member
    Join Date
    Aug 2011
    Posts
    24
    Rep Power
    0

    Default

    I am sharing the full php code including what it returns (in the end of the code) :

    PHP Code:
    <?php
    function encrypt($sData, $sKey='funstein'){ 
        $sResult = ''; 
        for($i=0;$i<strlen($sData);$i++){ 
            $sChar    = substr($sData, $i, 1); 
            $sKeyChar = substr($sKey, ($i % strlen($sKey)) - 1, 1); 
            $sChar    = chr(ord($sChar) + ord($sKeyChar)); 
            $sResult .= $sChar; 
        } 
        return encode_base64($sResult); 
    } 
    
    
    function encode_base64($sData){ 
        $sBase64 = base64_encode($sData); 
        return strtr($sBase64, '+/', '-_'); 
    } 
    
    function decrypt($sData, $sKey='funstein'){ 
        $sResult = ''; 
        $sData   = decode_base64($sData); 
        for($i=0;$i<strlen($sData);$i++){ 
            $sChar    = substr($sData, $i, 1); 
            $sKeyChar = substr($sKey, ($i % strlen($sKey)) - 1, 1); 
            $sChar    = chr(ord($sChar) - ord($sKeyChar)); 
            $sResult .= $sChar; 
        } 
        return $sResult; 
    } 
    
    function decode_base64($sData){ 
        $sBase64 = strtr($sData, '-_', '+/'); 
        return base64_decode($sBase64); 
    
    }
    
    $code = "foo" ;
    
    // Encrypts foo using the key funstein.
    $enc = encrypt($code, "funstein") ;
    
    //This will echo the encrypted version : 1NXk
    echo $enc ;
    echo "\r\n" ;
    
    
    // Decrypts the encrypted version 1NXk.
    $dec = decrypt($enc, "funstein");
    
    // What a coincidence! It echoes foo again.
    echo $dec ; 
    ?>

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Sorry, the PHP code doesn't mean anything to me. When I read the comments in the PHP code to see what algorithm it is using I'm unable to understand what it is saying. Perhaps if you could get the author to document the algorithm better, someone could help you.

    Did you look at your java code to see why the index is past the end of the String?

  9. #9
    funstein is offline Member
    Join Date
    Aug 2011
    Posts
    24
    Rep Power
    0

    Default

    btw, line 51 is char cKeyChar = sKeyChar.charAt(1); which it cant be obviously, because its not empty.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Print out the String sKeyChar and see its contents. Remember array indexes start at 0.

  11. #11
    funstein is offline Member
    Join Date
    Aug 2011
    Posts
    24
    Rep Power
    0

    Default

    Alright, I just found out Java's substring function can not function as php's, so this method is unusable. Can you recommend me an encryption method that I can easily encrypt $data using a $key, and decrypt in Alright, I just found out Java's substring function can not function as php's, so this method is unusable. Can you recommend me an encryption method that I can easily encrypt $data using a $key in php, and decrypt in Java using the encrypted code and the $key??

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Java's substring function can not function as php's,
    Please explain what you want to do in java. I'm sure the substring method can be made to do what you want.
    It is just a question of computing the correct arguments.

  13. #13
    funstein is offline Member
    Join Date
    Aug 2011
    Posts
    24
    Rep Power
    0

    Default

    no, I know that it is impossible. Can you just recommend me a way of crypting a data, moving it to another place and decrypting it there?

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    I know that it is impossible
    Can you explain why it is impossible?

    Can you explain the algorithm that the PHP code is using to do the encryption?

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    sKeyChar is a single character.
    It's a single character in that PHP code as well.
    So referencing sKeyChar.charAt(1) (which isn't done in that PHP code as far as I can tell) will always throw that exception.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    The String is one character long. There is a character at index 0. There is NO character at index 1.

    See the end of post#10

  17. #17
    funstein is offline Member
    Join Date
    Aug 2011
    Posts
    24
    Rep Power
    0

    Default ...

    I have fixed that to 0, already. The problem is, there is a math operation in one of the lines, "i % key.length()". With the value of 0, there happens to be a -1 in that line which is invalid for substr in Java, but not in PHP. In PHP, negative substr values pull characters in backwards direction.

    Can someone please recommend me an easy way of encryption / decryption that can be implemented in both Java and PHP? It would help a lot as I am seriously out of time.

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    What does your corrected code output when you execute it?

    If you would define the algorithm that the PHP is using, it could be written in Java.

    Can you run a test with the PHP program, have it print the input and the output

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Here's code that will go thru the letters of the key and the input:
    Java Code:
            String anahtar = "The input";
            String key = "key";
    
            for(int i = 0; i < anahtar.length(); i++) {
                 char cChar = anahtar.charAt(i);
                 char cKeyChar = key.charAt(i % key.length());
                 System.out.println(cKeyChar  + " NV=" + Character.getNumericValue(cKeyChar)
                                   + ", " + cChar + " NV=" + Character.getNumericValue(cChar));
            }

  20. #20
    funstein is offline Member
    Join Date
    Aug 2011
    Posts
    24
    Rep Power
    0

    Default Nearly done.

    Alright, so I am nearly done. I have the Java application running, only got one little problem. The encrypted string consists of ASCII characters. But after decryption, Java interprets them as Unicode chars, leaving me with a bunch of Chinese letters.

    Hello world.

    will return

    マラネマバᄀロリラ

    Can someone please tell me what is wrong? Really need this in 2 hours.

Page 1 of 3 123 LastLast

Similar Threads

  1. problem with encryption in steganography
    By stegano in forum New To Java
    Replies: 2
    Last Post: 02-26-2011, 01:10 PM
  2. Java encryption problem
    By lqu in forum Advanced Java
    Replies: 0
    Last Post: 01-24-2011, 04:30 AM
  3. OAEP Padding Problem by encryption
    By Buhmann in forum Advanced Java
    Replies: 1
    Last Post: 09-07-2009, 01:19 PM
  4. WebService encryption problem
    By falnik in forum Advanced Java
    Replies: 3
    Last Post: 07-26-2009, 04:21 AM
  5. Encryption-Decryption Problem
    By dinesh.1.sharma in forum Advanced Java
    Replies: 1
    Last Post: 07-17-2008, 10:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •