大文件加签处理
2017-11-15 09:48
113 查看
package com.bwf.util.crypt; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; import java.security.SecureRandom; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import javax.crypto.Cipher; import javax.crypto.CipherInputStream; import org.apache.commons.codec.binary.Base64; import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.bwf.log.LOG_TYPE; import com.bwf.util.StringUtil; /** * RSA大文件加密处理 * * @author BAOWEIFENG234 * @version $Id: RSAUtils.java, v 0.1 2017-11-2 下午3:51:44 BAOWEIFENG234 Exp $ */ public class RSAUtils { private static Logger logger = LoggerFactory.getLogger(LOG_TYPE.PAFF_SERVICE.val); /** * 签名算法 */ public static final String SIGN_ALGORITHMS = "SHA1WithRSA"; public static final String ENCRYPT_ALGORITHMS = "RSA"; public static final String BYTE_ENCODE = "UTF-8"; /** * 测试用例 * @param args */ public static void main(String[] args) { //生成秘钥对 //genKeyPair(); String privateKeyStr = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAMhsrk0lhECY/F46DnSdsKZxRtoW991Q2nfF0IPlyrYKvJMjaj592aRRW6p4Y7qlXecr8//yxGPd8c3v8CAPDZHLuG/yIQJMD9j8/XRUb483InwQc5OBj+GCGhW4gfObzuEjW6jVTyK1OkufxQw0AgGwWm4rt7S9KKlUDnzP+tQTAgMBAAECgYBNqBaNG7k3kk9Oby5ErwjQJNnwDLAzV22etM3LHVFC4bwZQsKavgaDLWbYEVeK7SIKPTSIelbv3wIe6hXfRhXaNC6ZqFjW9nCLUXrdFwuw40Rjxycsr42ZReb1RWbxkLe4WGms0rHwkJwLX35lS13evY5ee5VJdasrsswgS7TMkQJBAP2aHJ31HjLZ9Tmte/GQbx2jUaguhEFyB8XiL1z8THogf4uMubWqEQe35Tki5wVj+/jfRF65amac50YWcEtZupsCQQDKUdf7YOWBz6z1pcdFHPdR/aNKEOZAbR+OBcNAAehsQulemwgz5BDbCpyQQJGAFZkf2AipzRJtKZnE5l1vHUfpAkAGY4KDDDDFkS4XVOKBdGUINxwQg5N7XpcJiigtTIHUtcrgxhvLksw5CI3ywYlWXm1zmJRNWphum1dMMbaEf28vAkEAvaZ0mE5fNQzwa6BHqBCcAX6Jp28p9vCTqLTcLs7bCUi7q5QV31aFOl67Hjl4aqFUVVZT4J+8Cxn462qp4POIqQJAGonqu70ShsOOHbUGnNbqAuz1hYVH3A5cmfu8YIP0km6t9nauTmxJ7sou+WfPYg3bEznW4uzAibh0iwEHACwaow=="; String publicKeyStr = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIbK5NJYRAmPxeOg50nbCmcUbaFvfdUNp3xdCD5cq2CryTI2o+fdmkUVuqeGO6pV3nK/P/8sRj3fHN7/AgDw2Ry7hv8iECTA/Y/P10VG+PNyJ8EHOTgY/hghoVuIHzm87hI1uo1U8itTpLn8UMNAIBsFpuK7e0vSipVA58z/rUEwIDAQAB"; //加签验签 String fsign = signFile("D:/test/20160323FBSfbs21531416360.txt", privateKeyStr, BYTE_ENCODE); doCheckFile("D:/test/20160323FBSfbs21531416361.txt", fsign, publicKeyStr, BYTE_ENCODE); //加密解密 encryptFileBig(privateKeyStr, "D:/test/2300.txt", "D:/test/aencrypt6360.encrypt"); decryptFileBig(publicKeyStr, "D:/test/aencrypt6360.encrypt", "D:/test/aencrypt6360-1.txt"); System.exit(0); } /** * 随机生成密钥对 */ public static void genKeyPair() { // KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象 KeyPairGenerator keyPairGen = null; try { keyPairGen = KeyPairGenerator.getInstance(ENCRYPT_ALGORITHMS); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } keyPairGen.initialize(1024, new SecureRandom()); KeyPair keyPair = keyPairGen.generateKeyPair(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); try { String publicKeyString = new String(Base64.encodeBase64(publicKey.getEncoded())); String privateKeyString = new String(Base64.encodeBase64(privateKey.getEncoded())); System.out.println("publicKeyString:" + publicKeyString); System.out.println("privateKeyString:" + privateKeyString); /*FileWriter pubfw = new FileWriter(filePath + "/publicKey.keystore"); FileWriter prifw = new FileWriter(filePath + "/privateKey.keystore"); BufferedWriter pubbw = new BufferedWriter(pubfw); BufferedWriter pribw = new BufferedWriter(prifw); pubbw.write(publicKeyString); pribw.write(privateKeyString); pubbw.flush(); pubbw.close(); pubfw.close(); pribw.flush(); pribw.close(); prifw.close(); */ } catch (Exception e) { logger.error("genKeyPair-error{}", e); } } /** * RSA签名 * * @param filePath * @param privateKey * @param encode * @return */ public static String signFile(String filePath, String privateKey, String encode) { FileInputStream fis = null; InputStreamReader isr = null; BufferedReader bfr = null; try { PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey)); KeyFactory keyf = KeyFactory.getInstance(ENCRYPT_ALGORITHMS); PrivateKey priKey = keyf.generatePrivate(priPKCS8); java.security.Signature signature = java.security.Signature.getInstance(SIGN_ALGORITHMS); signature.initSign(priKey); fis = new FileInputStream(new File(filePath)); isr = new InputStreamReader(fis, BYTE_ENCODE); bfr = new BufferedReader(isr); String lineTxt = ""; while (StringUtil.isNotBlank(lineTxt = bfr.readLine())) { signature.update(lineTxt.getBytes(encode)); } byte[] signed = signature.sign(); String ret = new String(Base64.encodeBase64(signed)); System.out.println("signed:" + ret); return ret; } catch (Exception e) { logger.error("signFile-error:{}" + e); } finally { if (bfr != null) { try { bfr.close(); } catch (IOException e) { } } if (isr != null) { try { d175 isr.close(); } catch (IOException e) { } } if (fis != null) { try { fis.close(); } catch (IOException e) { } } } return null; } /** * RSA验签名检查 * @param content 待签名数据 * @param sign 签名值 * @param publicKey 分配给开发商公钥 * @param encode 字符集编码 * @return 布尔值 */ public static boolean doCheckFile(String filePath, String sign, String publicKey, String encode) { FileInputStream fis = null; InputStreamReader isr = null; BufferedReader bfr = null; try { KeyFactory keyFactory = KeyFactory.getInstance(ENCRYPT_ALGORITHMS); byte[] encodedKey = Base64.decodeBase64(publicKey); PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey)); java.security.Signature signature = java.security.Signature.getInstance(SIGN_ALGORITHMS); signature.initVerify(pubKey); fis = new FileInputStream(new File(filePath)); isr = new InputStreamReader(fis, BYTE_ENCODE); bfr = new BufferedReader(isr); String lineTxt = ""; while (StringUtil.isNotBlank(lineTxt = bfr.readLine())) { signature.update(lineTxt.getBytes(encode)); } boolean bverify = signature.verify(Base64.decodeBase64(sign)); return bverify; } catch (Exception e) { logger.error("doCheckFile-error:{}" + e); } finally { if (bfr != null) { try { bfr.close(); } catch (IOException e) { } } if (isr != null) { try { isr.close(); } catch (IOException e) { } } if (fis != null) { try { fis.close(); } catch (IOException e) { } } } return false; } public static void encryptFileBig(String privateKeyStr, String srcFileName, String destFileName) { if (privateKeyStr == null) { new Exception("加密私钥为空, 请设置"); } Cipher cipher = null; InputStream is = null; OutputStream out = null; CipherInputStream cis = null; try { // 使用默认RSA PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec( Base64.decodeBase64(privateKeyStr)); KeyFactory keyf = KeyFactory.getInstance(ENCRYPT_ALGORITHMS); PrivateKey priKey = keyf.generatePrivate(priPKCS8); cipher = Cipher.getInstance("RSA", new BouncyCastleProvider()); cipher.init(Cipher.ENCRYPT_MODE, priKey); int blockSize = cipher.getBlockSize(); is = new FileInputStream(srcFileName); File f = new File(srcFileName); int size = Integer.valueOf(String.valueOf(f.length())); byte[] encryptByte = new byte[size]; is.read(encryptByte); out = new FileOutputStream(destFileName); int outputBlockSize = cipher.getOutputSize(encryptByte.length); int leavedSize = encryptByte.length % blockSize; int blocksNum = leavedSize == 0 ? encryptByte.length / blockSize : encryptByte.length / blockSize + 1; byte[] cipherData = new byte[blocksNum * outputBlockSize]; for (int i = 0; i < blocksNum; i++) { if ((encryptByte.length - i * blockSize) > blockSize) { cipher.doFinal(encryptByte, i * blockSize, blockSize, cipherData,i * outputBlockSize); } else { cipher.doFinal(encryptByte, i * blockSize, encryptByte.length - i * blockSize,cipherData, i * outputBlockSize); } } out.write(cipherData); } catch (Exception e) { logger.error("encryptFileBig-error:{}" + e); } finally { if (null != cis) { try { cis.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != is) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != out) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 文件解密 * * @param publicKeyStr * @param srcFileName * @param destFileName */ public static void decryptFileBig(String publicKeyStr, String srcFileName, String destFileName) { Cipher cipher = null; InputStream is = null; OutputStream out = null; try { X509EncodedKeySpec encodedKey = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyStr)); KeyFactory keyf = KeyFactory.getInstance(ENCRYPT_ALGORITHMS); PublicKey pubKey = keyf.generatePublic(encodedKey); cipher = Cipher.getInstance("RSA", new BouncyCastleProvider()); cipher.init(Cipher.DECRYPT_MODE, pubKey); File f = new File(srcFileName); is = new FileInputStream(f); out = new FileOutputStream(destFileName); int blockSize = cipher.getBlockSize(); int size = Integer.valueOf(String.valueOf(f.length())); byte[] decryptByte = new byte[size]; is.read(decryptByte); //分别对各块数据进行解密 int j = 0; while ((decryptByte.length - j * blockSize) > 0) { out.write(cipher.doFinal(decryptByte, j * blockSize, blockSize)); j++; } } catch (Exception e) { logger.error("decryptFileBig-error:{}" + e); } finally { if (null != out) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != is) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
<dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-ext-jdk16</artifactId> <version>1.46</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.8</version> </dependency>
相关文章推荐
- Java中用内存映射处理大文件
- (系统)基于I/O文件处理的猜拳系统
- sql中压缩日志文件和ntextl转化为varchar的处理(downmoon)
- django学习——media处理上传图片、文件方法设置
- mybatis在mapper.xml文件中的大于小于等符号处理
- 批处理文件bat 语法备忘
- dom4j处理xml文件,读取xml字符串,格式化xml文件
- 本地工程中删除了一些不需要的文件后,提交SVN报错处理
- df和du显示的磁盘空间使用情况不一致的原因及处理(文件删除后磁盘空间不释放)
- ASP FSO文件处理函数大全
- Qt5文件及磁盘处理
- Linux如何查找处理文件名后包含空格的文件
- 批处理实现自动删除过期文件的定期操作
- ASP.NET MVC 实践之路 之十八 利用ASP.NET MVC处理文件上传与下载
- 文件指针、文件I/O的错误处理、成员函数来处理文件I/O
- 用C与脚本的混合编程来处理配置文件
- C#中,有两个不同的CS文件,但这两个文件具有相同的命名空间(namespace)。一个CS文件要用另一个CS文件中的类的话,下面有一个处理方式。
- ios 静态库(.a)文件冲突处理
- 化零为整WCF(8) - 消息处理(使用流数据传输文件)
- 以日期格式处理xml文件到json格式的txt文件