工作中常常使用的几种加密以及签名的方式:RSA加密
来源:互联网 发布:网络计划图计算 编辑:程序博客网 时间:2024/05/17 06:55
import java.io.FileInputStream;import java.io.IOException;import java.math.BigInteger;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;import java.security.KeyFactory;import java.security.PrivateKey;import java.security.PublicKey;import java.security.Signature;import java.security.spec.PKCS8EncodedKeySpec;import java.security.spec.X509EncodedKeySpec;public class RSACoder { private static final String Algorithm = "RSA"; public static final String Signature_Algorithm_MD5 = "MD5withRSA"; public static final String Signature_Algorithm_SHA1 = "SHA1WithRSA"; public static String signMS(String data, String privateKeyFile) throws Exception { PrivateKey privateKey = getPrivateKeyPem(privateKeyFile); PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded()); KeyFactory keyFactory = KeyFactory.getInstance(Algorithm); PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec); Signature signature = Signature.getInstance(Signature_Algorithm_SHA1); signature.initSign(priKey); signature.update(data.getBytes("UTF-8")); return Base64.encode(signature.sign()); } private static PrivateKey getPrivateKeyPem(String fileName) throws Exception { String key = readFile(fileName, "UTF-8"); byte[] keyBytes = buildPKCS8Key(key); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(Algorithm); PrivateKey privateKey = keyFactory.generatePrivate(keySpec); return privateKey; } private static byte[] buildPKCS8Key(String privateKey) throws IOException { if (privateKey.contains("-----BEGIN PRIVATE KEY-----")) { return Base64.decode(privateKey.replaceAll("-----\\w+ PRIVATE KEY-----", "")); } else if (privateKey.contains("-----BEGIN RSA PRIVATE KEY-----")) { final byte[] innerKey = Base64.decode(privateKey.replaceAll("-----\\w+ RSA PRIVATE KEY-----", "")); final byte[] result = new byte[innerKey.length + 26]; System.arraycopy(Base64.decode("MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKY="), 0, result, 0, 26); System.arraycopy(BigInteger.valueOf(result.length - 4).toByteArray(), 0, result, 2, 2); System.arraycopy(BigInteger.valueOf(innerKey.length).toByteArray(), 0, result, 24, 2); System.arraycopy(innerKey, 0, result, 26, innerKey.length); return result; } else { return Base64.decode(privateKey); } } private static String readFile(String filePath, String charSet) throws Exception { FileInputStream fileInputStream = new FileInputStream(filePath); try { FileChannel fileChannel = fileInputStream.getChannel(); ByteBuffer byteBuffer = ByteBuffer.allocate((int) fileChannel.size()); fileChannel.read(byteBuffer); byteBuffer.flip(); return new String(byteBuffer.array(), charSet); } finally { fileInputStream.close(); } } public static boolean verifyMS(String data, String signData, String publicKeyFile) throws Exception { PublicKey publicKey = getPubliceKeyPem(publicKeyFile); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey.getEncoded()); KeyFactory keyFactory = KeyFactory.getInstance(Algorithm); PublicKey pubKey = keyFactory.generatePublic(keySpec); Signature signature = Signature.getInstance(Signature_Algorithm_SHA1); signature.initVerify(pubKey); signature.update(data.getBytes("UTF-8")); return signature.verify(Base64.decode(signData)); } private static PublicKey getPubliceKeyPem(String fileName) throws Exception { String key = readFile(fileName, "UTF-8"); key = key.replaceAll("\\-{5}[\\w\\s]+\\-{5}[\\r\\n|\\n]", ""); KeyFactory keyFactory = KeyFactory.getInstance(Algorithm); byte[] encodedKey = Base64.decode(key); PublicKey pubKey = keyFactory .generatePublic(new X509EncodedKeySpec(encodedKey)); return pubKey; }}
0 0
- 工作中常常使用的几种加密以及签名的方式:RSA加密
- 工作中常常使用的几种加密以及签名的方式:DES加密
- 工作中常常使用的几种加密以及签名的方式:Base64
- 工作中常常使用的几种加密以及签名的方式:32位MD5
- RSA加密使用方式及签名验证
- Python使用Rsa公钥加密,私钥解密以及私钥签名,公钥验签的例子
- 无线加密的几种加密方式
- 关于使用JS前台加密、JAVA后台解密的RSA实现,RSA加密和签名
- 关于使用JS前台加密、JAVA后台解密的RSA实现,RSA加密和签名
- iOS开发中常用到的加密方式
- 加密方式-非对称加密(RSA加密与签名)
- iOS加密方式(RSA签名加密、AES加密等)
- Android 常见的几种加密方式RSA、CBC的集成
- android RSA加密的使用
- RSA加密解密的使用!
- 对称加密与非对称加密,以及RSA的原理
- wifi 的几种加密方式
- ios几种加密的方式
- udp与tcp区别
- String类和StringBuffer类的区别
- 常用的加密算法有哪些
- 定时任务 Crontab命令 详解
- YII2.0 常用路径
- 工作中常常使用的几种加密以及签名的方式:RSA加密
- android各种格式文件的打开(代码)
- Encryption: What is key wrapping?
- matlab 调用mex -setup问题
- 1号店如何玩转混合云
- 并发和并行
- HorizontalScrollView内的多个tab的滑动处理
- 机器的“深度学习”会是交易的未来吗
- Canny边缘检测、霍夫直线、霍夫圆