Simple class for encryption

Search This thread

Jonny

Retired Forum Moderator
Jul 22, 2011
9,293
9,616
Here is a simple class for encrypting strings. As we should all know, encryption is important, especially for things like login forms, personal user data etc. Therefore it is important that if you are storing such information either locally on the device or sending it to a remote server, you encrypt such details.

This class uses the SHA-512 hashing algorithm to convert a string into its raw bit format. This is then converted into a hex string and returned.

There are ways of making this even more secure (by adding salts etc) but this is just meant to be for a starting point ;)

Code:
public class Crypto {
    final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();

    private static String convertToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }

    public static String SHA512(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest md = MessageDigest.getInstance("SHA-512");
        md.update(text.getBytes("iso-8859-1"), 0, text.length());
        byte[] sha512hash = md.digest();
        return convertToHex(sha512hash);
    }
}

Usage:

Code:
String example = "example";
try {
    example = Crypto.SHA512(example);
} catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}
 
Last edited:

Masrepus

Senior Member
Feb 12, 2013
767
99
Thanks a lot, exactly what i have been looking for yesterday, must have been reading my thoughts ;P
Just one question, how to revert the hex to the normal text version again

---------------------------------
Phone : Nexus 4
OS:
Pure KitKat 4.4.2 stock, no root, no mods
---------------------------------

4d 61 73 72 65 70 75 73 20 66 74 77

Gesendet von Tapatalk
 

SimplicityApks

Senior Member
May 26, 2013
354
344
Aachen
Thanks a lot, exactly what i have been looking for yesterday, must have been reading my thoughts ;P
Just one question, how to revert the hex to the normal text version again

The SHA algorithm used here is intended to and can only be used one way. So basically the user enters his password which is encrypted by the method above and the hex string is then saved. Each time the user wants to login, the password he entered is encrypted again. If the hex string matches the saved one the user gains access.

This method is very secure as there is no way to uniquely decode the password from the hex string.
 
  • Like
Reactions: Masrepus

Jonny

Retired Forum Moderator
Jul 22, 2011
9,293
9,616
The SHA algorithm used here is intended to and can only be used one way. So basically the user enters his password which is encrypted by the method above and the hex string is then saved. Each time the user wants to login, the password he entered is encrypted again. If the hex string matches the saved one the user gains access.

This method is very secure as there is no way to uniquely decode the password from the hex string.

^ This. How I use it is for GCM notifications for the app for my school. Users register for push notifications for different year groups (eg year 7, year 8 etc) using their email (mandatory as this is used for some mysql enquiries server side) and real name (optional). Obviously I don't want these showing in plain text format if the website/database is compromised so the information needed to be encrypted before stored in the remote database.

In addition to that I didn't want the plain text information being intercepted whilst being sent to the server so I encrypted it locally using this class then send to the server - it also means that I can still use this to run mysql enquiries on the hashed email string :)

Sent from my HTC One using Tapatalk
 

Masrepus

Senior Member
Feb 12, 2013
767
99
Ah okay i understand

---------------------------------
Phone : Nexus 4
OS:
Pure KitKat 4.4.2 stock, no root, no mods
---------------------------------

4d 61 73 72 65 70 75 73 20 66 74 77

Gesendet von Tapatalk
 

Top Liked Posts

  • There are no posts matching your filters.
  • 5
    Here is a simple class for encrypting strings. As we should all know, encryption is important, especially for things like login forms, personal user data etc. Therefore it is important that if you are storing such information either locally on the device or sending it to a remote server, you encrypt such details.

    This class uses the SHA-512 hashing algorithm to convert a string into its raw bit format. This is then converted into a hex string and returned.

    There are ways of making this even more secure (by adding salts etc) but this is just meant to be for a starting point ;)

    Code:
    public class Crypto {
        final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
    
        private static String convertToHex(byte[] bytes) {
            char[] hexChars = new char[bytes.length * 2];
            for (int j = 0; j < bytes.length; j++) {
                int v = bytes[j] & 0xFF;
                hexChars[j * 2] = hexArray[v >>> 4];
                hexChars[j * 2 + 1] = hexArray[v & 0x0F];
            }
            return new String(hexChars);
        }
    
        public static String SHA512(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
            MessageDigest md = MessageDigest.getInstance("SHA-512");
            md.update(text.getBytes("iso-8859-1"), 0, text.length());
            byte[] sha512hash = md.digest();
            return convertToHex(sha512hash);
        }
    }

    Usage:

    Code:
    String example = "example";
    try {
        example = Crypto.SHA512(example);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    1
    Thanks a lot, exactly what i have been looking for yesterday, must have been reading my thoughts ;P
    Just one question, how to revert the hex to the normal text version again

    The SHA algorithm used here is intended to and can only be used one way. So basically the user enters his password which is encrypted by the method above and the hex string is then saved. Each time the user wants to login, the password he entered is encrypted again. If the hex string matches the saved one the user gains access.

    This method is very secure as there is no way to uniquely decode the password from the hex string.