Java Encryption newbie

Discussion in 'Java' started by Marco A. Cruz Quevedo, Sep 11, 2010.

  1. i everybody,

    I want to develop a simple encryption program that does the following
    (in pseudocode)
    ----
    mycert = opencert("mycert.cer");
    publickey pubkey = mycert.getPublicKey();

    string message = readfile("MessageFile.txt");

    string scrambled = encrypt(message,pubkey);

    writefile("encrypted",scrambled);

    ----

    The certificate was generated by keytool and exported by

    keytool -exportcert -alias myself -rfc -file mycert.cer

    I have been reading many java documentation files but I cannot figure
    out which classes I should use.

    Any advice will be greatly appreciated.

    Regards,

    Marco A. Cruz Quevedo.

    ------------------------
    Freedom is not a permission for chaos.
     
    Marco A. Cruz Quevedo, Sep 11, 2010
    #1
    1. Advertising

  2. On 09/11/2010 03:09 PM, Marco A. Cruz Quevedo wrote:
    > mycert = opencert("mycert.cer");


    <http://java.sun.com/javase/6/docs/api/java/security/cert/CertificateFactory.html>
    has a bit of code that tells you how to get a
    java.security.cert.Certificate from a file.

    > publickey pubkey = mycert.getPublicKey();


    This is even more trivial.

    > string message = readfile("MessageFile.txt");


    I presume you know how to use Java file I/O already, so I won't even
    bother here.

    > string scrambled = encrypt(message,pubkey);


    Encryption and decryption are handled via javax.crypto.Cipher. Note that
    this is done in blocks, so you presumably would need reading/writing in
    blocks as opposed to streamed I/O.

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
     
    Joshua Cranmer, Sep 11, 2010
    #2
    1. Advertising

  3. Marco A. Cruz Quevedo wrote:

    > I want to develop a simple encryption program that does the following.


    Take a good look and see if using keyczar is a viable option for you.

    Keyczar is a open source crypto library initially developed by google
    because "Cryptography is easy to get wrong.". It designed to use safe
    defaults and offer a high level api to reduce programmatical faults.

    http://www.keyczar.org/

    --
    Fredrik Jonson
     
    Fredrik Jonson, Sep 12, 2010
    #3
  4. Marco A. Cruz Quevedo

    Roedy Green Guest

    On Sat, 11 Sep 2010 12:09:58 -0700 (PDT), "Marco A. Cruz Quevedo"
    <> wrote, quoted or indirectly quoted someone who
    said :

    >I want to develop a simple encryption program that does the following


    see http://mindprod.com/jgloss/aes.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    You encapsulate not just to save typing, but more importantly, to make it easy and safe to change the code later, since you then need change the logic in only one place. Without it, you might fail to change the logic in all the places it occurs.
     
    Roedy Green, Sep 14, 2010
    #4
  5. rossum wrote:
    > On Sat, 11 Sep 2010 12:09:58 -0700 (PDT), "Marco A. Cruz Quevedo"
    > <> wrote:
    >
    >> i everybody,
    >>
    >> I want to develop a simple encryption program that does the following
    >> (in pseudocode)
    >> ----
    >> mycert = opencert("mycert.cer");
    >> publickey pubkey = mycert.getPublicKey();
    >>
    >> string message = readfile("MessageFile.txt");
    >>
    >> string scrambled = encrypt(message,pubkey);

    > This may not be what you want to do. Public Key (asymmetric)
    > encryption is secure but slow. It is not generally used for large
    > quantities of data. It is more common to use the public key to
    > encrypt and send a short private key and then to use that private key
    > to encrypt a large volume of data using AES or some other symmetric
    > encryption system.
    >
    > rossum
    >
    >

    Thank you very much for your comment but in fact, the messages I need
    to encrypt are less than 1Kb, so it suits my needs.

    Regards,

    Marco A. Cruz Quevedo
    ------------------------
    Freedom is not a permission for chaos.
     
    Marco A. Cruz Quevedo, Sep 14, 2010
    #5
  6. Marco A. Cruz Quevedo

    Arne Vajhøj Guest

    On 14-09-2010 11:33, Marco A. Cruz Quevedo wrote:
    > rossum wrote:
    >> On Sat, 11 Sep 2010 12:09:58 -0700 (PDT), "Marco A. Cruz Quevedo"
    >> <> wrote:
    >>> I want to develop a simple encryption program that does the following
    >>> (in pseudocode)
    >>> ----
    >>> mycert = opencert("mycert.cer");
    >>> publickey pubkey = mycert.getPublicKey();
    >>>
    >>> string message = readfile("MessageFile.txt");
    >>>
    >>> string scrambled = encrypt(message,pubkey);

    >> This may not be what you want to do. Public Key (asymmetric)
    >> encryption is secure but slow. It is not generally used for large
    >> quantities of data. It is more common to use the public key to
    >> encrypt and send a short private key and then to use that private key
    >> to encrypt a large volume of data using AES or some other symmetric
    >> encryption system.


    > Thank you very much for your comment but in fact, the messages I need
    > to encrypt are less than 1Kb, so it suits my needs.


    The technique suggested by rossum is usually preferred
    for data sizes >32 bytes or so.

    Arne
     
    Arne Vajhøj, Sep 15, 2010
    #6
    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. Daniel Fiske
    Replies:
    0
    Views:
    459
    Daniel Fiske
    Aug 29, 2003
  2. Andy Fish

    dummies guide to java encryption

    Andy Fish, Jun 21, 2004, in forum: Java
    Replies:
    2
    Views:
    10,346
    Roedy Green
    Jun 21, 2004
  3. vaneric

    newbie doubt in encryption

    vaneric, Dec 9, 2008, in forum: Java
    Replies:
    2
    Views:
    287
    RedGrittyBrick
    Dec 9, 2008
  4. Marco A. Cruz Quevedo

    Java encryption newbie (2)

    Marco A. Cruz Quevedo, Oct 9, 2010, in forum: Java
    Replies:
    4
    Views:
    338
    Arne Vajhøj
    Oct 11, 2010
  5. Robert
    Replies:
    2
    Views:
    133
    Roland Schmitt
    Oct 5, 2005
Loading...

Share This Page