I have created a android project that fetches text from EditText field and it encrypts the text using AES encryption and returns the value . The project works fine in Java code .
But in android , it gives for javax.xml.bind.DataConvertorType . Its not getting detected . I browsed through articles from net they told to use proguard and make some changes in it .
Well , i am using ADT 19 and could not find proguard.cfg file so i uncommented a line in project.properties
proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
And made changes in proguard-project.txt as seen below
# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
-keep class javax.xml.bind.** { *; }
But its still giving Runtime error as ClassDefinition not found for that DataTypeConvertor File , Please guide
I call the method as
String encryptedText = AesUtil.Encrypt(inputToEncrypt);
I am getting RuntimeError
File 1 AesUtil
package encryption;
public class AesUtil {
private static final String IV = "00000000000000000000000000000000";
private static final String SALT = "00000000000000000000000000000000";
private static final int KEY_SIZE = 128;
private static final int ITERATION_COUNT = 10;
private static final String PASSPHRASE = "companycompnaycompnay";
public static String Encrypt(String PLAIN_TEXT) {
AesUtilHelper util = new AesUtilHelper(KEY_SIZE, ITERATION_COUNT);
String encrypt = util.encrypt(SALT, IV, PASSPHRASE, PLAIN_TEXT);
return encrypt;
}
public static String Decrypt(String CIPHER_TEXT) {
AesUtilHelper util = new AesUtilHelper(KEY_SIZE, ITERATION_COUNT);
String decrypt = util.decrypt(SALT, IV, PASSPHRASE, CIPHER_TEXT);
// System.out.println(decrypt);
return decrypt;
}
public static void main(String args[])
{
String PLAIN_TEXT = "lol";
String CIPHER_TEXT = Encrypt(PLAIN_TEXT);
System.out.println("encrypted text:"+" "+CIPHER_TEXT);
//CIPHER_TEXT = "k3YSx1TilJeP9w34YsYP4g==";
System.out.println("decrypted text"+" "+Decrypt(CIPHER_TEXT));
}
}
File 2 AesUtilHelper
package encryption;
import java.io.UnsupportedEncodingException;
import java.security.SecureRandom;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
public class AesUtilHelper {
private final int keySize;
private final int iterationCount;
private final Cipher cipher;
public AesUtilHelper(int keySize, int iterationCount) {
this.keySize = keySize;
this.iterationCount = iterationCount;
try {
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
}
catch (Exception e) {
throw fail(e);
}
}
public String encrypt(String salt, String iv, String passphrase, String plaintext) {
try {
SecretKey key = generateKey(salt, passphrase);
byte[] encrypted = doFinal(Cipher.ENCRYPT_MODE, key, iv, plaintext.getBytes("UTF-8"));
return base64(encrypted);
}
catch (UnsupportedEncodingException e) {
throw fail(e);
}
}
public String decrypt(String salt, String iv, String passphrase, String ciphertext) {
try {
SecretKey key = generateKey(salt, passphrase);
byte[] decrypted = doFinal(Cipher.DECRYPT_MODE, key, iv, base64(ciphertext));
return new String(decrypted, "UTF-8");
}
catch (UnsupportedEncodingException e) {
throw fail(e);
}
}
private byte[] doFinal(int encryptMode, SecretKey key, String iv, byte[] bytes) {
try {
cipher.init(encryptMode, key, new IvParameterSpec(hex(iv)));
return cipher.doFinal(bytes);
}
catch (Exception e) {
e.printStackTrace();
throw fail(e);
}
}
private SecretKey generateKey(String salt, String passphrase) {
try {
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(passphrase.toCharArray(), hex(salt), iterationCount, keySize);
SecretKey key = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES");
return key;
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String random(int length) {
byte[] salt = new byte[length];
new SecureRandom().nextBytes(salt);
return hex(salt);
}
public static String base64(byte[] bytes) {
return DatatypeConverter.printBase64Binary(bytes);
}
public static byte[] base64(String str) {
return DatatypeConverter.parseBase64Binary(str);
}
public static String hex(byte[] bytes) {
return DatatypeConverter.printHexBinary(bytes);
}
public static byte[] hex(String str) {
return DatatypeConverter.parseHexBinary(str);
}
private IllegalStateException fail(Exception e) {
return new IllegalStateException(e);
}
}
0 comments:
Post a Comment