PBE

Discussion in 'Java' started by jimgardener, Jun 23, 2008.

  1. jimgardener

    jimgardener Guest

    i am learning cryptography using Jason weiss book.i tried out
    encryption using PBEWithMD5AndDES ,i could encrypt a string as below

    byte[] salt=new byte[] { (byte)0x3a, (byte)0x44, (byte)0x7f,
    (byte)0xfl,(byte)0xa2, (byte)0xe5, (byte)0x87, (byte)0x31 };
    int iter=1000;
    String msg="a plain message";
    char[] passwd=new char[]{'m','y','c','o','d','e'};
    PBEKeySpec pbeKeySpec = new PBEKeySpec(passwd, salt, iter) ;
    SecretKeyFactory factory=null;
    SecretKey key=null;
    Cipher cipher=null;
    try{
    factory =SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    key = factory.generateSecret(pbeKeySpec) ;
    cipher=Cipher.getInstance("PBEWithMD5AndDES");
    cipher.init (Cipher. ENCRYPT_MODE, key) ;

    byte[] cipherText = cipher.doFinal(msg .getBytes()) ;
    FileOutputStream out=new FileOutputStream("pbeencrypted.txt");
    out.write(cipherText);
    Arrays.fill(passwd, '\u0000');


    now how can i decrypt this?can i use the same keyspec as above and
    what algorithm should i use to create cipher? cipher.init (Cipher.
    DECRYPT_MODE, key) ; will cause 'InvalidKeyException: requires PBE
    parameters'

    can someone help?
    jim
    jimgardener, Jun 23, 2008
    #1
    1. Advertising

  2. jimgardener

    Roedy Green Guest

    On Mon, 23 Jun 2008 07:32:57 -0700 (PDT), jimgardener
    <> wrote, quoted or indirectly quoted someone who
    said :

    >now how can i decrypt this?can i use the same keyspec as above and
    >what algorithm should i use to create cipher? cipher.init (Cipher.
    >DECRYPT_MODE, key) ; will cause 'InvalidKeyException: requires PBE
    >parameters'


    I have a similar example posted at
    http://mindprod.com/jgloss/cipher.html

    It uses the simpler AES algorithm, but much the same problems apply.

    In the real world, you must somehow get your secret key to the other
    end, perhaps by secure courier. In public/private key systems you can
    exchange public keys by insecure channels, so long as you make sure
    there was no tampering with a followup phone call to confirm the
    digest.

    JCE has methods to export keys as raw bytes. You can probable also
    export them armoured in various packagings too. see
    http://mindprod.com/jgloss/armouring.html
    if you want experiment with manual armouring.

    JCE suffers from a lack of documenation. There is no way you can do
    even the simplest thing without something beyond the JavaDoc. You
    can try books or googling for algorithm and method names. I learned
    most from comments in various snippets of code.

    see http://mindprod.com/jgloss/jce.html

    It also helps to read up on the mathematical background of the various
    algorithms not so that you fully understand them,, but so at least
    you have an idea of what sort of extra parms they may be hungry for.


    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Jun 23, 2008
    #2
    1. Advertising

  3. jimgardener

    Roedy Green Guest

    On Mon, 23 Jun 2008 07:32:57 -0700 (PDT), jimgardener
    <> wrote, quoted or indirectly quoted someone who
    said :

    >now how can i decrypt this?can i use the same keyspec as above and
    >what algorithm should i use to create cipher? cipher.init (Cipher.
    >DECRYPT_MODE, key) ; will cause 'InvalidKeyException: requires PBE
    >parameters'


    try google on [PBE DECRYPT_MODE] that will find you code involving PBE
    and decryption. That will likely give you the key. Watch out.
    BouncyCastle, Sun etc providers need not all work identically.
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Jun 23, 2008
    #3
  4. jimgardener

    jimgardener Guest

    try google on [PBE DECRYPT_MODE] that will find you code involving PBE
    > and decryption. That will likely give you the key.



    i modified the code to

    PBEKeySpec pbeKeySpec = new PBEKeySpec(passwd) ;
    and
    PBEParameterSpec ps = new PBEParameterSpec(salt, 1000);
    instead of
    PBEKeySpec pbeKeySpec = new PBEKeySpec(passwd,salt, 1000) ;

    and then used the ps in
    cipher.init (Cipher. ENCRYPT_MODE, key,ps) ;
    and later
    cipher.init(Cipher.DECRYPT_MODE, key,ps);

    this gives the correct decryption..

    I still couldn't get the case PBEKeySpec pbeKeySpec = new
    PBEKeySpec(passwd,salt, 1000) to do decryption ..i wonder if someone
    can solve this..
    jim
    jimgardener, Jun 24, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    849
Loading...

Share This Page