Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. OneWay hash encryption error in different Os.

OneWay hash encryption error in different Os.

Scheduled Pinned Locked Moved Java
helplinuxalgorithmssecuritycryptography
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    saigowthami
    wrote on last edited by
    #1

    Hello, When i am encrypting a string (One way hashed) using SHA algorithm, the output differs from two different platforms. When done in windows, an extra CR (CR LF) character is appearing but when done in linux box only LF character is appearing in between the output encrypted value. FYI - CR (enter) LF(Space) This leading me with issues in further process of application. Kindly help for the same Gowthami

    L B 2 Replies Last reply
    0
    • S saigowthami

      Hello, When i am encrypting a string (One way hashed) using SHA algorithm, the output differs from two different platforms. When done in windows, an extra CR (CR LF) character is appearing but when done in linux box only LF character is appearing in between the output encrypted value. FYI - CR (enter) LF(Space) This leading me with issues in further process of application. Kindly help for the same Gowthami

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      This has nothing to do with encryption. The CR LF sequence is set in Windows wherever the output string of a printf() format contains the \n control. In linux (and UNIX) it is replaced with a single LF.

      Use the best guess

      S 1 Reply Last reply
      0
      • L Lost User

        This has nothing to do with encryption. The CR LF sequence is set in Windows wherever the output string of a printf() format contains the \n control. In linux (and UNIX) it is replaced with a single LF.

        Use the best guess

        S Offline
        S Offline
        saigowthami
        wrote on last edited by
        #3

        No it is not about the last character of string. see for example if i do one way hash for '1234567890127866' have a look on the outputs Windows: +zWcE7pR4k97ysyi54WS4G6J1Qjfp/tXllE23mMLCFBrIONMlrXyMw9aF1UCFKCFjbcz0Yzge78u(THIS IS THE PLACE WHERE I M GETTING CR LF)Usye+/4Ulw== Linux: +zWcE7pR4k97ysyi54WS4G6J1Qjfp/tXllE23mMLCFBrIONMlrXyMw9aF1UCFKCFjbcz0Yzge78u(THIS IS THE PLACE WHERE I M GETTING ONLY LF)Usye+/4Ulw== which means the whole characters represents single string

        Regards Gowthami

        L 1 Reply Last reply
        0
        • S saigowthami

          Hello, When i am encrypting a string (One way hashed) using SHA algorithm, the output differs from two different platforms. When done in windows, an extra CR (CR LF) character is appearing but when done in linux box only LF character is appearing in between the output encrypted value. FYI - CR (enter) LF(Space) This leading me with issues in further process of application. Kindly help for the same Gowthami

          B Offline
          B Offline
          Bernhard Hiller
          wrote on last edited by
          #4

          See the answer to a question on StackOverflow: http://stackoverflow.com/questions/2620975/strange-n-in-base64-encoded-string-in-ruby[^]: "... add a newline for every 60th character ..." And see also http://en.wikipedia.org/wiki/Base64[^] That means that the new lines do not contain meaning, they are used for formatting purposes only. Before comparing the strings, you remove all types of newlines, and things will work fine (some decoders for base64 may require newlines, but since you need to compare hashed values only, that does not matter).

          S 1 Reply Last reply
          0
          • S saigowthami

            No it is not about the last character of string. see for example if i do one way hash for '1234567890127866' have a look on the outputs Windows: +zWcE7pR4k97ysyi54WS4G6J1Qjfp/tXllE23mMLCFBrIONMlrXyMw9aF1UCFKCFjbcz0Yzge78u(THIS IS THE PLACE WHERE I M GETTING CR LF)Usye+/4Ulw== Linux: +zWcE7pR4k97ysyi54WS4G6J1Qjfp/tXllE23mMLCFBrIONMlrXyMw9aF1UCFKCFjbcz0Yzge78u(THIS IS THE PLACE WHERE I M GETTING ONLY LF)Usye+/4Ulw== which means the whole characters represents single string

            Regards Gowthami

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            I have just tried the same test and get a completely different result ([B@1748ba4), so I can only assume that there is some more information that you have not given us.

            Use the best guess

            S 1 Reply Last reply
            0
            • L Lost User

              I have just tried the same test and get a completely different result ([B@1748ba4), so I can only assume that there is some more information that you have not given us.

              Use the best guess

              S Offline
              S Offline
              saigowthami
              wrote on last edited by
              #6

              meaning?? have you tried to encrypt the string in both windows and linux??

              md = MessageDigest.getInstance("SHA-512");
              md.update(plaintext.getBytes("UTF-8"));

                      byte raw\[\] = md.digest();
              
                      String hash = (new BASE64Encoder()).encode(raw);
              
                      return hash;
              

              This is what I have used to encrypt

              L 1 Reply Last reply
              0
              • S saigowthami

                meaning?? have you tried to encrypt the string in both windows and linux??

                md = MessageDigest.getInstance("SHA-512");
                md.update(plaintext.getBytes("UTF-8"));

                        byte raw\[\] = md.digest();
                
                        String hash = (new BASE64Encoder()).encode(raw);
                
                        return hash;
                

                This is what I have used to encrypt

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                saigowthami wrote:

                meaning??

                Meaning that all you showed us was the source data and the resulting hash value, we had no idea what code you had used.

                Use the best guess

                S 1 Reply Last reply
                0
                • B Bernhard Hiller

                  See the answer to a question on StackOverflow: http://stackoverflow.com/questions/2620975/strange-n-in-base64-encoded-string-in-ruby[^]: "... add a newline for every 60th character ..." And see also http://en.wikipedia.org/wiki/Base64[^] That means that the new lines do not contain meaning, they are used for formatting purposes only. Before comparing the strings, you remove all types of newlines, and things will work fine (some decoders for base64 may require newlines, but since you need to compare hashed values only, that does not matter).

                  S Offline
                  S Offline
                  saigowthami
                  wrote on last edited by
                  #8

                  ok but my database is in linux machine, where the strings encrypted and saved to database, while i m running the same encryption util in linux the output is not matching with the database one. Meanwhile both places the code is same to encrypt the string.

                  B 1 Reply Last reply
                  0
                  • L Lost User

                    saigowthami wrote:

                    meaning??

                    Meaning that all you showed us was the source data and the resulting hash value, we had no idea what code you had used.

                    Use the best guess

                    S Offline
                    S Offline
                    saigowthami
                    wrote on last edited by
                    #9

                    Ya i have posted my code in my previous reply itself

                    L 1 Reply Last reply
                    0
                    • S saigowthami

                      Ya i have posted my code in my previous reply itself

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      I cannot find BASE64Encoder so cannot try recreating your problem. However, as Bernhard explained in this response[^], these extra line terminators are added when you convert the data to a string. Keep it as a byte[] and see if you still have the problem.

                      Use the best guess

                      1 Reply Last reply
                      0
                      • S saigowthami

                        ok but my database is in linux machine, where the strings encrypted and saved to database, while i m running the same encryption util in linux the output is not matching with the database one. Meanwhile both places the code is same to encrypt the string.

                        B Offline
                        B Offline
                        Bernhard Hiller
                        wrote on last edited by
                        #11

                        Look: - before you store the value in the database, remove all newlines. - before you compare a value with a value from the db, remove all newlines. That works. Of course, you have to update your databse in case that you have some entries there already.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups