java AES、SHA-1加密函数

来源:互联网 发布:javac 运行java文件 编辑:程序博客网 时间:2024/05/16 15:18
public static String AES(String content, String key)  {            if (key == null) {                System.out.print("Key为空null");                return null;            }            // 判断Key是否为16位            if (key.length() != 16) {                System.out.print("Key长度不是16位");                return null;            }            byte[] raw = key.getBytes();            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");            Cipher cipher = null;            try {                cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");            } catch (NoSuchAlgorithmException e) {                // TODO Auto-generated catch block                e.printStackTrace();            } catch (NoSuchPaddingException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }            // "算法/模式/补码方式"             IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());             try {                cipher.init(Cipher.ENCRYPT_MODE,skeySpec,iv);            } catch (InvalidKeyException e) {                // TODO Auto-generated catch block                e.printStackTrace();            } catch (InvalidAlgorithmParameterException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }            byte[] encrypted;            String msg=null;            try {                encrypted = cipher.doFinal(content.getBytes());                int totalLength = iv.getIV().length + encrypted.length;                byte[] combine = new byte[totalLength];                System.arraycopy(iv.getIV(), 0, combine, 0, iv.getIV().length);                System.arraycopy(encrypted, 0, combine, iv.getIV().length,encrypted.length);                String baseEncoder = new BASE64Encoder().encode(combine);// 此处使用BASE64做转码                try {                    msg=URLEncoder.encode(baseEncoder, "UTF-8");                } catch (UnsupportedEncodingException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            } catch (IllegalBlockSizeException e) {                // TODO Auto-generated catch block                e.printStackTrace();            } catch (BadPaddingException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }                return msg;        }
/* * SHA-1加密 */     public String SHA1(String decript) {            try {                MessageDigest digest = java.security.MessageDigest                        .getInstance("SHA-1");                digest.update(decript.getBytes());                byte messageDigest[] = digest.digest();                // Create Hex String                StringBuffer hexString = new StringBuffer();                // 字节数组转换为 十六进制 数                for (int i = 0; i < messageDigest.length; i++) {                    String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);                    if (shaHex.length() < 2) {                        hexString.append(0);                    }                    hexString.append(shaHex);                }                return hexString.toString();            } catch (NoSuchAlgorithmException e) {                e.printStackTrace();            }            return "";        }     public static String bytetoString(byte[] digest) {        String str = "";        String tempStr = "";        for (int i = 1; i < digest.length; i++) {            tempStr = (Integer.toHexString(digest[i] & 0xff));            if (tempStr.length() == 1) {                str = str + "0" + tempStr;            } else {                str = str + tempStr;            }        }        return str.toLowerCase();    }
0 0
原创粉丝点击