SmsUtils
public class SmsUtils {
public interface BackUpCallBackMsg{
void before(int count);
void OnBackUpSms(int process);
}
// 短信备份
public static boolean backups(Context context,BackUpCallBackMsg callback) {
FileOutputStream fos = null;
File file;
int i =0;
int process=0;
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
try {
file = new File(Environment.getExternalStorageDirectory(),
"backups.xml");
while(file.exists()){
file = new File(Environment.getExternalStorageDirectory(),
"backups"+i+".xml");
i++;
}
fos = new FileOutputStream(file);
ContentResolver resolver = context.getContentResolver();
Uri uri = Uri.parse("content://sms/");
// 1接收 2 发送
Cursor cursor = resolver.query(uri, new String[] { "address",
"date", "type", "body" }, null, null, null);
int count = cursor.getCount();
XmlSerializer xs = Xml.newSerializer();
xs.setOutput(fos, "utf-8");
xs.startDocument("utf-8", true);
xs.startTag(null, "smss");
xs.attribute(null, "size", count+"");
callback.before(count);
while (cursor.moveToNext()) {
xs.startTag(null, "sms");
xs.startTag(null, "address");
xs.text(cursor.getString(0));
xs.endTag(null, "address");
xs.startTag(null, "date");
xs.text(cursor.getString(1));
xs.endTag(null, "date");
xs.startTag(null, "type");
xs.text(cursor.getString(2));
xs.endTag(null, "type");
xs.startTag(null, "body");
xs.text(Crypto.encrypt("love", cursor.getString(3)));
xs.endTag(null, "body");
xs.endTag(null, "sms");
process++;
callback.OnBackUpSms(process);
}
cursor.close();
xs.endTag(null, "smss");
xs.endDocument();
return true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(fos!=null){
try{
fos.flush();
fos.close();
}catch(IOException e){
}
}
}
} else {
}
return false;
}
}
Crypto.java
public class Crypto {
/**
* Encrypt plain string and encode to Base64
*
* @param seed
* @param plain
* @return
* @throws Exception
*/
public static String encrypt(String seed, String plain) throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] encrypted = encrypt(rawKey, plain.getBytes());
return Base64.encodeToString(encrypted, Base64.DEFAULT);
}
/**
* Decrypt Base64 encoded encrypted string
*
* @param seed
* @param encrypted
* @return
* @throws Exception
*/
public static String decrypt(String seed, String encrypted)
throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] enc = Base64.decode(encrypted.getBytes(), Base64.DEFAULT);
byte[] result = decrypt(rawKey, enc);
return new String(result);
}
private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator keygen = KeyGenerator.getInstance("AES");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(seed);
keygen.init(128, random); // 192 and 256 bits may not be available
SecretKey key = keygen.generateKey();
byte[] raw = key.getEncoded();
return raw;
}
private static byte[] encrypt(byte[] raw, byte[] plain) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
byte[] encrypted = cipher.doFinal(plain);
return encrypted;
}
private static byte[] decrypt(byte[] raw, byte[] encrypted)
throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, keySpec);
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}
}