美文网首页
积累的一些代码片段

积累的一些代码片段

作者: 依然爱幽默 | 来源:发表于2016-11-02 10:07 被阅读0次
  • 倒计时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;
    //    }
    }

相关文章

网友评论

      本文标题:积累的一些代码片段

      本文链接:https://www.haomeiwen.com/subject/mrjvuttx.html