开发者

Encrypting the user credentials using SHA1 algorithm

I am creating a blackberry application which sends the request to the server. So authe开发者_开发百科ntication of the user is required. So for doing this i want to encrypt UserID and password using SHA1 in blackberry. The encrypted data which is made using SHA1 algorithm on UserID and password is then passed to the server.

My problem is how do i implement this. Can someone give the sample code for implementing this in blackberry.


SHA1 is not an encryption algorithm. It is hash-function.

http://en.wikipedia.org/wiki/SHA1

If you are talking about Basic Authentication, then you need to use Base64 algorithm to hash username and password.

Here is discussed topic about this issue: HTTP authentication in J2ME


add this class

public class Sha1 {

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) {
    SHA1Digest sha1Digest = new SHA1Digest();
    sha1Digest.update(text.getBytes(), 0, text.length());
    byte[] hashValBytes = new byte[sha1Digest.getDigestLength()];
    hashValBytes = sha1Digest.getDigest();
    return convertToHex(hashValBytes);
}
}

then on your code , Write

 sha1 = Sha1.SHA1(email);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜