This week's book giveaway is in the Spring forum.
We're giving away four copies of Java Persistence with Spring Data and Hibernate and have Cătălin Tudose on-line!
See this thread for details.
Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Android Studio: Passing a String value to another class in Java

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm attempting to write an app to exploit some vulnerabilities in a testing app.  I think I'm very close to having it done, but I've reached the end of my Java knowledge and stuck on one part.  Background on what the app does:  It takes an encrypted password from one app, decrypts it, then with using that decrypted password, it will then attempt to decrypt an encrypted text file on the second app.  I have all of that logic worked out when I hard code the strings.  I'm now trying to make it so it's more dynamic in case the password or the encrypted text ever changes.    There are three parts to this:  1) Decrypt class: this decrypts the encrypted password on app1.  2) Decrypt 2 class: this decrypts the stored text on app2.  3) MainActivity class.  This has the button that starts the process, logs the info and some other functions.  Not sure if all of that is important on what I need to do, but figured I'd share it.

What I need to do is take the decrypted password I get that's in "Log.i("Password:  ", decrypted1);" from MainActivity class, and then pass that string to "String pin_to_md5 = RIGHTHERE" in the Decrypt2 class.  I've been at this for about 10 hours and can't figure it out.  I'm hoping someone here more knowledgeable than I am can help me out.   Below are the two classes I need to work with.  The Decrypt class is working flawlessly, so I hope I don't need to change anything with it.

MainActivity:



Decrypt2 class:
 
Rancher
Posts: 4936
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you save the value in  decrypted1 in a class level variable and pass it to the constructor for the Decrypt2 class?
 
Charley Bates
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Can you save the value in  decrypted1 in a class level variable and pass it to the constructor for the Decrypt2 class?



That's the route I want to go, but not sure how to go about doing it.  I'm not sure how to save the value in decrypted1 in a class level variable or pass it to the constructor.  I'm sure it's probably not that difficult of a task, but I'm still learning Java.  
 
Norm Radder
Rancher
Posts: 4936
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Declare decrypted1 before the  onCreate method so it is at class level.  Then assign it a value on line 39.  That value will be available when the Decrypt2 class is created.

There is a problem with the loop that the assignment statement is in.  Only the last value assigned to decrypted1 will be available when the Decrypt2 constructor is called.
 
Charley Bates
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Declare decrypted1 before the  onCreate method so it is at class level.  Then assign it a value on line 39.  That value will be available when the Decrypt2 class is created.

There is a problem with the loop that the assignment statement is in.  Only the last value assigned to decrypted1 will be available when the Decrypt2 constructor is called.



Is there a way you can show me an example on how to do this?  I'm trying to understand how it would work, but I'm not sure how it would retain the decrypted password valume from the encrypted password from the URI query if it's declared before the onCreate method.  I think it would make more sense to me if I can see an example of it.


As far as "Only the last value assigned to decrypted1" I think that will still work for what I'm trying to achieve.  So lets say for example I run the app, and it spits out the decrypted password as "test1234" for decrypted1.  I only care to use that in Decrypt2, so that should be fine if it's the last value assigned.  I think that is what you  mean by that.   Sorry if I misunderstood.
 
Norm Radder
Rancher
Posts: 4936
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

how it would retain the decrypted password valume from the encrypted password from the URI query if it's declared before the onCreate method.  


The declaration of the variable does not need to assign it a value.  The variable's initial value would be null.  When line 39 is executed it would be assigned the value.  New line 39:
 
Charley Bates
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

how it would retain the decrypted password valume from the encrypted password from the URI query if it's declared before the onCreate method.  


The declaration of the variable does not need to assign it a value.  It's initial value would be null.  When line 39 is executed it would be assigned the value.  New line 39:



Thank you.  That gives me something to work with.  I'll see if I can get it working.  I appreciate the help on this.
 
Charley Bates
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

how it would retain the decrypted password valume from the encrypted password from the URI query if it's declared before the onCreate method.  


The declaration of the variable does not need to assign it a value.  The variable's initial value would be null.  When line 39 is executed it would be assigned the value.  New line 39:



So this is where my inexperience with Java is kicking in.  When you say declare before the onCreate method, is it as simple as this or is there more to it:



If that is right, I'm not able to get Decrypt2 class to use decrypted1, but before I mess around with that too much, I want to make sure I have it declared correction.
 
Norm Radder
Rancher
Posts: 4936
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes that looks like what I was talking about for placing a declared variable.

I'm not able to get Decrypt2 class to use decrypted1


How are you passing the value of  decrypted1 to the Decrypt2 class?
 
Charley Bates
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Yes that looks like what I was talking about for placing a declared variable.

I'm not able to get Decrypt2 class to use decrypted1


How are you passing the value of  decrypted1 to the Decrypt2 class?



I've been trying to create a constructor, but either it's not working, or I just have no idea how to do it.  I'm guessing the latter.   Any tips on how to form it?  I've been at this for a few days now (the overall app, not this particular piece) and I feel like this is the last thing I need to be done.  I hate leaving things unfinished, so I have to see it through now since I'm so close.  You help me get this working, and I'll gladly venmu or paypal you like $10-15 bucks to get a coffee or drink on me for helping me out.  Plus I'll learn how to do this for in the future.
 
Norm Radder
Rancher
Posts: 4936
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Passing a value to a constructor is very basic java.  How did you get to this point in your programming without learning how to use a class constructor?
Here is a link to the tutorial re constructors:   https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html


I've been trying to create a constructor,


If you are having problems with the code, you need to post the code and the error messages or a description of the problem.
 
Marshal
Posts: 77539
372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my opinion, which applies to odinary Java® and might not apply to Android,
  • 1: Every class must have at least one explicit constructor.
  • 2: Constructors can have any access modifier including private.
  • 3: Private access can have a useful side‑effect as described in the Java® Language Specification (=JLS).
  • 4: Every field in the class must be initialised to a real value by the time the constructor completes.
  • 5: The name of the constructor is exactly the same as the name of the class. Not a matter of opinion.
  • 6: A constructor mustn't have a return type. Not a matter of opinion, again.
  • 7: A class can have multiple constructors, with different parameter lists. In my opinion, the more constructors there are, the more opportunity there is for something to go wrong, so that requires great care.
  • 8: Constructors can call one another, but I haven't got the time to explain this(...); and super(...);
  • I also don't have the time to explain validation of arguments, defensive copying, letting this references escape, and calling methods from inside constructors.
     
    Charley Bates
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Norm Radder wrote:Passing a value to a constructor is very basic java.  How did you get to this point in your programming without learning how to use a class constructor?
    Here is a link to the tutorial re constructors:   https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html


    I've been trying to create a constructor,


    If you are having problems with the code, you need to post the code and the error messages or a description of the problem.



    So I made a few changes, but I still can't get the string I need.  Based on the error, it looks like it's passing the null value.  Here's the code for two classes:

    MainActivity -> I need to get the value from "String decrypted1 = d1.encryppass(encrypted);"


    Decrypt2 class -> pass that value to




    The error -> This goes away if I change it to the hardcoded value, so this tells me it's passing the null value.
     
    Norm Radder
    Rancher
    Posts: 4936
    38
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Several problems I see:
    decrypted1 is declared in two places: line 8 and line 41.  The local declaration on line 41 masks the declaration on line 8.  Remove the declaration on line 41 and make it an assignment statement.  See post  Yesterday 4:10 PM

    The value of decrypt1 should be passed in the constructor to the Decrypt2 class.  Once the constructor gets that value it can be used to compute the value of  secretKeySpe.  The existing code  uses a new instance of MainActivity(not the one with the desired value) to access the value of  decrypted1 which will not have a value in the new instance.
     
    Charley Bates
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Norm Radder wrote:Several problems I see:
    decrypted1 is declared in two places: line 8 and line 41.  The local declaration on line 41 masks the declaration on line 8.  Remove the declaration on line 41 and make it an assignment statement.  See post  Yesterday 4:10 PM

    The value of decrypt1 should be passed in the constructor to the Decrypt2 class.  Once the constructor gets that value it can be used to compute the value of  secretKeySpe.  The existing code  uses a new instance of MainActivity(not the one with the desired value) to access the value of  decrypted1 which will not have a value in the new instance.



    I removed line 8, but not sure how to proceed from here.   I understand how basic constructors work, but I think this is beyond my understanding of constructors.  I'm not sure how to build a constructor that will then pull the dynamic value from "d1.encryppass(encrypted)"  This is where the decrypted password is.  I've spent a lot of time researching this, but I couldn't find anything specific to my usecase.
     
    Norm Radder
    Rancher
    Posts: 4936
    38
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    I removed line 8,

    No, line 8 was required to declare decrypt1 at the class level.  What needed to be changed was line 41 so it was only an assignment statement that assigned a value to the variable declared on line 8.

    The purpose of the constructor I described was to accept the value of decrypt1 and save it so that it could be used to compute the value of secretKeySpe.
    The statement that computes secretKeySpe needs to be inside of the constructor so that it can use the passed value of decrypt1.  The declaration of secretKeySpe needs to  be at the class level the same as with decrypt1 in the MainActivity class.
     
    Charley Bates
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Norm Radder wrote:

    I removed line 8,

    No, line 8 was required to declare decrypt1 at the class level.  What needed to be changed was line 41 so it was only an assignment statement that assigned a value to the variable declared on line 8.

    The purpose of the constructor I described was to accept the value of decrypt1 and save it so that it could be used to compute the value of secretKeySpe.
    The statement that computes secretKeySpe needs to be inside of the constructor so that it can use the passed value of decrypt1.  The declaration of secretKeySpe needs to  be at the class level the same as with decrypt1 in the MainActivity class.




    Sorry, that's what I meat.  going on limited sleep :-/

    So I made some changes.  I couldn't completely get rid of line 8, but I did change the code up so hopefully it won't be an issue.  Here's my updated code.  The app crashes on me.  I think it's related to this line:

    Main Activity:



    Decrypt 2 class:



    Based on the error in logcat, I think it may be related to this line:     " public String pin_to_md5;SecretKeySpec secretKeySpe = new SecretKeySpec(this.pin_to_md5.getBytes(), "AES");"


     
    Norm Radder
    Rancher
    Posts: 4936
    38
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The declaration of the variable: secretKeySpe should be left where it is on line 5.  The assignment statement that gives it a value needs to be moved into the constructor so it can use the value of  pinToMd5 that was passed to the constructor.
    When line 5(is that 17 in your code) is executed, pinToMd5 will have a null value: NPE

    Why is a reference to MainActivity passed to the constructor on line 49?  I suggested that the value of decrypted1 be passed to the constructor.
     
    What's brown and sticky? ... a stick. Or a tiny ad.
    The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
    https://www.kickstarter.com/projects/paulwheaton/low-tech
    reply
      Bookmark Topic Watch Topic
    • New Topic