/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/package dbmpparser.msgutil;import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;import org.apache.log4j.Logger;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.SecureRandom;
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.security.spec.RSAPublicKeySpec;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.Cipher;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.security.*;
import java.math.BigInteger;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;import dbmpparser.*;/**
* 各种静态处理方法集合
* @author yangchenghao
*/
public class Utils {
// 写日志文件
static Logger logger = Logger.getLogger(Utils.class.getName());
/**
* 用公钥对进行MD5加密过的约定字符串,进行再一次加密
*
* 说明:
* @param originalString
* @param publicKeyArray
* @return
* @throws Exception
* 创建时间:2010-12-1 下午06:29:51
*/
public static byte[] encrypt(byte[] publicKey1, byte[] data) {
if (publicKey1 != null) {
try {
byte[] keyBytes;
//keyBytes = Base64.decode(new String(publicKey1));
System.out.println("获取公钥之前:--"+new String(publicKey1));//"65fbf22ba7be1c28a5a31036956fee8f2912e1f8e06c91674c3057bee666828f656befd5434a2b7d58f62637972731b45d087d23a7747e8417b3c6a2aa8b590c3ae5aafa90e97979569fadaa6f6bac556f04f897afefb9040067abf259f4d37f487fbea21a55b89be203f3cedfd45517d5dfab52096119fdfb0c48d4bbed"
//String teststr = new String(publicKey1,"UTF-16");
//keyBytes = (new BASE64Encoder()).encodeBuffer(publicKey1);
// String teststr = (new BASE64Encoder()).encodeBuffer(publicKey1);
// keyBytes = (new BASE64Decoder()).decodeBuffer(new String(publicKey1)); // X509EncodedKeySpec keySpec=new X509EncodedKeySpec(teststr.getBytes());
X509EncodedKeySpec keySpec=new X509EncodedKeySpec(publicKey1);
// PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec();
System.out.println("编码格式:"+keySpec.getFormat());
KeyFactory keyFactory=KeyFactory.getInstance("RSA");
PublicKey publickey=keyFactory.generatePublic(keySpec); //生成公钥时报错
Cipher cipher = Cipher.getInstance("RSA",
new BouncyCastleProvider());
cipher.init(Cipher.ENCRYPT_MODE, publickey);
return cipher.doFinal(data); } catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
}报错问题:java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException: insufficient data
at sun.security.rsa.RSAKeyFactory.engineGeneratePublic(RSAKeyFactory.java:163)
at java.security.KeyFactory.generatePublic(KeyFactory.java:304)
at dbmpparser.msgutil.Utils.encrypt(Utils.java:114)
at dbmpparser.msgutil.DESCipher.main(DESCipher.java:120)
Caused by: java.security.InvalidKeyException: IOException: insufficient data
at sun.security.x509.X509Key.decode(X509Key.java:380)
at sun.security.x509.X509Key.decode(X509Key.java:386)
at sun.security.rsa.RSAPublicKeyImpl.<init>(RSAPublicKeyImpl.java:65)
at sun.security.rsa.RSAKeyFactory.generatePublic(RSAKeyFactory.java:256)
at sun.security.rsa.RSAKeyFactory.engineGeneratePublic(RSAKeyFactory.java:159)
... 3 more
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/package dbmpparser.msgutil;import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;import org.apache.log4j.Logger;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.SecureRandom;
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.security.spec.RSAPublicKeySpec;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.Cipher;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.security.*;
import java.math.BigInteger;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;import dbmpparser.*;/**
* 各种静态处理方法集合
* @author yangchenghao
*/
public class Utils {
// 写日志文件
static Logger logger = Logger.getLogger(Utils.class.getName());
/**
* 用公钥对进行MD5加密过的约定字符串,进行再一次加密
*
* 说明:
* @param originalString
* @param publicKeyArray
* @return
* @throws Exception
* 创建时间:2010-12-1 下午06:29:51
*/
public static byte[] encrypt(byte[] publicKey1, byte[] data) {
if (publicKey1 != null) {
try {
byte[] keyBytes;
//keyBytes = Base64.decode(new String(publicKey1));
System.out.println("获取公钥之前:--"+new String(publicKey1));//"65fbf22ba7be1c28a5a31036956fee8f2912e1f8e06c91674c3057bee666828f656befd5434a2b7d58f62637972731b45d087d23a7747e8417b3c6a2aa8b590c3ae5aafa90e97979569fadaa6f6bac556f04f897afefb9040067abf259f4d37f487fbea21a55b89be203f3cedfd45517d5dfab52096119fdfb0c48d4bbed"
//String teststr = new String(publicKey1,"UTF-16");
//keyBytes = (new BASE64Encoder()).encodeBuffer(publicKey1);
// String teststr = (new BASE64Encoder()).encodeBuffer(publicKey1);
// keyBytes = (new BASE64Decoder()).decodeBuffer(new String(publicKey1)); // X509EncodedKeySpec keySpec=new X509EncodedKeySpec(teststr.getBytes());
X509EncodedKeySpec keySpec=new X509EncodedKeySpec(publicKey1);
// PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec();
System.out.println("编码格式:"+keySpec.getFormat());
KeyFactory keyFactory=KeyFactory.getInstance("RSA");
PublicKey publickey=keyFactory.generatePublic(keySpec); //生成公钥时报错
Cipher cipher = Cipher.getInstance("RSA",
new BouncyCastleProvider());
cipher.init(Cipher.ENCRYPT_MODE, publickey);
return cipher.doFinal(data); } catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
}报错问题:java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException: insufficient data
at sun.security.rsa.RSAKeyFactory.engineGeneratePublic(RSAKeyFactory.java:163)
at java.security.KeyFactory.generatePublic(KeyFactory.java:304)
at dbmpparser.msgutil.Utils.encrypt(Utils.java:114)
at dbmpparser.msgutil.DESCipher.main(DESCipher.java:120)
Caused by: java.security.InvalidKeyException: IOException: insufficient data
at sun.security.x509.X509Key.decode(X509Key.java:380)
at sun.security.x509.X509Key.decode(X509Key.java:386)
at sun.security.rsa.RSAPublicKeyImpl.<init>(RSAPublicKeyImpl.java:65)
at sun.security.rsa.RSAKeyFactory.generatePublic(RSAKeyFactory.java:256)
at sun.security.rsa.RSAKeyFactory.engineGeneratePublic(RSAKeyFactory.java:159)
... 3 more
解决方案 »
- 求解:巴巴运动网源码部署到myeclipse8.6启动tomcat7.0时出错
- jsp页面如何处理inputstream,求助啊~~愁死我了~~~~
- hibernate多对多关联的困惑
- RMI远程文件下载
- jsf的问题[有答案后马上给分]
- 关于JBuilder2005开发struts中的<html:errors/>不能显示的问题
- 非常急!谁有struts1.1的源码?
- 重新发贴,关于一个问题的J2EE设计
- 各位大侠救救我吧,这个问题解决不了就没法活了
- bootstrap-table取后台数据问题
- 请高手帮我分析一个架构问题
- 传说中的Exception in threads "main" java.lang.NoClassDefFoundError: 试了n种方法了,求救.....
* 初始化RSA密钥
*
*/
static{
KeyPairGenerator keyPairGen;
try {
keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(1024);
KeyPair keyPair = keyPairGen.generateKeyPair(); privateKey = (RSAPrivateKey) keyPair.getPrivate();
publicKey = (RSAPublicKey) keyPair.getPublic();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
keyBytes = Base64.decode(new String(publicKey1));
System.out.println("获取公钥之前:--"+new String(publicKey1));
说实话我是真的看不懂这些是在干什么!