Android常用整理

SharePreferance

 SharedPreferences sp = getSharedPreferences("app", MODE_PRIVATE);
        // 保存配置到 SharedPreferences
        SharedPreferences.Editor editor = sp.edit();

        // 添加內容到存儲區
        editor.putString("name", mTxtName.getText().toString());

        editor.putString("pass", mTxtPass.getText().toString());

        // Editor 必須要 提交 可以使用commit() 或者 apply() (API 9以上)

        editor.apply();
SharedPreferences sp =getSharedPreferences("app",MODE_PRIVATE);

            String name = sp.getString("name", null);
            String pass = sp.getString("pass", null);

機型適配:

  1. 合理使用Wrap_content ,match_parent
  2. 儘可能使用RelativiLayout
  3. 針對不同的機型,使用不同的佈局文件(Use Size Qualifiers):
    res/layout/main.xml
    res/layout-large/main.xml
  4. 儘量使用點9圖片(Use Nine-patch Bitmaps)

px:即像素,1px代表屏幕上一個物理的像素點;px單位不被建議使用,因爲同樣100px的圖片,在不同手機上顯示的實際大小可能不同,偶爾用到px的情況,是需要畫1像素表格線或陰影線的時候,用其他單位如dp會顯得模糊。
dp也可寫爲dip,即density-independent pixel。你可以想象dp更類似一個物理尺寸,比如一張寬和高均爲100dp的圖片在320×480和480×800的手機上“看起來”一樣大。而實際上,它們的像素值並不一樣。dp正是這樣一個尺寸,不管這個屏幕的密度是多少,屏幕上相同dp大小的元素看起來始終差不多大。
sp:sp和dp很類似但唯一的區別是,Android系統允許用戶自定義文字尺寸大小(小、正常、大、超大等等),所以目前主流應用字體大小已經改用dp,不用sp,省去用戶手動調整字體適配的麻煩。
這裏寫圖片描述

加密解密:

package mobi.vhly.cryptodemo;

/**
 * Created by vhly[FR].
 * <p>
 * Author: vhly[FR]
 * Email: [email protected]
 * Date: 2016/10/19
 */

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * 常用的加密算法
 */
public final class CryptUtil {

    private CryptUtil(){}

    ///////////////////////////////////////////////////////////////////////////
    // DES
    ///////////////////////////////////////////////////////////////////////////

    /**
     * DES 加密算法
     * @param data 原始數據
     * @param key 密碼,必須是8個字節
     * @return byte[] 經過加密之後的內容
     */
    public static byte[] desEncrypt(byte[] data, byte[] key){
        byte[] ret = null;
        if (data != null && key != null) {
            if(data.length > 0 && key.length == 8){
                // 1. 使用 Cipher 引擎 來初始化 加密,並且設置密碼
                try {
                    Cipher cipher = Cipher.getInstance("DES");

                    // 1.1 DESKeySpec 用於描述DES的密碼
                    DESKeySpec spec = new DESKeySpec(key);
                    // 1.2 使用 SecretKeyFactory 生成 Key對象
                    SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
                    SecretKey sk = factory.generateSecret(spec);

                    // 1.3 初始化 Cipher 爲加密操作,並且指定密鑰
                    cipher.init(Cipher.ENCRYPT_MODE, sk);

                    // 2. 加密數據
                    ret = cipher.doFinal(data);

                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (InvalidKeySpecException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                }
            }
        }
        return ret;
    }

    /**
     * DES 解密算法
     * @param data 原始數據
     * @param key 密碼,必須是8個字節
     * @return byte[] 經過解密之後的內容
     */
    public static byte[] desDecrypt(byte[] data, byte[] key){
        byte[] ret = null;
        if (data != null && key != null) {
            if(data.length > 0 && key.length == 8){
                // 1. 使用 Cipher 引擎 來初始化 解密,並且設置密碼
                try {
                    Cipher cipher = Cipher.getInstance("DES");

                    // 1.1 DESKeySpec 用於描述DES的密碼
                    DESKeySpec spec = new DESKeySpec(key);
                    // 1.2 使用 SecretKeyFactory 生成 Key對象
                    SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
                    SecretKey sk = factory.generateSecret(spec);

                    // 1.3 初始化 Cipher 爲解密操作,並且指定密鑰
                    cipher.init(Cipher.DECRYPT_MODE, sk);

                    // 2. 解密數據
                    ret = cipher.doFinal(data);

                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (InvalidKeySpecException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                }
            }
        }
        return ret;
    }

    ///////////////////////////////////////////////////////////////////////////
    // AES 方式1
    ///////////////////////////////////////////////////////////////////////////

    public static byte[] aesEncryptSimple(byte[] data, byte[] key){
        byte[] ret = null;
        if (data != null && key != null) {
            if(data.length > 0 && key.length == 16){
                // AES 128bit = 16bytes
                try {
                    Cipher cipher = Cipher.getInstance("AES");
                    SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
                    cipher.init(Cipher.ENCRYPT_MODE, keySpec);
                    ret = cipher.doFinal(data);
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                }
            }
        }
        return ret;
    }
    public static byte[] aesDecryptSimple(byte[] data, byte[] key){
        byte[] ret = null;
        if (data != null && key != null) {
            if(data.length > 0 && key.length == 16){
                // AES 128bit = 16bytes
                try {
                    Cipher cipher = Cipher.getInstance("AES");
                    SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
                    cipher.init(Cipher.DECRYPT_MODE, keySpec);
                    ret = cipher.doFinal(data);
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                }
            }
        }
        return ret;
    }

    ///////////////////////////////////////////////////////////////////////////
    // AES 方式2 使用兩套密碼
    ///////////////////////////////////////////////////////////////////////////

    /**
     * 使用兩套密碼的加密,強度更高
     * @param data 數據
     * @param key 第一個密碼
     * @param ivData 第二個密碼
     * @return byte[]
     */
    public static byte[] aesEncryptWithIv(byte[] data, byte[] key, byte[] ivData){
        byte[] ret = null;
        if (data != null && key != null && ivData != null) {
            if(data.length > 0 && key.length == 16 && ivData.length == 16){
                // 使用兩套密碼的,算法需要寫成 AES/算法模式/填充模式
                try {
                    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
                    // 準備第一套密碼
                    SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
                    // 準備第二套密碼
                    IvParameterSpec iv = new IvParameterSpec(ivData);
                    // 設置兩套密碼的初始化
                    cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv);
                    cipher.update(data);
                    ret = cipher.doFinal();
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidAlgorithmParameterException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                }
            }
        }
        return ret;
    }

    public static byte[] aesDecryptWithIv(byte[] data, byte[] key, byte[] ivData){
        byte[] ret = null;
        if (data != null && key != null && ivData != null) {
            if(data.length > 0 && key.length == 16 && ivData.length == 16){
                // 使用兩套密碼的,算法需要寫成 AES/算法模式/填充模式
                try {
                    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
                    // 準備第一套密碼
                    SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
                    // 準備第二套密碼
                    IvParameterSpec iv = new IvParameterSpec(ivData);
                    // 設置兩套密碼的初始化
                    cipher.init(Cipher.DECRYPT_MODE, keySpec, iv);
                    cipher.update(data);
                    ret = cipher.doFinal();
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidAlgorithmParameterException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                }
            }
        }
        return ret;
    }

    ///////////////////////////////////////////////////////////////////////////
    // RSA
    ///////////////////////////////////////////////////////////////////////////

    // 1. 生成密鑰對 公鑰和私鑰

    /**
     *
     * @param bits 必須在 1024,2048
     * @return
     */
    public static KeyPair generateRsaKey(int bits){
        KeyPair ret = null;
        try {
            KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");

            kpg.initialize(bits);

            ret = kpg.generateKeyPair();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * RSA加密,使用公鑰加密,那麼必須使用私鑰解密
     *         使用私鑰機密,那麼必須使用公鑰解密
     * @param data
     * @param key
     * @return
     */
    public static byte[] rsaEncrypt(byte[] data, Key key){
        byte[] ret = null;
        if (data != null && data.length > 0 && key != null) {
            try {
                Cipher cipher = Cipher.getInstance("RSA");
                cipher.init(Cipher.ENCRYPT_MODE, key);
                ret = cipher.doFinal(data);
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (NoSuchPaddingException e) {
                e.printStackTrace();
            } catch (InvalidKeyException e) {
                e.printStackTrace();
            } catch (BadPaddingException e) {
                e.printStackTrace();
            } catch (IllegalBlockSizeException e) {
                e.printStackTrace();
            }
        }
        return ret;
    }

    public static byte[] rsaDecrypt(byte[] data, Key key){
        byte[] ret = null;
        if (data != null && data.length > 0 && key != null) {
            try {
                Cipher cipher = Cipher.getInstance("RSA");
                cipher.init(Cipher.DECRYPT_MODE, key);
                ret = cipher.doFinal(data);
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (NoSuchPaddingException e) {
                e.printStackTrace();
            } catch (InvalidKeyException e) {
                e.printStackTrace();
            } catch (BadPaddingException e) {
                e.printStackTrace();
            } catch (IllegalBlockSizeException e) {
                e.printStackTrace();
            }
        }
        return ret;
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章