Help? Java sucks



  • import com.sun.corba.se.impl.ior.ByteBuffer;

    import java.;

    import java.io.FileWriter;

    import java.lang.
    ;

    import java.security.;

    import javax.crypto.
    ;

    import javax.crypto.spec.*;

    //Tried to utilize built in java encryption techniques, but it was
    nearly impossbie because i could not figure out why it was not getting
    passed through...either the key isnt being generated, or the cipher
    does not like the key that is being generated.  

    //tried to get my grandpa to help me but he couldnt figure it out either....this is what we were able to come up with...

    public class EncryptString

    {

       

     //   EncryptString() {}

        /**

         * @param args the command line arguments

         */

        public static void main(String[] args)

        {

            if(args.length < 2)

            {

                System.out.println("Usage:");

                System.out.println("EncryptString AlgorithmName String...");

                return;

            }

            System.out.println(args[0]);

            ByteBuffer Encrypted = new ByteBuffer();

            StringBuffer szbEncryptMe = new StringBuffer();

            szbEncryptMe.append(args[1]);

            for(int i = 2; i < args.length; i++)

                szbEncryptMe.append(" ").append(args[i]);

            System.out.println(szbEncryptMe);

            

            System.out.println( (Encrypt(szbEncryptMe.toString(), args[0])).toString() );

        }

        

        static final byte[] invalid = { 0, 0, 0, 0 };



        public static void AppendByteArrayToByteBuffer(byte[] array, ByteBuffer buffer)

        {

            for(int i = 0; i < array.length; i++)

                buffer.append(array[i]);

        }

        

        public static byte[] Encrypt(String szEncryptMe, String szAlgorithm)

        {

            KeyGenerator kgen;

            SecretKey skey;

            SecretKeySpec skeySpec;

            Cipher cipher;

            ByteBuffer rawdata = new ByteBuffer();

            try {

                kgen
    = KeyGenerator.getInstance(szAlgorithm); 
    //NoSuchAlgorithmException

                skey = kgen.generateKey();

                AppendByteArrayToByteBuffer(skey.getEncoded(), rawdata);

                skeySpec = new SecretKeySpec(rawdata.toArray(), szAlgorithm);

               
    cipher = Cipher.getInstance(szAlgorithm); //NoSuchAlgorithmException

                System.out.println("init Encrypt Mode w skeySpec");

               
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec); //InvalidKeyException

                System.out.println("doFinal on szEncryptMe");

               
    AppendByteArrayToByteBuffer(cipher.doFinal(szEncryptMe.getBytes()),
    rawdata); //IllegalBlockSizeException

                System.out.println("finished:");

                System.out.println(rawdata.toString());

            } catch (Exception e) {

                System.out.println(e.getMessage());

                e.printStackTrace();

                return invalid;

            }

            return rawdata.toArray();

        }



    }



    WHY DOESNT THIS catch the bytes and create a string to output? why god?
    why? i hate java security package and i also hate java projects



  • @TJenner said:

    import com.sun.corba.se.impl.ior.ByteBuffer;

    import java.;

    import java.io.FileWriter;

    import java.lang.
    ;

    import java.security.;

    import javax.crypto.
    ;

    import javax.crypto.spec.*;

    //Tried to utilize built in java encryption techniques, but it was
    nearly impossbie because i could not figure out why it was not getting
    passed through...either the key isnt being generated, or the cipher
    does not like the key that is being generated.  

    //tried to get my grandpa to help me but he couldnt figure it out either....this is what we were able to come up with...

    public class EncryptString

    {

       

     //   EncryptString() {}

        /**

         * @param args the command line arguments

         */

        public static void main(String[] args)

        {

            if(args.length < 2)

            {

                System.out.println("Usage:");

                System.out.println("EncryptString AlgorithmName String...");

                return;

            }

            System.out.println(args[0]);

            ByteBuffer Encrypted = new ByteBuffer();

            StringBuffer szbEncryptMe = new StringBuffer();

            szbEncryptMe.append(args[1]);

            for(int i = 2; i < args.length; i++)

                szbEncryptMe.append(" ").append(args[i]);

            System.out.println(szbEncryptMe);

            

            System.out.println( (Encrypt(szbEncryptMe.toString(), args[0])).toString() );

        }

        

        static final byte[] invalid = { 0, 0, 0, 0 };



        public static void AppendByteArrayToByteBuffer(byte[] array, ByteBuffer buffer)

        {

            for(int i = 0; i < array.length; i++)

                buffer.append(array[i]);

        }

        

        public static byte[] Encrypt(String szEncryptMe, String szAlgorithm)

        {

            KeyGenerator kgen;

            SecretKey skey;

            SecretKeySpec skeySpec;

            Cipher cipher;

            ByteBuffer rawdata = new ByteBuffer();

            try {

                kgen
    = KeyGenerator.getInstance(szAlgorithm); 
    //NoSuchAlgorithmException

                skey = kgen.generateKey();

                AppendByteArrayToByteBuffer(skey.getEncoded(), rawdata);

                skeySpec = new SecretKeySpec(rawdata.toArray(), szAlgorithm);

               
    cipher = Cipher.getInstance(szAlgorithm); //NoSuchAlgorithmException

                System.out.println("init Encrypt Mode w skeySpec");

               
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec); //InvalidKeyException

                System.out.println("doFinal on szEncryptMe");

               
    AppendByteArrayToByteBuffer(cipher.doFinal(szEncryptMe.getBytes()),
    rawdata); //IllegalBlockSizeException

                System.out.println("finished:");

                System.out.println(rawdata.toString());

            } catch (Exception e) {

                System.out.println(e.getMessage());

                e.printStackTrace();

                return invalid;

            }

            return rawdata.toArray();

        }



    }



    WHY DOESNT THIS catch the bytes and create a string to output? why god?
    why? i hate java security package and i also hate java projects


    As someone who enjoys working in Java, I must tell you that seeking advice and help in the manner you've done is all wrong. Why should I even waste my time with a post titled "Java sucks"?

    sincerely,
    Richard Nixon



  • Why should I even waste my time with a post titled "Java sucks"?

    Because you've wasted time learning a language that sucks.



  • people arent allowed to get frustrated without getting flamed? dont take it personally



  • Is it my imagination or are you trying to encrypt a string with a randomly generated key?  How are you going to decrypt it?

    Also, your code throws a NoSuchAlgorithmException.  However, you never tell us what algorithm you used when you called your code.  Maybe there is no such algorithm.

     



  • You use toString() too easily.

    System.out.println( (Encrypt(szbEncryptMe.toString(), args[0])).toString() );
    does not output the byte array the way you think (even if everything else worked as expected)

    System.out.println( new String (Encrypt(szbEncryptMe.toString(), args[0])));

    would (kind of) do the trick (though it's still not a good idea).

    I think you have to learn that Java is not C. A string is not the same as a byte array.

    byte[] result = Encrypt(szbEncryptMe.toString(), args[0]);
    Sytem.out.write(result,0,result.length);

    is the way to go.




  • and you're not supposed to use anything from the com.sun packages, they're for internal use by the JVM only and documented as such.

    Java doesn't suck, but many who fail to use it properly sometimes do get that impression.



  • @Savior said:

    Why should I even waste my time with a post titled "Java sucks"?

    Because you've wasted time learning a language that sucks.



    ZING! My own personal troll strikes again!

    Haven't been out in awhile, have you?

    sincerely,
    Richard Nixon


  • @Richard Nixon said:

    @Savior said:

    Why should I even waste my time with a post titled "Java sucks"?

    Because you've wasted time learning a language that sucks.



    ZING! My own personal troll strikes again!

    Haven't been out in awhile, have you?

    sincerely,
    Richard Nixon

    Why? Did you miss me? [;)]


Log in to reply