想实现公钥加密,私钥解密,基于b/s的
客户端的都打包在1.rar压缩文件里面了,有一个显示页面,是同过js获取Ukey里面的的证书(准确的说是获取浏览器里面的信息),然后自己讲证书给导出来了放在了服务器上面,然后服务器端代码是
TestZs.java
import it.sauronsoftware.base64.Base64;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;
import java.security.PublicKey;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.security.cert.CertificateException;
import javax.security.cert.X509Certificate;
import org.apache.commons.codec.binary.Hex;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class TestZs {
private static final Provider DEFAULT_PROVIDER = new BouncyCastleProvider();
private static final String ALGORITHOM = "RSA";
public static void main(String[] args) throws UnsupportedEncodingException {
File file=new File("d:\\cqcca1.cer");
FileInputStream inputstream=null;
X509Certificate zert=null;
try {
inputstream=new FileInputStream(file);
zert=X509Certificate.getInstance(inputstream);
encryptString(zert.getPublicKey(), "aaaaaa");
} catch (FileNotFoundException e) {
System.out.println("aaaaaa");
e.printStackTrace();
} catch (CertificateException e) {
System.out.println("zzzz");
e.printStackTrace();
} finally{
try {
inputstream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static String encryptString(PublicKey publicKey, String plaintext) {
if (publicKey == null || plaintext == null) {
return null;
}
byte[] data = plaintext.getBytes();
try {
byte[] en_data = encrypt(publicKey, data);
System.out.println("=====");
String aaa=new String(Base64.encode(en_data));
System.out.println(aaa);
System.out.println("=====");
return new String(Hex.encodeHex(en_data));
} catch (Exception ex) {
}
return null;
}
public static byte[] encrypt(PublicKey publicKey, byte[] data) throws Exception {
Cipher ci = Cipher.getInstance(ALGORITHOM, DEFAULT_PROVIDER);
ci.init(Cipher.ENCRYPT_MODE, publicKey);
return ci.doFinal(data);
}
}
这个证书就是从ukey里面导出来的,只有公钥信息,实现的是用公钥对字符串aaaaaa加密,但是对比那个客户端页面对aaaaaa加密的结果不一样,都是用的rsa加密,也都变成了base64编码,哪里出问题了?
客户端的都打包在1.rar压缩文件里面了,有一个显示页面,是同过js获取Ukey里面的的证书(准确的说是获取浏览器里面的信息),然后自己讲证书给导出来了放在了服务器上面,然后服务器端代码是
TestZs.java
import it.sauronsoftware.base64.Base64;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;
import java.security.PublicKey;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.security.cert.CertificateException;
import javax.security.cert.X509Certificate;
import org.apache.commons.codec.binary.Hex;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class TestZs {
private static final Provider DEFAULT_PROVIDER = new BouncyCastleProvider();
private static final String ALGORITHOM = "RSA";
public static void main(String[] args) throws UnsupportedEncodingException {
File file=new File("d:\\cqcca1.cer");
FileInputStream inputstream=null;
X509Certificate zert=null;
try {
inputstream=new FileInputStream(file);
zert=X509Certificate.getInstance(inputstream);
encryptString(zert.getPublicKey(), "aaaaaa");
} catch (FileNotFoundException e) {
System.out.println("aaaaaa");
e.printStackTrace();
} catch (CertificateException e) {
System.out.println("zzzz");
e.printStackTrace();
} finally{
try {
inputstream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static String encryptString(PublicKey publicKey, String plaintext) {
if (publicKey == null || plaintext == null) {
return null;
}
byte[] data = plaintext.getBytes();
try {
byte[] en_data = encrypt(publicKey, data);
System.out.println("=====");
String aaa=new String(Base64.encode(en_data));
System.out.println(aaa);
System.out.println("=====");
return new String(Hex.encodeHex(en_data));
} catch (Exception ex) {
}
return null;
}
public static byte[] encrypt(PublicKey publicKey, byte[] data) throws Exception {
Cipher ci = Cipher.getInstance(ALGORITHOM, DEFAULT_PROVIDER);
ci.init(Cipher.ENCRYPT_MODE, publicKey);
return ci.doFinal(data);
}
}
这个证书就是从ukey里面导出来的,只有公钥信息,实现的是用公钥对字符串aaaaaa加密,但是对比那个客户端页面对aaaaaa加密的结果不一样,都是用的rsa加密,也都变成了base64编码,哪里出问题了?