I'm developing Android App for a website which is Prestashop base.
When I'm signing in the user with email, I receive following value for Password field.
"passwd": "$2y$10$UwXekiYUoeH7K8rpi9YxKerHxHQZacMSIG3VKseVQ2fjlGlFRfN4W",
All I want to do is, just compare this value with a String(value from password EditText).
After successful comparison, I'll be able to Login the user.
Please help me! how can encrypt String value, to compare with above value?
(or is there any technique to decrypt above password value?)
You can use String1.equals(String2) in a if else statement to get this done.
String1 = received password value;
String2 = edittext.getText().toString; //value from edit text.
if(String1.equals(String2))
{
//Login sucessfull
}
else
{
//Failed
}
Decoding:
String password = "$2y$10$UwXekiYUoeH7K8rpi9YxKerHxHQZacMSIG3VKseVQ2fjlGlFRfN4W";
byte[] tmp2 = Base64.decode(password);
String val2 = new String(tmp2, "UTF-8"); // here you will get the decoded value.
Related
I need to send encoded username and Password through request body.
Here I'm sending username and password in the format of JSON object.
How to do it?
Thanks in advance.
Try
Encode
String encodedUsername = URLEncoder.encode(username);
String encodedPassword = URLEncoder.encode(password);
Decode
String decodedUsername = URLDecoder.decode(encodedUsername);
String decodedPassword = URLDecoder.decode(encodedPassword);
A simple Google search would've done the trick...
URLEncoder should be the way to go. You only need to keep in mind to
encode only the individual query string parameter name and/or value,
not the entire URL, for sure not the query string parameter separator
character & nor the parameter name-value separator character =.
String q = "random word £500 bank $";
String url = "http://example.com/query?q=" + URLEncoder.encode(q, "UTF-8");
...
Source:
Java URL encoding of query string parameters
I'm getting the password-edit-text value but I don't get what I want.
my code in getting the value:
String username = "", password = "";
username = ((EditText)findViewById(R.id.username_pet)).toString();
password = ((EditText)findViewById(R.id.password_pet)).toString();
but when I put the result to Toast I don't get it right. What I get is this
error.
any help will be much appreciated. Thanks!
Your code would return you the id of your EditText yet what you want is the Text.
Try this
Replace this by :
username = ((EditText)findViewById(R.id.username_pet)).toString();
password = ((EditText)findViewById(R.id.password_pet)).toString();
this
username = ((EditText)findViewById(R.id.username_pet)).getText();
password = ((EditText)findViewById(R.id.password_pet)).getText();
Hope this helps
username = ((EditText)findViewById(R.id.username_pet)).getText().toString();
password = ((EditText)findViewById(R.id.password_pet)).getText().toString();
This is happening because you are attempting to convert to string before getting the text of the input field. The app is doing exactly what you are telling it to do: Getting the EditText View and converting the View to a string, just not the text within the view. If that makes sense.
Try
EditText userName = (EditText) findViewById(R.id.username_pet);
String userNameFromEditText = (String) username.getText().toString();
Then toast the value of userNameFromEditText.
boolean isLogin = false;
after user signed in successfully to my app:
isLogin = true;
String user_email_address = "example1#gmail.com";
now i save user_email_address to my database server by request POST: https://example.com/insert_email.php?email=example1#gmail.com
user tables
-------id----------user_email_address-----------------
1 example1#gmail.com
2 example2#gmail.com
... ..................
------------------------------------------------------
now, i want only logined users to insert and send comment for posts by form, in my android app isLogin = true, So:
String user_comment = editText.getText().toString();
https://example.com/insert_comment.php?email=example1#gmail.com&comment=user_comment
so on server:
INSERT TO user_comment_table
user_email, user_comment
VALUES ( user_email, user_comment )
so
comment tables
-------user_email----------user_comment-----------------
example1#gmail.com comment ......
example1#gmail.com comment ......
... ..................
--------------------------------------------------------
Now, i need authorization and Authentication for this:
https://example.com/insert_comment.php?email=example1#gmail.com&comment=user_comment
You should read this post (authentication on your Android app/user to your server). This should answer the questions you have.
http://android-developers.blogspot.com/2016/05/improving-security-and-user-experience.html
I have a register form. i need to save the content in db. my data are saved successfully. but when i use space or some special characters my datas are not stored in db. what should i use to store the special characters and space in db,
if (Name.getText().toString().equals("")) {
Name.setError("Enter your name");
Name.requestFocus();
} else if (Dob.getText().toString().equals("")) {
Dob.setError("Enter Date of Birth");
Dob.requestFocus();
} else if (Collegeid.getText().toString().equals("")) {
Collegeid.setError("Enter your CollegeID");
Collegeid.requestFocus();
}
else {
String sname = Name.getText().toString();
String dob = Dob.getText().toString().trim();
String cid = Collegeid.getText().toString().trim();
}
Refer the screenshot above
Try to use [] brackets in the SQLite statements. It allow to you to add anything what you want.
I am developing an app, it has a login page. I need to store the login credentials. Can it be in my strings.xml file? Because I have heard that Strings.xml can not be modified at run time. So where can I store data i.e. User details or application details?
You can store login information in SharedPreference or SqliteDatabase.
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("username", YOUR_USERNAME);
editor.putString("password", YOUR_PASSWORD);
editor.commit();
For retrieving Login information
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
String username = prefs.getString("username", null);
String password = prefs.getString("password", null);
If you need more security you can use SQLCipher using SqliteDatabase
Please go through this link.
Use Shared Preferences. Like so:
Create these methods for use, or just use the content inside of the methods whenever you want:
public String getUserName()
{
SharedPreferences sp = getSharedPreferences("userNameAndPassword", 0);
String str = sp.getString("userName","no userName created");
return str;
}
public String getPassword()
{
SharedPreferences sp = getSharedPreferences("userNameAndPassword", 0);
String str = sp.getString("password","no password created");
return str;
}
public void writeToUserNameAndPassword(String userName, String password)
{
SharedPreferences.Editor pref =
getSharedPreferences("userNameAndPassword",0).edit();
pref.putString("userName", userName);
pref.putString("password", password);
pref.commit();
}
You could call them like this:
// their userName if "foo" and their password is "bar"
writeToUserNameAndPassword("foo", "bar");
if (getUserName().equals(inputUserName) && getPassword.equals(inputPassword))
{
// they have the right userName and password
}
else if (getUserName().equals("no userName created")
&& getPassword().equals("no password created"))
{
// these preference Strings for their userName/password have both not been created
}
else if (getUserName().equals("no userName created"))
{
// this preference String for their userName has not been created,
// but the password has been
}
else if (getPassword().equals("no password created"))
{
// this preference String for their password has not been created,
// but the userName has been
}
else
{
// they entered the wrong userName and/or password
}
Some explanation (if needed):
"password" and "userName" are the 'key' Strings in the preference. So you reference those keys to obtain the String you put in there. It is a reference name for the String you put.
"userNameAndPassword" is the preference name. You use the preference name, "userNameAndPassword", to reference the preference you want to access.
"no password created" and "no userName created" are the Strings that the getString method will return if the preference doesn't have a String referenced to by "password" or "userName", meaning that it hasn't been created.
Another way to put it: they are the default values of the reference String. So if nothing has been put their instead, the method will return the default values. You have to set the default values.
So, for example, if no "password" String has been put into the "userNameAndPassword" preference (written to using putString), then the getPassword() method will return "no password created".
As #Armit mentioned before, you can store the data in the SharedPreferences. Just be aware that this gets stored in a simple XML file that can be seen and modified with an editor. You should at least encrypt it or, better, not save it at all. Usually, you log in to a server or site and then save only the return token. You only use the token to connect again and you don't have to save the password in plain text.
In simple words YOU CAN'T STORE OR CHANGE the content of strings.xml
But yes as User #amit said you can
store these values in Shared Preferences
Or You can Use SQLite Database to store what ever you want learn sqlite
For example
for setting the Value
SharedPreferences.Editor prefEditor = getPreferences(MODE_PRIVATE).edit();
prefEditor.putInt(LAUNCH_COUNT, 1); // you can have multiple put (values)
prefEditor.commit();
prefEditor.apply();
For getting the value
SharedPreferences sp = getPreferences(MODE_PRIVATE);
int launchCount = sp.getInt(LAUNCH_COUNT, -1);