日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內(nèi)最全I(xiàn)T社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁 > php開源 > 綜合技術(shù) > [置頂] Android/Java中的常用簽名算法

[置頂] Android/Java中的常用簽名算法

來源:程序員人生   發(fā)布時間:2016-11-16 08:43:05 閱讀次數(shù):2642次

Android/Java中經(jīng)常使用的簽名算法實現(xiàn):

(包括BASE64、MD5、SHA1、HMAC_SHA1、AES、RSA等)

package com.helloWorld; import java.security.KeyFactory; import java.security.MessageDigest; import java.security.PrivateKey; import java.security.PublicKey; import java.security.SecureRandom; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.Locale; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import android.util.Base64; /** * 加解密工具類 * @author helloWorld * */ public class EncryptUtils { /** * Hex字符 */ public static final String HEX_DIGITS = "0123456789ABCDEF"; /** * UTF⑻ */ public static final String UTF_8 = "UTF⑻"; /** * base64編碼 * @param bytes * @return */ public static String base64Encode(byte[] bytes) { return Base64.encodeToString(bytes, Base64.NO_WRAP); } /** * base64解碼 * @param base64Str * @return * @throws Exception */ public static byte[] base64Decode(String base64Str) throws Exception { return Base64.decode(base64Str, Base64.NO_WRAP); } /** * 字節(jié)數(shù)組轉(zhuǎn)成16進(jìn)制字符串 * @param bytes * @return */ public static String bytesToHexString(byte[] bytes) { if (bytes == null || bytes.length <= 0) { return null; } StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < bytes.length; ++i) { int v = bytes[i] & 0xff; String hv = Integer.toHexString(v); if (hv.length() < 2) { stringBuilder.append(0); } stringBuilder.append(hv); } return stringBuilder.toString(); } /** * 16進(jìn)制字符串轉(zhuǎn)換成字節(jié)數(shù)組 * @param hexString * @return */ public static byte[] hexStringToBytes(String hexString) { if (StringUtils.isBlank(hexString)) { return null; } char[] hexChars = hexString.toUpperCase(Locale.US).toCharArray(); int length = hexString.length()/2; byte[] bytes = new byte[length]; for (int i = 0; i < length; ++i) { bytes[i] = (byte)(HEX_DIGITS.indexOf(hexChars[i*2]) << 4 | HEX_DIGITS.indexOf(hexChars[i*2 + 1])); } return bytes; } /** * 獲得MD5.字節(jié)數(shù)組 * @param bytes * @return * @throws Exception */ public static byte[] getMd5(byte[] bytes) throws Exception { if (bytes == null) { throw new Exception("illegal params."); } MessageDigest md5 = MessageDigest.getInstance("MD5"); md5.update(bytes); return md5.digest(); } /** * 獲得MD5值,返回16進(jìn)制字符串 * @param str * @return * @throws Exception */ public static String getMd5Hex(String str) throws Exception { if (StringUtils.isBlank(str)) { throw new Exception("illegal params."); } return bytesToHexString(str.getBytes(UTF_8)); } /** * 獲得SHA1值。 * @param bytes * @return * @throws Exception */ public static byte[] getSha1(byte[] bytes) throws Exception { if (bytes == null) { throw new Exception("illegal params."); } MessageDigest sha1 = MessageDigest.getInstance("SHA⑴"); sha1.update(bytes); return sha1.digest(); } /** * 獲得SHA1值。 * @param str * @return * @throws Exception */ public static String getSha1Hex(String str) throws Exception { if (StringUtils.isBlank(str)) { throw new Exception("illegal params."); } return bytesToHexString(getSha1(str.getBytes(UTF_8))); } /** * * @param content * @param key * @return * @throws Exception */ public static byte[] getHmacSha1(byte[] content, byte[] key) throws Exception { SecretKeySpec signingKey = new SecretKeySpec(key, "HmacSHA1"); Mac mac = Mac.getInstance(signingKey.getAlgorithm()); mac.init(signingKey); return mac.doFinal(content); } /** * * @param content * @param key * @return * @throws Exception */ public static String getHmacSha1Hex(String content, String key) throws Exception { SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(UTF_8), "HmacSHA1"); Mac mac = Mac.getInstance(signingKey.getAlgorithm()); mac.init(signingKey); return bytesToHexString(mac.doFinal(content.getBytes(UTF_8))); } /** * * @param contentBytes * @param key * @param opmode * @return * @throws Exception */ public static byte[] aes(int opmode, byte[] content, byte[] key) throws Exception { SecureRandom secureRandom = null; //if (android.os.Build.VERSION.SDK_INT >= 17) {//SHA1PRNG強(qiáng)隨機(jī)種子算法,要區(qū)分4.2以上版本的調(diào)用方法 secureRandom = SecureRandom.getInstance("SHA1PRNG", "Crypto"); //} else { // secureRandom = SecureRandom.getInstance("SHA1PRNG"); //} secureRandom.setSeed(key); KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128, secureRandom); Cipher cipher = Cipher.getInstance("AES/ECB/ZeroBytePadding"); cipher.init(opmode, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES")); return cipher.doFinal(content); } /** * AES加密,返回16進(jìn)制字符串 * @param content * @param key * @return * @throws Exception */ public static String aesEncryptHex(String content, String key) throws Exception { if (StringUtils.isBlank(content) || StringUtils.isBlank(key)) { throw new Exception("illegal params."); } return bytesToHexString(aes(Cipher.ENCRYPT_MODE, content.getBytes(UTF_8), key.getBytes(UTF_8))); } /** * AES解密,針對原始加密串為16進(jìn)制字符串 * @param encryptHexStr * @param key * @return * @throws Exception */ public static String aesDecryptHex(String encryptHexStr, String key) throws Exception { if (StringUtils.isBlank(encryptHexStr) || StringUtils.isBlank(key)) { throw new Exception("illegal params."); } return new String(aes(Cipher.DECRYPT_MODE, hexStringToBytes(encryptHexStr), key.getBytes(UTF_8))); } /** * rsaSign * @param content * @param privateKey * @return * @throws Exception */ public static byte[] rsaSign(byte[] content, byte[] privateKey) throws Exception { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); byte[] encodedKey = Base64.decode(privateKey, Base64.DEFAULT); PrivateKey privateKeyObj = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(encodedKey)); java.security.Signature signature = java.security.Signature.getInstance("SHA1WithRSA"); signature.initSign(privateKeyObj); signature.update(content); return signature.sign(); } /** * rsaSignHex * @param content * @param privateKey * @return * @throws Exception */ public static String rsaSignHex(String content, String privateKey) throws Exception { if (StringUtils.isBlank(content) || StringUtils.isBlank(privateKey)) { throw new Exception("illegal params."); } return bytesToHexString(rsaSign(content.getBytes(UTF_8), privateKey.getBytes(UTF_8))); } /** * rsaVerify * @param content * @param sign * @param publicKey * @return * @throws Exception */ public static boolean rsaVerify(byte[] content, byte[] sign, byte[] publicKey) throws Exception { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); byte[] encodedKey = Base64.decode(publicKey, Base64.DEFAULT); PublicKey publicKeyObj = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey)); java.security.Signature signature = java.security.Signature.getInstance("SHA1WithRSA"); signature.initVerify(publicKeyObj); signature.update(content); return signature.verify(sign); } /** * rsaVerifyHex * @param content * @param sign * @param publicKey * @return * @throws Exception */ public static boolean rsaVerifyHex(String content, String sign, String publicKey) throws Exception { if (StringUtils.isBlank(content) || StringUtils.isBlank(sign) || StringUtils.isBlank(publicKey)) { throw new Exception("illegal params."); } return rsaVerify(content.getBytes(UTF_8), hexStringToBytes(sign), publicKey.getBytes(UTF_8)); } }



生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 国产香蕉在线观看 | 一区精 | 日韩极品在线 | 国产超碰人人做人人爽aⅴ 亚州国产 | 黄色小视频在线 | 求毛片 | 日本中文字幕在线播放 | 成年人免费观看视频网站 | av在线资源网 | 欧洲久久久 | 一级特黄a免费观看视频 | 久久久久精 | 色网网站| 毛片毛片毛片毛片毛片毛片毛片毛片毛片毛片 | 黄色三级免费看 | 中文字幕av一区二区 | 日韩专区在线播放 | 狠狠躁日日躁夜夜躁影院 | av中文字幕在线 | 国产曰批免费观看久久久 | 爱爱视频在线看 | 欧美 日韩 中文字幕 | 99re视频在线播放 | 97狠狠操| 一二区成人影院电影网 | 日韩不卡在线 | 男人操女人免费视频 | 欧美一区二区三区视频在线 | 激情欧美日韩一区二区 | 九九热在线视频 | 国产嫩草影院久久久久 | 九九精品视频在线观看 | 亚洲精品一区二区三区不 | 91精品久久久久久久久青青 | 综合色婷婷 | 国产免费一区二区三区 | 在线a级毛片 | 精品无码久久久久久国产 | 激情的网站| 99热国产在线 | 国产精品二区三区 |