注意JAVA 的STRING .getBytes() 默认取的是操作系统的编码,最好统一UTF-8.

--

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1; import java.io.ByteArrayOutputStream;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map; import javax.crypto.Cipher; /**
*
* @author jk
*/
public class RSAUtils { /**
* 加密算法RSA
*/
public static final String KEY_ALGORITHM = "RSA"; // public static final String ECB_PKCS1_PADDING = "RSA";//加密填充方式 public static final String ECB_PKCS1_PADDING = "RSA/ECB/PKCS1Padding";//加密填充方式 private static final int _rsa_keysize = 1024; /**
* RSA最大加密明文大小,keysize=1024, (1024/8)-11=117
*/
private static final int MAX_ENCRYPT_BLOCK = (_rsa_keysize/8)-11; public static final String _charset = "UTF-8"; public static byte[] encryptByPublicKey(byte[] data, String publicKey) throws Exception {
byte[] keyBytes = Base64Utils.decode(publicKey);
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key publicK = keyFactory.generatePublic(x509KeySpec);
// 对数据加密
String getAgorithm = keyFactory.getAlgorithm();
getAgorithm = ECB_PKCS1_PADDING;
Cipher cipher = Cipher.getInstance(getAgorithm);
cipher.init(Cipher.ENCRYPT_MODE, publicK);
int inputLen = data.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offSet = 0;
byte[] cache;
int i = 0;
// 对数据分段加密
while (inputLen - offSet > 0) {
if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
} else {
cache = cipher.doFinal(data, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * MAX_ENCRYPT_BLOCK;
}
byte[] encryptedData = out.toByteArray();
out.close();
return encryptedData;
} /**
* java端公钥加密
*/
public static String encryptedDataOnJava(String data, String PUBLICKEY) {
try {
data = Base64Utils.encode(encryptByPublicKey(data.getBytes(_charset), PUBLICKEY));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return data;
} }

--

package javaapplication1;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.codec.binary.Base64; /**
* */
/**
* <p>
* BASE64编码解码工具包
* </p>
* <p>
* 依赖javabase64-1.3.1.jar
* </p>
*
* @author IceWee
* @date 2012-5-19
* @version 1.0
*/
public class Base64Utils { /**
* */
/**
* 文件读取缓冲区大小
*/
private static final int CACHE_SIZE = 1024; public static final String _charset = "UTF-8"; /**
* */
/**
* <p>
* BASE64字符串解码为二进制数据
* </p>
*
* @param base64
* @return
* @throws Exception
*/
public static byte[] decode(String str) throws Exception {
Base64 _base64 = new Base64();
return _base64.decodeBase64(str.getBytes(_charset));
} /**
* */
/**
* <p>
* 二进制数据编码为BASE64字符串
* </p>
*
* @param bytes
* @return
* @throws Exception
*/
public static String encode(byte[] bytes) throws Exception {
Base64 _base64 = new Base64();
return new String(_base64.encodeBase64Chunked(bytes));
} /**
* */
/**
* <p>
* 将文件编码为BASE64字符串
* </p>
* <p>
* 大文件慎用,可能会导致内存溢出
* </p>
*
* @param filePath 文件绝对路径
* @return
* @throws Exception
*/
public static String encodeFile(String filePath) throws Exception {
byte[] bytes = fileToByte(filePath);
return encode(bytes);
} /**
* */
/**
* <p>
* BASE64字符串转回文件
* </p>
*
* @param filePath 文件绝对路径
* @param base64 编码字符串
* @throws Exception
*/
public static void decodeToFile(String filePath, String base64) throws Exception {
byte[] bytes = decode(base64);
byteArrayToFile(bytes, filePath);
} /**
* */
/**
* <p>
* 文件转换为二进制数组
* </p>
*
* @param filePath 文件路径
* @return
* @throws Exception
*/
public static byte[] fileToByte(String filePath) throws Exception {
byte[] data = new byte[0];
File file = new File(filePath);
if (file.exists()) {
FileInputStream in = new FileInputStream(file);
ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
byte[] cache = new byte[CACHE_SIZE];
int nRead = 0;
while ((nRead = in.read(cache)) != -1) {
out.write(cache, 0, nRead);
out.flush();
}
out.close();
in.close();
data = out.toByteArray();
}
return data;
} /**
* */
/**
* <p>
* 二进制数据写文件
* </p>
*
* @param bytes 二进制数据
* @param filePath 文件生成目录
*/
public static void byteArrayToFile(byte[] bytes, String filePath) throws Exception {
InputStream in = new ByteArrayInputStream(bytes);
File destFile = new File(filePath);
if (!destFile.getParentFile().exists()) {
destFile.getParentFile().mkdirs();
}
destFile.createNewFile();
OutputStream out = new FileOutputStream(destFile);
byte[] cache = new byte[CACHE_SIZE];
int nRead = 0;
while ((nRead = in.read(cache)) != -1) {
out.write(cache, 0, nRead);
out.flush();
}
out.close();
in.close();
} }

--

最新文章

  1. python jenkins-api,jira crowd. email-servers
  2. js返回上一页并刷新的多种实现方法
  3. hdu Rescue
  4. Android调用Sqlite数据库时自动生成db-journal文件的原因
  5. 《Java数据结构与算法》笔记-CH3简单排序
  6. iOS开发——新特性Swift篇&amp;Swift 2.0 异常处理
  7. 一个分门别列介绍JavaScript各种常用工具的脑图
  8. CloudXNS首次使用体验
  9. Android高级编程笔记(四)深入探讨Activity(转)
  10. Dockerfile的书写规则及指令使用方法
  11. ETL kettle 数据调取防止意外停止处理
  12. KoaHub.JS基于Node.js开发的mysql的node.js驱动程序代码
  13. java 基础知识五 数组
  14. .NET Framework 4.7 安装
  15. bootstrap学习笔记之导航条基础
  16. 【leetcode74】Sum of Two Integers(不用+,-求两数之和)
  17. asp.net 六大对象之Request、Response
  18. pwnable.kr-flag-witeup
  19. Spring基础2
  20. Objective-C 学习笔记(四) 数组

热门文章

  1. e与复利
  2. help2man: can&#39;t get `--help&#39; info from automake-1.15 Try `--no-discard-stderr&#39; if option outputs to stderr Makefile:3687: recipe for target &#39;doc/automake-1.15.1&#39; failed
  3. mvc core2.1 Identity.EntityFramework Core 配置 (一)
  4. AangularJS相关术语
  5. Java中final的用法总结
  6. golang-test-tool-gotests
  7. centos Cannot allocate memory for the buffer pool
  8. linux忘记root密码
  9. git配置config文件
  10. pycharm 的操作1