Question

I have got the code to find the IMEI number of the device, But now I want to Encrypt that format, How can I encrypt that ?

Was it helpful?

Solution

Here give the sample for Encrypt Ad Descrypt for String using Cipher

http://www.androidsnippets.com/encryptdecrypt-strings

OTHER TIPS

if you're trying to encrypt the number on the device itself, it's not possible.

if you're trying to encrypt the number you got with your code, there are many ways to do that, try looking at this code snippet: http://www.androidsnippets.com/encryptdecrypt-strings

You could use functions like these:

private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(clear);
        return encrypted;
    }

    private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);
        byte[] decrypted = cipher.doFinal(encrypted);
        return decrypted;
    }

And invoke them like this:

ByteArrayOutputStream baos = new ByteArrayOutputStream();  
bm.compress(Bitmap.CompressFormat.PNG, 100, baos); // bm is the bitmap object   
byte[] b = baos.toByteArray();  

byte[] keyStart = "this is a key".getBytes();
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(keyStart);
kgen.init(128, sr); // 192 and 256 bits may not be available
SecretKey skey = kgen.generateKey();
byte[] key = skey.getEncoded();    

// encrypt
byte[] encryptedData = encrypt(key,b);
// decrypt
byte[] decryptedData = decrypt(key,encryptedData);

Ref from : android encryption/decryption with AES

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top