/*
* 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
解决方案 »
- 如何将一个java文件编译成web Service?
- Stateful Bean Create函数的参数问题
- javamail 的问题?
- 请问怎么在运行时取出在Ibatis映射好的SQL
- 使用spring的Web MVC的问题
- ibatis的namespace有什么用?如何使用呢?
- java.lang.UnsupportedClassVersionError
- 周4 东软 来招人,要会jsp,servlet,struts,前面还好,struts不会,问问大家,假如我周1晚开始学,周1,2,3,这3天学struct,看什么书好
- 请教Tomcat中设置了数据库连接的DataSource,与直接通过JDBC访问数据库,性能会提升多少,数据访问会更快吗?
- 求问Java老司机,这段代码的参数传递这是怎么样的,最好能给个内存图
- 求助:频繁连接FTP服务器报错
- ibatis3.X如何调用Oracle存储过程定义的in数组
/**
* 初始化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();
}
}