I'm sorry but I need help for building an Android app that will have a function to generate MD5 hash from a file and a textfile. Can you help me, I don't very understand on Android development, but my teacher keep pushing me (sorry about that).
Thank you so much for your help.
Hari (Indonesia)
MD5 Hash function:
public static final String md5(final String s)
{
try
{
MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
digest.update(s.getBytes());
byte messageDigest[] = digest.digest();
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < messageDigest.length; i++)
{
String h = Integer.toHexString(0xFF & messageDigest[i]);
while (h.length() < 2)
h = "0" + h;
hexString.append(h);
}
return hexString.toString();
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
return "";
}
Related
I need provide hashing password with sha1 and Base64 like next:
base64(sha1(password))
here is what i tried for:
private static String convertToHex(byte[] data) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < data.length; i++) {
int halfbyte = (data[i] >>> 4) & 0x0F;
int two_halfs = 0;
do {
if ((0 <= halfbyte) && (halfbyte <= 9)) {
buf.append((char) ('0' + halfbyte));
}
else {
buf.append((char) ('a' + (halfbyte - 10)));
}
halfbyte = data[i] & 0x0F;
} while(two_halfs++ < 1);
}
return buf.toString();
}
public static String SHA1(String password) throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(password.getBytes("iso-8859-1"), 0, password.length());
byte[] sha1hash = md.digest();
return convertToHex(sha1hash);
}
and than :
String encodedPass = Base64.encodeToString(password.getBytes(), Base64.DEFAULT);
but this doesn't work correctly...
Maybe there some mistake or some much easier way to do it?
the problem was next:
convertToHex(sha1hash) - return string, but for correct work of Base64 I was needed to put byte [] here:
Base64.encodeToString(byte[], Base64.DEFAULT);
so solution is pretty simple:
public static String encodePassword(String password) throws NoSuchAlgorithmException, UnsupportedEncodingException {
String result;
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(password.getBytes("iso-8859-1"), 0, password.length());
byte[] sha1hash = md.digest();
result = Base64.encodeToString(sha1hash, Base64.DEFAULT);
result = result.substring(0, result.length()-1);
return result;
}
im using this code to encrypt my password......
private static final String md5(final String password) {
try {
MessageDigest digest = java.security.MessageDigest
.getInstance("MD5");
digest.update(password.getBytes());
byte messageDigest[] = digest.digest();
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < messageDigest.length; i++) {
String h = Integer.toHexString(0xFF & messageDigest[i]);
while (h.length() < 2)
h = "0" + h;
hexString.append(h);
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return "";
}
how can i add a (Secretkey) so i can send the value to a .net
What about using AES encryption?
You can find examples yow to use it at What are best practices for using AES encryption in Android?
I am trying to convert a user fed password into a md5 hash. The following code smippet was taken from a weblog. But it is showing error in eclipse (android development):
MessageDigest cannot be resolved.
public class MainActivity extends Activity {
private String md5(String in) {
MessageDigest digest;
try {
digest = MessageDisgest.getInstance("MD5");
digest.reset();
digest.update(in.getBytes());
byte[] a = digest.digest();
int len = a.length;
StringBuilder sb = new StringBuilder(len << 1);
for(int i=0;i<len;i++) {
sb.append(Character.forDigit((a[i] & 0xf0) >> 4, 16));
sb.append(Character.forDigit(a[i] & 0x0f, 16));
}
return sb.toString();
} catch (NoSuchAlgorithmException e) { e.printStackTrace();}
return null;
}
I am not able to find any error. What could be wrong?
Try to change MessageDisgest to MessageDigest
I'm using ormlite for my android-app. But now i have a problem. I have a class user with an attribute password. I want to encrypt/decrypt it. But i haven't found a solution, which works with ormlite. Has anyone an idea? I already found, that encryption is not supported by ormlite, but im searching for an other solution, which works with ormlite.
Is it possible to override den CRUD operations in the Dao? (I'm new to android, sorry if its a stupid question)
Thanks for help
Well you wouldn't specify a cleartext password field to be stored in the DB but only store the encrypted password (or even better only the password hash, see Best way to store password in database).
So you would have something like
class User {
#DatabaseField(canBeNull = false)
private String passwordHash;
public void setPassword(String password) {
this.passwordHash = hashPassword(password);
}
public boolean isPasswordCorrect(String givenPassword) {
return TextUtils.equals(hasPassword(givenPassword), passwordHash);
}
private String hashPassword(String password) {
return AeSimpleSHA1.SHA1(password);
}
}
public class AeSimpleSHA1 {
private static String convertToHex(byte[] data) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < data.length; i++) {
int halfbyte = (data[i] >>> 4) & 0x0F;
int two_halfs = 0;
do {
if ((0 <= halfbyte) && (halfbyte <= 9))
buf.append((char) ('0' + halfbyte));
else
buf.append((char) ('a' + (halfbyte - 10)));
halfbyte = data[i] & 0x0F;
} while(two_halfs++ < 1);
}
return buf.toString();
}
public static String SHA1(String text)
throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest md;
md = MessageDigest.getInstance("SHA-1");
byte[] sha1hash = new byte[40];
md.update(text.getBytes("iso-8859-1"), 0, text.length());
sha1hash = md.digest();
return convertToHex(sha1hash);
}
}
SHA1 stuff shamelessly copied from How to SHA1 hash a string in Android?.
I am making an app in which i have to encrypt the data and send it to server and in that case while converting data to int form i am getting number format exception .My code is as follows:
public String encryptData(String key, String s)
{
try{
System.out.println("encrypt");
// byte[] utf8 = data.getBytes("UTF8");
byte[] fin=s.getBytes();
System.out.println("encrypt2");
// byte[] enc = ecipher.doFinal(fin);
System.out.println("encrypt3");
}catch (Exception e) {
System.out.println(e);
}
System.out.println("1");
int keyLen = key.length();
// int dataLen = Convert.ToInt16(data.length());
System.out.println("2");
Integer dataLen=Integer.parseInt(s); // **This line is giving exception**
System.out.println("3");
char chData;
char chKey;
char[] data1 = s.toCharArray();
char[] key1 = key.toCharArray();
System.out.println("4");
StringBuilder encryptedData = new StringBuilder();
for (int i = 0; i < dataLen; i++)
{
chData = data1[i];
for (int j = 0; j < keyLen; j++)
{
chKey = key1[j];
chData = (char)(chData ^ chKey);
}
encryptedData.append(chData);
}
return (encryptedData.toString());
}
and My xml is :
xml="<?xml version='1.0' encoding='utf-8'?>" + "<admin_auth_req><user_name>" +username+ "</user_name>" + "<password>" +PWOrd+ "</password></admin_auth_req>";
Exception is:
java.lang.NumberFormatException: unable to parse '<?xml version='1.0' encoding='utf-8'?><admin_auth_req><user_name>newuser</user_name><password>tester</password></admin_auth_req>' as integer
`
You are triying to convert a String value which is not a number representation into String. What you really should do is to find the length of the String. So change this line:
Integer dataLen=Integer.parseInt(s);
to:
int dataLen = s.length();
You are trying to parse this string as Integer???
<?xml version='1.0' encoding='utf-8'?><admin_auth_req><user_name>newuser</user_name><password>tester</password></admin_auth_req>
What did you expect?