Frage

I want to encrypt and decrypt the request and response of the web service(KSOAP or JSON) using SHA512 algorithm with private and public keys in android.

Does anyone have an idea on this. I don't have much experience on this. So please excuse me if i am wrong.

i guess the below class is used for encrypting a string. I want to know how to decrypt the string. And also i want to know how to use the private/public keys in this.

public class SHA2Demo {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    String message = "test";

    MessageDigest messageDigest = null;
    try {
        messageDigest = MessageDigest.getInstance("SHA-512");
    } catch (NoSuchAlgorithmException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        messageDigest.update(message.getBytes("UTF-16BE"));
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    byte[] digest = messageDigest.digest();

    StringBuffer digestInHex = new StringBuffer();

    for (int i = 0, l = digest.length; i < l; i++) {
        // Preserve the bit representation when casting to integer.
        int intRep = digest[i] & 0xFF;
        // Add leading zero if value is less than 0x10.
        if (intRep < 0x10)  digestInHex.append('\u0030');
        // Convert value to hex.
        digestInHex.append(Integer.toHexString(intRep));
    }

    System.out.println(digestInHex.toString());

}

}

Thanks in advance.

War es hilfreich?

Lösung 2

Sha512 is a hashing algorithm. You can NOT de-hash the response once it is hashed. That's what hash is for.

RSA or AES is what you might be looking for. Other things you may want to consider is Transport level security using SSL.

Andere Tipps

Sha512 is a hash, not an encryption. You cannot encrypt anything with it. Use something like RSA or elliptic curves.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top