積累的一些代碼片段

  • 倒計(jì)時(shí)button
   public class CountdownUtil extends CountDownTimer{
       private Context context;
       private Button button;

       public CountdownUtil(long millisInFuture, long countDownInterval, Context context, Button button) {
           super(millisInFuture, countDownInterval);
           this.context = context;
           this.button = button;
       }

       @Override
       public void onTick(long millisUntilFinished) {
           if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
               button.setBackground(context.getDrawable(R.drawable.frame_button_gray));
           }else{
               button.setBackgroundColor(context.getResources().getColor(R.color.gray));
           }
           button.setClickable(false);
           String string = (millisUntilFinished / 1000) + "秒后重新獲取";
           button.setText(string);
       }

       @Override
       public void onFinish() {
           button.setText((context.getResources().getString(R.string.register_sendCode)));
           if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
               button.setBackground(context.getDrawable(R.drawable.frame_button));
           }else{
               button.setBackgroundColor(ContextCompat.getColor(context, R.color.red_text));
           }
           button.setClickable(true);
       }
   }
  • AES加密
    public class AESCrypt {
        private final Cipher cipher;
        private final SecretKeySpec key;
        private AlgorithmParameterSpec spec;
        public static final String SEED_16_CHARACTER = Constant.SECRET_KEY;

        public AESCrypt() throws Exception {
            // hash password with SHA-256 and crop the output to 128-bit for key
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            digest.update(SEED_16_CHARACTER.getBytes("UTF-8"));
            byte[] keyBytes = new byte[32];
            System.arraycopy(digest.digest(), 0, keyBytes, 0, keyBytes.length);
            cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            key = new SecretKeySpec(keyBytes, "AES");
            spec = getIV();
        }

        public AlgorithmParameterSpec getIV() {
            byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,};
            IvParameterSpec ivParameterSpec;
            ivParameterSpec = new IvParameterSpec(iv);
            return ivParameterSpec;
        }

        public String encrypt(String plainText) throws Exception {
            cipher.init(Cipher.ENCRYPT_MODE, key, spec);
            byte[] encrypted = cipher.doFinal(plainText.getBytes("UTF-8"));
            return new String(Base64.encode(encrypted, Base64.DEFAULT), "UTF-8");
        }

        public String decrypt(String cryptedText) throws Exception {
            cipher.init(Cipher.DECRYPT_MODE, key, spec);
            byte[] bytes = Base64.decode(cryptedText, Base64.DEFAULT);
            byte[] decrypted = cipher.doFinal(bytes);
            return new String(decrypted, "UTF-8");
        }

    //    public static byte[] encrypt(String content, String password) {
    //        try {
    //            KeyGenerator kgen = KeyGenerator.getInstance("AES");
    //            kgen.init(128, new SecureRandom(password.getBytes()));
    //            SecretKey secretKey = kgen.generateKey();
    //            byte[] enCodeFormat = secretKey.getEncoded();
    //            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
    //            Cipher cipher = Cipher.getInstance("AES");// 創(chuàng)建密碼器
    //            byte[] byteContent = content.getBytes("utf-8");
    //            cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
    //            byte[] result = cipher.doFinal(byteContent);
    //            return result; // 加密
    //        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
    //                | UnsupportedEncodingException | IllegalBlockSizeException | BadPaddingException e) {
    //            e.printStackTrace();
    //        }
    //        return null;
    //    }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,915評(píng)論 18 139
  • 1、不安全的隨機(jī)數(shù)生成,在CSRF TOKEN生成、password reset token生成等,會(huì)造成toke...
    nightmare丿閱讀 3,748評(píng)論 0 1
  • 概述 之前一直對(duì)加密相關(guān)的算法知之甚少,只知道類(lèi)似DES、RSA等加密算法能對(duì)數(shù)據(jù)傳輸進(jìn)行加密,且各種加密算法各有...
    Henryzhu閱讀 3,050評(píng)論 0 14
  • ¥開(kāi)啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開(kāi)一個(gè)線程,因...
    小菜c閱讀 6,511評(píng)論 0 17
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語(yǔ)法,類(lèi)相關(guān)的語(yǔ)法,內(nèi)部類(lèi)的語(yǔ)法,繼承相關(guān)的語(yǔ)法,異常的語(yǔ)法,線程的語(yǔ)...
    子非魚(yú)_t_閱讀 31,765評(píng)論 18 399