积累的一些代码片段

  • 倒计时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");// 创建密码器
    //            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;
    //    }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,981评论 19 139
  • 1、不安全的随机数生成,在CSRF TOKEN生成、password reset token生成等,会造成toke...
    nightmare丿阅读 3,752评论 0 1
  • 概述 之前一直对加密相关的算法知之甚少,只知道类似DES、RSA等加密算法能对数据传输进行加密,且各种加密算法各有...
    Henryzhu阅读 3,055评论 0 14
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,537评论 0 17
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,767评论 18 399