Java DES

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eyeofsoul
    New Member
    • Sep 2007
    • 31

    Java DES

    hello..i am working on encrypting string in des..instead of generating the key, how can i make it encrypt using other key that i desire?!..
  • Dököll
    Recognized Expert Top Contributor
    • Nov 2006
    • 2379

    #2
    Originally posted by eyeofsoul
    hello..i am working on encrypting string in des..instead of generating the key, how can i make it encrypt using other key that i desire?!..
    Hello, eyeofsoul!

    I am a beginner in this forum my first couple of real posts actually. Would you like to go into it a little more? Might help experts here get a better handle on it, perhaps a step by step pseudocode or something in that kind of a line.

    Also, stay tuned, I'd fire up the post again later rather than posting similar posts.

    Good luck:-)

    Dököll

    Comment

    • eyeofsoul
      New Member
      • Sep 2007
      • 31

      #3
      Originally posted by Dököll
      Hello, eyeofsoul!

      I am a beginner in this forum my first couple of real posts actually. Would you like to go into it a little more? Might help experts here get a better handle on it, perhaps a step by step pseudocode or something in that kind of a line.

      Also, stay tuned, I'd fire up the post again later rather than posting similar posts.

      Good luck:-)

      Dököll
      i found a code similar in what i am looking for but when i compile, it suppose to have different cipher text between both string but all i got is the same..
      Code:
      public static void testUsingPassPhrase() {
      
              System.out.println();
              System.out.println("+----------------------------------------+");
              System.out.println("|  -- Test Using Pass Phrase Method --   |");
              System.out.println("+----------------------------------------+");
              System.out.println();
      
              String secretString = "http://www.nawuza.org";
              String passPhrase   = "My Pass Phrase";
             
      
              // Create encrypter/decrypter class
              StringEncrypter desEncrypter = new StringEncrypter(passPhrase);
              StringEncrypter blowfishEncrypter = new StringEncrypter(passPhrase);
              StringEncrypter desedeEncrypter = new StringEncrypter(passPhrase);
      
              // Encrypt the string
              String desEncrypted       = desEncrypter.encrypt(secretString);
              String blowfishEncrypted  = blowfishEncrypter.encrypt(secretString);
              String desedeEncrypted    = desedeEncrypter.encrypt(secretString);
              
              // Decrypt the string
              String desDecrypted       = desEncrypter.decrypt(desEncrypted);
              String blowfishDecrypted  = blowfishEncrypter.decrypt(blowfishEncrypted);
              String desedeDecrypted    = desedeEncrypter.decrypt(desedeEncrypted);
      
              // Print out values
              System.out.println("PBEWithMD5AndDES Encryption algorithm");
              System.out.println("    Original String  : " + secretString);
              System.out.println("    Encrypted String : " + desEncrypted);
              System.out.println("    Decrypted String : " + desDecrypted);
              System.out.println();
              System.out.println(blowfishEncrypted);
              System.out.println(desedeEncrypted);
              System.out.println("hahaha");
      
          }

      Comment

      • chaarmann
        Recognized Expert Contributor
        • Nov 2007
        • 785

        #4
        Sometimes, looking up the methods of a class solves all your problems.
        I didn't know myself how to define my own key, but then I looked for constructor of StringEncrypter class in the JDK description and found it immediately.

        I also found also code in Google right away by looking for StringEncrypter , here is the solution you need: (change encrytionKey to your own key)

        Code:
           String encryptionKey = "123456789012345678901234567890";
        	
             EncryptionScheme encryptionScheme = DesEdeEncryptionScheme.INSTANCE;
        
             StringEncrypter encrypter = 
                  new StringEncrypter( encryptionScheme, encryptionKey );
             String encryptedString = encrypter.encrypt( stringToEncrypt );
        So problem solved within 10 seconds.
        What was your difficulty in simply looking it up?

        Comment

        • eyeofsoul
          New Member
          • Sep 2007
          • 31

          #5
          Originally posted by chaarmann
          Sometimes, looking up the methods of a class solves all your problems.
          I didn't know myself how to define my own key, but then I looked for constructor of StringEncrypter class in the JDK description and found it immediately.

          I also found also code in Google right away by looking for StringEncrypter , here is the solution you need: (change encrytionKey to your own key)

          Code:
             String encryptionKey = "123456789012345678901234567890";
          	
               EncryptionScheme encryptionScheme = DesEdeEncryptionScheme.INSTANCE;
          
               StringEncrypter encrypter = 
                    new StringEncrypter( encryptionScheme, encryptionKey );
               String encryptedString = encrypter.encrypt( stringToEncrypt );
          So problem solved within 10 seconds.
          What was your difficulty in simply looking it up?
          what is mean is, i have a password.then i transform the password into a key to encrypt for example a string using some algo for example blowfish. if we what to encrypt, a key is needed right. What is confusing me is on how to create our own key using the password(a string) and then encrypt some string using some other algo.sorry but your are not getting what in my mind,

          Comment

          • chaarmann
            Recognized Expert Contributor
            • Nov 2007
            • 785

            #6
            Originally posted by eyeofsoul
            what is mean is, i have a password.then i transform the password into a key to encrypt for example a string using some algo for example blowfish. if we what to encrypt, a key is needed right. What is confusing me is on how to create our own key using the password(a string) and then encrypt some string using some other algo.sorry but your are not getting what in my mind,
            So why don't you use the password directly as a key then? if it is too short, just concatenate it multiple times until it has the right length for the key. For example "mypass" --> "mypassmypassmy pas".
            If you don't want to use the password directly, then you can use the hashcode of the password string as a key.

            What you wrote here is very strange . Normally, you have one single key for the application to encrypt all strings. You don't take passwords to generate the key, and then with that key you encrypt all the strings. Why would you double-encrypt if a single encryption is already strong enough? That does not make much sense to me. Are you planning to give the decryption-key out? Then you should think about Public Key encryption.
            I am really not getting what's in your mind. I am just guessing from what you have written. Maybe you can write it down more clearly?

            Comment

            • eyeofsoul
              New Member
              • Sep 2007
              • 31

              #7
              Originally posted by chaarmann
              So why don't you use the password directly as a key then? if it is too short, just concatenate it multiple times until it has the right length for the key. For example "mypass" --> "mypassmypassmy pas".
              If you don't want to use the password directly, then you can use the hashcode of the password string as a key.

              What you wrote here is very strange . Normally, you have one single key for the application to encrypt all strings. You don't take passwords to generate the key, and then with that key you encrypt all the strings. Why would you double-encrypt if a single encryption is already strong enough? That does not make much sense to me. Are you planning to give the decryption-key out? Then you should think about Public Key encryption.
              I am really not getting what's in your mind. I am just guessing from what you have written. Maybe you can write it down more clearly?
              ok..let's start from the beginning..i have a string1("Intern et Explorer"). then i want to encrypt it using DES for example.in order to encrypt the string1, we need a key which represent we are using DES encryption. what i want to do is i have another string2 which is "dessecret" . then i use the string to generate key for the encryption.
              Code:
              KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
              SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
              passphrase is the string2..above is how i generate the DES key..if i am not mistaken.

              Comment

              • chaarmann
                Recognized Expert Contributor
                • Nov 2007
                • 785

                #8
                Originally posted by eyeofsoul
                ok..let's start from the beginning..i have a string1("Intern et Explorer"). then i want to encrypt it using DES for example.in order to encrypt the string1, we need a key which represent we are using DES encryption. what i want to do is i have another string2 which is "dessecret" . then i use the string to generate key for the encryption.
                Code:
                KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
                SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
                passphrase is the string2..above is how i generate the DES key..if i am not mistaken.
                and what about my sentence: If you don't want to use the password directly, then you can use the hashcode of the password string as a key.

                To help me understanding, can you answer why you don't want to use the password or the hashcode of the password (in your example "dessecret" or "dessecret".has hCode() ) directly as a decryption key? With this key then you can decrypt/encrypt your string "Internet Exploder". Exactly as you have described it again. Or am I missing something in your description?

                Comment

                Working...