81,092
社区成员
发帖
与我相关
我的任务
分享
String str="Qw16MiIozn1azOfNHM6N9JOm+Q3kEBd1+RUQ+Y5L4MOl4wtsXqYEj8Bw/9eYBKqwhIe1hkL19qiXEKGlpQ8/D6eImgIpu1OKg7XYne6UtD2ck6A5xrNM0jNXJaukDhfVtkYm9wfYfz7fFgCP6YQPMG3k42SbeDlv+qpc7d6l0W0=";
String RSA_PUBLIC_KEY="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCeNXlKz6QjWQ1APVPTjxvZuoOYeUz/SPAs5UrVv4E+qO4H7a06X+222Y6AzpG1ag0DsapeMAb10rChVvLbippkdvMdF6zIJM3LZ2wNP6BVsvrsn+7SYOFeCtMtU6PfZlz0iVbMvyHyVTyq4aQ+OgnkerHU4MwmKBTmJ6megv9OvQIDAQAB";
public static void main(String[] args) {
System.out.println("hello");
String RSA_PUBLIC_KEY="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCeNXlKz6QjWQ1APVPTjxvZuoOYeUz/SPAs5UrVv4E+qO4H7a06X+222Y6AzpG1ag0DsapeMAb10rChVvLbippkdvMdF6zIJM3LZ2wNP6BVsvrsn+7SYOFeCtMtU6PfZlz0iVbMvyHyVTyq4aQ+OgnkerHU4MwmKBTmJ6megv9OvQIDAQAB";
String aa;
try {
aa = RSAUtil.decryptByPublicKey("Qw16MiIozn1azOfNHM6N9JOm+Q3kEBd1+RUQ+Y5L4MOl4wtsXqYEj8Bw/9eYBKqwhIe1hkL19qiXEKGlpQ8/D6eImgIpu1OKg7XYne6UtD2ck6A5xrNM0jNXJaukDhfVtkYm9wfYfz7fFgCP6YQPMG3k42SbeDlv+qpc7d6l0W0=",
RSA_PUBLIC_KEY);
System.out.println(aa);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return cipher.doFinal(data);
package csst.com.p2p.util;
import java.io.IOException;
import android.util.Base64;
/**
* 编码
*/
public class Coder {
/** 编码:UTF-8 */
protected static final String CHARSET_UTF8 = "UTF-8";
/** 算法:MD5 */
protected static final String ALGORITHM_MD5 = "MD5";
/** 算法:RSA */
protected static final String ALGORITHM_RSA = "RSA";
/** 算法:SHA */
protected static final String ALGORITHM_SHA = "SHA";
/**
* MAC算法可选以下多种算法
*
* <pre>
* HmacMD5
* HmacSHA1
* HmacSHA256
* HmacSHA384
* HmacSHA512
* </pre>
*/
protected enum HmacAlgorithm {
/** HmacMD5 */
HMAC_MD5("HmacMD5"),
/** HmacSHA1 */
HMAC_SHA1("HmacSHA1"),
/** HmacSHA256 */
HMAC_SHA256("HmacSHA256"),
/** HmacSHA384 */
HMAC_SHA384("HmacSHA384"),
/** HmacSHA512 */
HMAC_SHA512("HmacSHA512");
private String algorithm;
private HmacAlgorithm(String algorithm) {
this.algorithm = algorithm;
}
public String getAlgorithm() {
return this.algorithm;
}
public void setAlgorithm(String algorithm) {
this.algorithm = algorithm;
}
}
/**
* BASE64解码
*
* @param key
* @return
* @throws Exception
*/
public static byte[] decoderBASE64(String key) throws IOException {
return Base64.decode(key,Base64.NO_PADDING);//decodeBase64(key);
}
/**
* BASE64编码
*
* @param key
* @return
*/
public static String encoderBASE64(byte[] key) {
return Base64.encodeToString(key,Base64.NO_PADDING);//.encodeBase64String(key);
}
/**
* ASCII码转BCD码
*
*/
public static byte[] ASCII2BCD(byte[] ascii, int asc_len) {
byte[] bcd = new byte[asc_len / 2];
int j = 0;
for (int i = 0; i < (asc_len + 1) / 2; i++) {
bcd[i] = asc2bcd(ascii[j++]);
bcd[i] = (byte) (((j >= asc_len) ? 0x00 : asc2bcd(ascii[j++])) + (bcd[i] << 4));
}
return bcd;
}
public static byte asc2bcd(byte asc) {
byte bcd;
if ((asc >= '0') && (asc <= '9'))
bcd = (byte) (asc - '0');
else if ((asc >= 'A') && (asc <= 'F'))
bcd = (byte) (asc - 'A' + 10);
else if ((asc >= 'a') && (asc <= 'f'))
bcd = (byte) (asc - 'a' + 10);
else
bcd = (byte) (asc - 48);
return bcd;
}
/**
* BCD转字符串
*/
public static String bcd2Str(byte[] bytes) {
char temp[] = new char[bytes.length * 2], val;
for (int i = 0; i < bytes.length; i++) {
val = (char) (((bytes[i] & 0xf0) >> 4) & 0x0f);
temp[i * 2] = (char) (val > 9 ? val + 'A' - 10 : val + '0');
val = (char) (bytes[i] & 0x0f);
temp[i * 2 + 1] = (char) (val > 9 ? val + 'A' - 10 : val + '0');
}
return new String(temp);
}
/**
* 拆分字符串
*/
public static String[] splitString(String string, int len) {
int x = string.length() / len;
int y = string.length() % len;
int z = 0;
if (y != 0) {
z = 1;
}
String[] strings = new String[x + z];
String str = "";
for (int i = 0; i < x + z; i++) {
if (i == x + z - 1 && y != 0) {
str = string.substring(i * len, i * len + y);
} else {
str = string.substring(i * len, i * len + len);
}
strings[i] = str;
}
return strings;
}
/**
* 拆分数组
*/
public static byte[][] splitArray(byte[] data, int len) {
int x = data.length / len;
int y = data.length % len;
int z = 0;
if (y != 0) {
z = 1;
}
byte[][] arrays = new byte[x + z][];
byte[] arr;
for (int i = 0; i < x + z; i++) {
arr = new byte[len];
if (i == x + z - 1 && y != 0) {
System.arraycopy(data, i * len, arr, 0, y);
} else {
System.arraycopy(data, i * len, arr, 0, len);
}
arrays[i] = arr;
}
return arrays;
}
}
package csst.com.p2p.util;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.Key;
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.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
/**
* RSA算法工具类
*/
public class RSAUtil extends Coder {
/**
* 私钥解密
*
* @param data
* BASE64编码后的待解密字符串
* @param privateKey
* BASE64编码后的私钥
* @return
* @throws Exception
*/
public static String decryptByPrivateKey(String data, String privateKey) throws Exception {
byte[] dataByte = decoderBASE64(data);
byte[] decryptByte = decryptByPrivateKey(dataByte, privateKey);
dataByte = null;
return new String(decryptByte);
}
/**
* 公钥解密
*
* @param data
* BASE64编码后的待解密字符串
* @param publicKey
* BASE64编码后的公钥
* @return
* @throws Exception
*/
public static String decryptByPublicKey(String data, String publicKey) throws Exception {
byte[] dataByte = decoderBASE64(data);
byte[] decryptByte = decryptByPublicKey(dataByte, publicKey);
dataByte = null;
return new String(decryptByte);
}
/**
* 私钥加密
*
* @param data
* 待加密字符串
* @param privateKey
* BASE64编码后的私钥
* @return
* @throws Exception
*/
public static String encryptByPrivateKey(String data, String privateKey) throws Exception {
byte[] dataByte = data.getBytes();
byte[] encryptByte = encryptByPrivateKey(dataByte, privateKey);
dataByte = null;
return encoderBASE64(encryptByte);
}
/**
* 公钥加密
*
* @param data
* 待加密字符串
* @param publicKey
* BASE64编码后的公钥
* @return
* @throws Exception
*/
public static String encryptByPublicKey(String data, String publicKey) throws Exception {
byte[] dataByte = data.getBytes();
byte[] encryptByte = encryptByPublicKey(dataByte, publicKey);
dataByte = null;
return encoderBASE64(encryptByte);
}
/**
* 解密<br>
* 用私钥解密
*
* @param data 待解密数据
* @param key
* @return
* @throws Exception
*/
public static byte[] decryptByPrivateKey(byte[] data, String key) throws Exception {
// 取得私钥
PrivateKey privateKey = getPrivateKey(key);
return decrypt(data, privateKey);
}
/**
* 解密<br>
* 用公钥解密
*
* @param data 待解密数据
* @param key
* @return
* @throws Exception
*/
public static byte[] decryptByPublicKey(byte[] data, String key) throws Exception {
// 取得公钥
PublicKey publicKey = getPublicKey(key);
return decrypt(data, publicKey);
}
/**
* 加密<br>
* 用公钥加密
*
* @param data 待加密数据
* @param key
* @return
* @throws Exception
*/
public static byte[] encryptByPublicKey(byte[] data, String key) throws Exception {
// 取得公钥
PublicKey publicKey = getPublicKey(key);
return encrypt(data, publicKey);
}
/**
* 加密<br>
* 用私钥加密
*
* @param data 待加密数据
* @param key
* @return
* @throws Exception
*/
public static byte[] encryptByPrivateKey(byte[] data, String key) throws Exception {
// 取得私钥
PrivateKey privateKey = getPrivateKey(key);
return encrypt(data, privateKey);
}
/**
* 解密
*
* @param data 待解密数据
* @param key Key对象
* @return
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
private static byte[] decrypt(byte[] data, Key key) throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
// 对数据解密
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(data);
}
/**
* 加密
*
* @param data 待加密数据
* @param key Key对象
* @return
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
private static byte[] encrypt(byte[] data, Key key) throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
// 对数据加密
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(data);
}
/**
* 获取PrivateKey对象
*
* @param privateKey
* BASE64编码后的私钥
* @return PrivateKey
* @throws Exception
*/
private static PrivateKey getPrivateKey(String privateKey) throws Exception {
// 对密钥解码
byte[] keyBytes = decoderBASE64(privateKey);
// 取得私钥
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
return keyFactory.generatePrivate(pkcs8KeySpec);
}
/**
* 获取PublicKey对象
*
* @param publicKey
* BASE64编码后的公钥
* @return PublicKey
* @throws Exception
*/
private static PublicKey getPublicKey(String publicKey) throws Exception {
// 对公钥解密
byte[] keyBytes = decoderBASE64(publicKey);
// 取得公钥
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
return keyFactory.generatePublic(x509KeySpec);
}
/**
* 取得私钥,并用BASE64编码转成字符串
*
* @param keyMap
* @return
* @throws Exception
*/
public static String getPrivateKey(Map<String, Object> keyMap) throws Exception {
Key key = (Key) keyMap.get(PRIVATE_KEY);
return encoderBASE64(key.getEncoded());
}
/**
* 取得公钥,并用BASE64编码转成字符串
*
* @param keyMap
* @return
* @throws Exception
*/
public static String getPublicKey(Map<String, Object> keyMap) throws Exception {
Key key = (Key) keyMap.get(PUBLIC_KEY);
return encoderBASE64(key.getEncoded());
}
/**
* 初始化密钥
*
* @throws NoSuchAlgorithmException
* @return keyMap
*/
public static HashMap<String, Object> initKey() throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(ALGORITHM_RSA);
keyPairGen.initialize(KEY_SIZE);
// 密钥对
KeyPair keyPair = keyPairGen.generateKeyPair();
// 公钥
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
// 私钥
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
HashMap<String, Object> map = new HashMap<String, Object>(2);
map.put(PUBLIC_KEY, publicKey);
map.put(PRIVATE_KEY, privateKey);
return map;
}
private static int KEY_SIZE = 1024;
private static final String PUBLIC_KEY = "RSAPublicKey";
private static final String PRIVATE_KEY = "RSAPrivateKey";
private static KeyFactory keyFactory = null;
static {
// keyFactory初始化
try {
keyFactory = KeyFactory.getInstance(ALGORITHM_RSA);
} catch (NoSuchAlgorithmException e) {
System.err.println("rsa keyFactory 初始化失败");
e.printStackTrace();
}
}
}
private static String RSAC = "RSA/ECB/PKCS1Padding";
这种地方改掉
Cipher cipher = Cipher.getInstance(RSAC);