多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內(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ù):2634次

Android/Java中經(jīng)常使用的簽名算法實(shí)現(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)閉
程序員人生
主站蜘蛛池模板: 国产亚洲欧美另类专区 | 18网站在线观看 | 波多野结衣被强在线视频 | 久久精品二区 | xxxporn日本护士24| 成人午夜精品网站在线观看 | 亚洲欧美另类色妞网站 | 国产a级淫片 | 亚洲成人av| 亚洲精品欧美精品一区二区 | 极品丝袜高跟91白沙发在线 | 亚洲第一免费 | 日韩精品一区二区三区高清 | 国产成人精品久久一区二区小说 | 亚洲精品短视频 | 国产精品三级视频 | 91美女啪啪 | 高清免费a级在线观看国产 高清免费国产在线观看 | 亚洲欧美视频一区 | 毛片网此| 97热久久免费频精品99国产成人 | 欧美在线成人免费国产 | 亚洲精品国产成人99久久 | 综合网久久 | 在线播放精品视频 | 亚洲国产一区二区三区综合片 | 免费视频观看在线www日本 | 91精品国产综合久久欧美 | 久久国产精品二国产精品 | 亚洲视频一区二区在线观看 | 暖暖在线精品日本中文 | 亚洲欧美人成人综合在线50p | 经典三级一区二区三区视频 | 性欧美videofree另类 | 亚洲美女色 | 国产精品成久久久久三级 | 日本在线视频不卡 | 亚洲综合图区 | 黑人40厘米全进去xxxx猛交 | 黄色网址免费大全 | 欧美精品一级毛片 |