短信备份-加密

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;
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值