Results 1 to 8 of 8
  1. #1
    Randa is offline Member
    Join Date
    Sep 2008
    Posts
    9
    Rep Power
    0

    Default how to store the previous of a variable

    Hi ,

    I had a problem with XOR before , and it was resolved thx to Norm
    i cannot post a thread from there , so... thx

    now... how can i store a previous value of a variable

    ...
    smessage=new byte[messagesize];
    System.arraycopy(entete2,0,smessage,0,entete2.leng th);
    System.arraycopy(HE1,0,smessage,entete2.length,HE1 .length);
    System.arraycopy(HE2,0,smessage,(entete2.length+HE 1.length),HE2.length);
    System.arraycopy(cipherRnd,0,smessage,(entete2.len gth+HE1.length+HE2.length),cipherRnd.length);

    //if the previous packet exists , do xor , else it's the first
    if (PrevPkt.getpktclient() != null)
    {
    //define returned var.
    byte[] a = PrevPkt.getpktclient() ;
    int len = (smessage.length > a.length) ? smessage.length : a.length ;
    byte[] xorResult = new byte[len];
    XorB myXor = new XorB();
    xorResult = myXor.xorArray(smessage, a , PrevPkt.getpktclient().length );
    }
    ////save packet for future use
    PrevPkt p1 = new PrevPkt(smessage , 1) ;

    i need to save smessage, so i can use it the next time this method is called
    here , i tried storing it in a static variable , it doesn't work

    help .

    thx

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    An instance variable?

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default

    i need to save smessage, so i can use it the next time this method is called here , i tried storing it in a static variable , it doesn't work
    What does "didn't work" mean?
    How/where do you save the old value?

    PrevPkt p1 = new PrevPkt(smessage , 1) ;
    This creates a local variable that will disappear when the method exits.

  4. #4
    Randa is offline Member
    Join Date
    Sep 2008
    Posts
    9
    Rep Power
    0

    Default

    Hi ,

    PrevPkt is a class with setters and getters to hold/retreive the previous value .
    the problem is when the method is called , the value of smessage changes
    and also the value in PrevPkt , before it's even called
    i was using static declarations in the following code then i chaged it :

    public class PrevPkt
    {
    private byte[] pkt ;
    private byte[] seq ;

    public void setpkt(byte [] mess)
    {
    this.pkt = mess;
    }

    public byte[] getpkt()
    {
    return pkt ;
    }

    public void setseq(byte[] seq)
    {
    this.seq = seq;
    }

    public byte[] getseq()
    {
    return seq ;
    }


    is there a way to save the previous value of smessage , in order to reuse it
    when the method is called ?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default

    I don't see smessage in the code you just posted. What/where are you talking about?
    "when the method is called" - which method is "the method"?

  6. #6
    Randa is offline Member
    Join Date
    Sep 2008
    Posts
    9
    Rep Power
    0

    Default

    this is the method am working on , i've added remarks to point to my problem

    import java.io.*;
    import java.security.*;
    import java.security.cert.*;
    import java.util.*;
    import java.util.zip.*;
    import javax.crypto.*;
    import java.math.*;
    import java.lang.*;
    import java.lang.reflect.*;

    public class SendU_req
    {
    public byte[] buildMessage(String cername,String key ,byte[] rnd,byte[] SID,byte[] sequenceNumber) throws Exception
    {
    byte firstByte = 82;
    Date start=new Date();
    Long l=start.getTime();
    Short sh =l.shortValue();
    byte timestamplow=sh.byteValue();
    l=l.rotateRight(l,8);
    sh=l.shortValue();
    byte timestamphigh=sh.byteValue();
    byte exthigh=0;
    byte extlow1=8;
    FileInputStream fis=new FileInputStream(cername);
    CertificateFactory cf=CertificateFactory.getInstance("X.509");
    Collection c=cf.generateCertificates(fis);
    Iterator i=c.iterator();
    java.security.cert.Certificate cert = (java.security.cert.Certificate)i.next();
    byte[] certstream=cert.getEncoded();
    //System.out.println("cert"+certstream.length);
    int HEdatalength=certstream.length;
    HEdatalength=HEdatalength+4;
    Integer HEdatalen=HEdatalength;
    byte HEdatalow=HEdatalen.byteValue();
    HEdatalen=HEdatalen.rotateRight(HEdatalen,8);
    byte HEdatahigh=HEdatalen.byteValue();
    byte extlow2=16;
    byte[] usercap="AES;HMACwithSHA1;DiffieHellman".getBytes( );
    //System.out.println("usercap"+usercap.length);
    int HEdatalength2=usercap.length;
    HEdatalength2=HEdatalength2+4;
    Integer HEdatalen2=HEdatalength2;
    byte HEdatalow2=HEdatalen2.byteValue();
    HEdatalen2=HEdatalen2.rotateRight(HEdatalen2,8);
    byte HEdatahigh2=HEdatalen2.byteValue();

    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    char[] password ={'p','a','s','s','w','o','r','d'};
    java.io.FileInputStream fis1 =
    new java.io.FileInputStream(".keystore");
    ks.load(fis1, password);
    fis.close();
    fis1.close();
    // get my private key
    char[] keypassword ={'1','2','3','4','5','6'};
    PrivateKey myPrivateKey = (PrivateKey)ks.getKey(key, keypassword);
    Cipher rsaCipher = Cipher.getInstance("RSA");
    // Initialize the cipher for encryption
    rsaCipher.init(Cipher.ENCRYPT_MODE, myPrivateKey);
    byte[] cipherRnd = rsaCipher.doFinal(rnd);
    //System.out.println(cipherRnd.length);


    Integer messagesize=12+HEdatalength+HEdatalength2+cipherRn d.length;
    byte messagelenlow=messagesize.byteValue();
    messagesize=messagesize.rotateRight(messagesize,8) ;
    byte messagelenhigh=messagesize.byteValue();
    byte[] entete=new byte[8];
    entete[0]=firstByte;
    entete[1]=SID[0];
    entete[2]=messagelenhigh;
    entete[3]=messagelenlow;
    entete[4]=sequenceNumber[0];
    entete[5]=sequenceNumber[1];
    entete[6]=timestamphigh;
    entete[7]=timestamplow;

    byte[] HE1=new byte[HEdatalength];
    HE1[0]=exthigh;
    HE1[1]=extlow1;
    HE1[2]=HEdatahigh;
    HE1[3]=HEdatalow;
    System.arraycopy(certstream,0,HE1,4,certstream.len gth);

    byte[] HE2=new byte[HEdatalength2];
    HE2[0]=exthigh;
    HE2[1]=extlow2;
    HE2[2]=HEdatahigh2;
    HE2[3]=HEdatalow2;
    System.arraycopy(usercap,0,HE2,4,usercap.length);
    messagesize=8+HEdatalength+HEdatalength2+cipherRnd .length;

    byte[] smessage=new byte[messagesize];
    System.arraycopy(entete,0,smessage,0,entete.length );
    System.arraycopy(HE1,0,smessage,entete.length,HE1. length);
    System.arraycopy(HE2,0,smessage,(entete.length+HE1 .length),HE2.length);
    System.arraycopy(cipherRnd,0,smessage,(entete.leng th+HE1.length+HE2.length),cipherRnd.length);

    CheckedInputStream cis=new CheckedInputStream(new ByteArrayInputStream(smessage),new CRC32());
    byte[] buf=new byte[32];
    while(cis.read(buf)>0){}
    Long checksum=cis.getChecksum().getValue();
    byte bb1=checksum.byteValue();
    checksum=checksum.rotateRight(checksum,8);
    byte bb2=checksum.byteValue();
    checksum=checksum.rotateRight(checksum,8);
    byte bb3=checksum.byteValue();
    checksum=checksum.rotateRight(checksum,8);
    byte bb4=checksum.byteValue();
    byte[] bb={bb1,bb2,bb3,bb4};

    byte[] entete2=new byte[12];
    System.arraycopy(entete,0,entete2,0,entete.length) ;
    System.arraycopy(bb,0,entete2,8,4);

    messagesize=12+HEdatalength+HEdatalength2+cipherRn d.length;

    smessage=new byte[messagesize];
    System.arraycopy(entete2,0,smessage,0,entete2.leng th);
    System.arraycopy(HE1,0,smessage,entete2.length,HE1 .length);
    System.arraycopy(HE2,0,smessage,(entete2.length+HE 1.length),HE2.length);
    System.arraycopy(cipherRnd,0,smessage,(entete2.len gth+HE1.length+HE2.length),cipherRnd.length);

    //i have to check if it's the first smessage then i will not do the XOR
    System.out.println("at end previou paket =="+ PrevPkt.getpktclient() );
    //if the previous packet exists , do xor , else it's the first
    if (PrevPkt.getpktclient() != null)
    {
    //define returned var.
    byte[] a = PrevPkt.getpktclient() ;
    int len = (smessage.length > a.length) ? smessage.length : a.length ;
    byte[] xorResult = new byte[len];
    //call XOR method to compute the FEC
    XorB myXor = new XorB();
    xorResult = myXor.xorArray(smessage, a , PrevPkt.getpktclient().length );
    //add the result fec to smessage
    messagesize =smessage.length + xorResult.length;
    byte[] newsvsp =new byte[messagesize];
    System.arraycopy(smessage,0,newsvsp,0,smessage.len gth);
    System.arraycopy(xorResult,0,newsvsp,smessage.leng th,xorResult.length);
    }

    //i have to save acopy of smessage, so the next time this method is called i can do the XOR
    // PrevPkt p1 = new PrevPkt(smessage , 1) ;

    //Signature should be done on newsvsp
    //
    //Signature added on all packet
    Signature rsa=Signature.getInstance("MD5withRSA");
    rsa.initSign(myPrivateKey);
    rsa.update(smessage);
    byte[] sig=rsa.sign();
    // System.out.println("signature"+sig.length);
    byte[] message=new byte[(sig.length+messagesize)];
    System.arraycopy(smessage,0,message,0,messagesize) ;
    System.arraycopy(sig,0,message,messagesize,sig.len gth);
    return (message);

    }
    }


    with PrevPkt class , i was trying to store a copy of smessage
    i did not work

    is there a way in java , to hold the value of a variable , untill you want to change it ?

  7. #7
    Randa is offline Member
    Join Date
    Sep 2008
    Posts
    9
    Rep Power
    0

    Default

    Am only moddifying the code
    i did not write it !!:)

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default

    Without formatting I can NOT follow the logic of your code. It is too hard to read. I can't see where methods begin or end.
    Please use code tags around your code to preserver formatting.
    Also add boundary line comments at the beginning of methods:
    //*******************************************
    hold the value of a variable , untill you want to change it
    Where do you have to save the data? Can it be saved in a class variable? As long as the instance of the class exists, the values of its variables will exist.

    A class variable:
    String saveSMsg; // we'll save smessage here
    ....
    in a method:
    saveSMsg = smessage; // save smessage

Similar Threads

  1. add previous jar library to new one
    By Farzaneh in forum Eclipse
    Replies: 1
    Last Post: 08-23-2008, 03:31 PM
  2. help..on Next n Previous Button
    By norazanita in forum New To Java
    Replies: 0
    Last Post: 06-12-2008, 06:58 AM
  3. Next and Previous Buttons
    By JavaNewb in forum New To Java
    Replies: 1
    Last Post: 05-09-2008, 02:23 AM
  4. Replies: 2
    Last Post: 12-05-2007, 02:19 PM
  5. Using previous with CardLayout
    By uncopywritable in forum New To Java
    Replies: 2
    Last Post: 08-05-2007, 10:43 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
  •