- 浏览: 258780 次
- 性别:
- 来自: 深圳
最新评论
-
whizkid:
[img] private void enableNdefEx ...
android通过NFC读写数据 -
zhangminglife:
您好!不错,最近正在弄这个东西,能否把demo发给我一份谢谢了 ...
SSL双向认证java实现(转) -
water卡:
android如何调用显示和隐藏系统默认的输入法 -
water卡:
android如何调用显示和隐藏系统默认的输入法 -
sjp524617477:
good
生成android使用的BKS证书
在计算机软件开发世界中,编程语言种类极多,数据在各种语言的表现形式可能有所差异,但数据本身的处理可能,或者说本质上是完全一样的;比如数据在某个算法中的运算过程是一样的。在这里,我以加密与解密来作为例子说明。
在C++下,我使用OPENSSL库生成了RSA的公私钥对与DES加密之用的会话密钥,并将这三者及加密结果写入文件以备在Java环境下使用。
在C++程序中,我使用使用公钥来加密了DES的会话密钥,然后在Java下使用私钥来解密会话密钥;在运算结果中,我未做其它方面的码制转换,即按密钥的初始格式DER编码,数学运算结果也是按DER编码来实现。
在Java程序中,我从之前所存储的几个文件中取得密钥与加密结果来做解密。我使用了BC的JCE,即bcprov-jdk14-119.jar,在使用之前,需要先安装此JCE:
假设JDK:jdk1.4\jre\
把BC包放到JRE下的ext:jdk1.4\jre\lib\ext
修改文件jdk1.4\jre\lib\security\java.security:
#
# List of providers and their preference orders (see above):
#
security.provider.1=sun.security.provider.Sun
security.provider.2=com.sun.net.ssl.internal.ssl.Provider
security.provider.3=com.sun.rsajca.Provider
security.provider.4=com.sun.crypto.provider.SunJCE
security.provider.5=sun.security.jgss.SunProvider
security.provider.6=org.bouncycastle.jce.provider.BouncyCastleProvider
======================================================================
C++程序源码:
#include
#include
#include
//#define _RSA_KEY_PAIR_GENERATE_//密钥是否要生成 只需要在第一次运行时打开此宏
#define _RSA_KEY_PAIR_TOFILE_//密钥对是否要写入文件
#define MAX_RSA_KEY_LENGTH 512 //密钥的最大长度是512字节
#define PUBKEY_ENCRYPT
#define PRIKEY_DECRYPT
#pragma comment(lib, "../lib/libeay32.lib")
static const char * PUBLIC_KEY_FILE = "pubkey.key";
static const char * PRIVATE_KEY_FILE = "prikey.key";
int RsaKeyPairGen(void)
{
RSA *rsa = NULL;
#ifdef _RSA_KEY_PAIR_GENERATE_
//生成RSA密钥对:
rsa = RSA_new();
rsa = RSA_generate_key(1024, 0x10001, NULL, NULL);
#endif
//把密钥对写入文件,以后从文件里读取
#ifdef _RSA_KEY_PAIR_TOFILE_
unsigned char ucPubKey[MAX_RSA_KEY_LENGTH] = {0}, ucPriKey[MAX_RSA_KEY_LENGTH] = {0};
int len = i2d_RSAPublicKey(rsa,NULL);
unsigned char* pt = ucPubKey;
len = i2d_RSAPublicKey(rsa, &pt);
FILE *fpubkey = NULL;
fpubkey = fopen(PUBLIC_KEY_FILE, "wb");
if(fpubkey == NULL)
{
cout << "fopen pubkey.key failed!" << endl;
return 0x01;
}
fwrite(ucPubKey, 1, len, fpubkey);
fclose(fpubkey);
len = i2d_RSAPrivateKey(rsa,NULL);
unsigned char* pt2 = ucPriKey;
len = i2d_RSAPrivateKey(rsa,&pt2);
FILE *fprikey = NULL;
fprikey = fopen(PRIVATE_KEY_FILE, "wb");
if(fprikey == NULL)
{
cout << "fopen prikey.key failed!" << endl;
return 0x02;
}
fwrite(ucPriKey, 1, len, fprikey);
fclose(fprikey);
#endif
if(rsa != NULL)
{
RSA_free(rsa);
rsa = NULL;
}
return 0;
}
//从文件里读取私钥的数据,取得RSA格式的私钥:
int GetPriKey(unsigned char *pucPriKeyData, unsigned long KeyDataLen, RSA* *priRsa)
{
unsigned char *Pt = pucPriKeyData;
*priRsa = d2i_RSAPrivateKey(NULL, &Pt, KeyDataLen);
if(priRsa == NULL)
{
cout << "priRsa == NULL!" << endl;
return 0x22;
}
return 0;
}
//取得RSA格式的公钥:
int GetPubKey(unsigned char *pucPubKeyData,unsigned long KeyDataLen, RSA* *pubRsa)
{
unsigned char *Pt = pucPubKeyData;
*pubRsa = d2i_RSAPublicKey(NULL, &Pt, KeyDataLen);
if(pubRsa == NULL)
{
cout << "pubRsa == NULL!" << endl;
return 0x31;
}
return 0;
}
//公钥加密会话密钥:
int encSessionKeybyRsaPubKey(RSA *rsa, unsigned char *ucKey, unsigned long ulKeyLen,
unsigned char *outData, unsigned long *pulOutLen)
{
return (*pulOutLen = RSA_public_encrypt(ulKeyLen, ucKey, outData, rsa, 1));
}
//私钥解密会话密钥:
int decSessionKeybyRsaPriKey(RSA *rsa, unsigned char *InData, unsigned long ulDataLen,
unsigned char *ucKey, unsigned long *pulKeyLen)
{
return (*pulKeyLen = RSA_private_decrypt(ulDataLen, InData, ucKey, rsa, 1));
}
int main(int argc, char* argv[])
{
unsigned char ucKey[8] = {0x01, 0x03, 0x99, 0x4, \
0x80, 0x65, 0x34, 0x08};
unsigned char ucEncryptedKey[512] = {0}, ucDecryptedKey[512] = {0};
unsigned long encrypted_len = 0, decrypted_len = 0;
#ifdef _RSA_KEY_PAIR_GENERATE_
RsaKeyPairGen();
#endif
//取得公钥:
unsigned char ucPubKey[MAX_RSA_KEY_LENGTH] = {0};
FILE *fpubkey = NULL;
fpubkey = fopen(PUBLIC_KEY_FILE, "rb");
if(fpubkey == NULL)
{
cout << "fopen pubkey.key failed!" << endl;
return 0x03;
}
fseek(fpubkey, 0, SEEK_END);
int len_PK = ftell(fpubkey);
fseek(fpubkey, 0, SEEK_SET);
fread(ucPubKey, 1, len_PK, fpubkey);
fclose(fpubkey);
#ifdef PUBKEY_ENCRYPT
RSA *pRsaPubKey = NULL;
pRsaPubKey = RSA_new();
GetPubKey(ucPubKey, len_PK, &pRsaPubKey);
//公钥加密:
encSessionKeybyRsaPubKey(pRsaPubKey, ucKey, sizeof(ucKey), ucEncryptedKey, &encrypted_len);
//write to file:
FILE *fp = NULL;
fp = fopen("ucKey.data", "wb");
fwrite(ucEncryptedKey, 1, encrypted_len, fp);
fclose(fp);
if(pRsaPubKey != NULL)
{
RSA_free(pRsaPubKey); pRsaPubKey = NULL;
}
#endif
//取得私钥:
unsigned char ucPriKey[MAX_RSA_KEY_LENGTH] = {0};
FILE *fprikey = NULL;
fprikey = fopen(PRIVATE_KEY_FILE, "rb");
if(fprikey == NULL)
{
cout << "fopen prikey.key failed!" << endl;
return 0x02;
}
fseek(fprikey, 0, SEEK_END);
int len_SK = ftell(fprikey);
fseek(fprikey, 0, SEEK_SET);
fread(ucPriKey, 1, len_SK, fprikey);
fclose(fprikey);
#ifdef PRIKEY_DECRYPT
RSA *pRsaPriKey = NULL;
pRsaPriKey = RSA_new();
GetPriKey(ucPriKey, len_SK, &pRsaPriKey);
//私钥解密:
FILE *fp1 = NULL;
fp1 = fopen("ucKey.data", "rb");
int len = ftell(fp1);
fseek(fp1, 0, SEEK_SET);
fread(ucPriKey, 1, len_SK, fp1);
fclose(fp1);
decSessionKeybyRsaPriKey(pRsaPriKey, ucEncryptedKey, encrypted_len, ucDecryptedKey, &decrypted_len);
if(pRsaPriKey != NULL)
{
RSA_free(pRsaPriKey); pRsaPriKey = NULL;
}
//数据对比:
if(0 == memcmp(ucKey, ucDecryptedKey, decrypted_len))
{
cout << "OK!" << endl;
}
else
{
cout << "FAILED!" << endl;
}
#endif
return 0;
}
======================================================================
Java程序源码:
======================================================================
package jrsaencrypt;
import java.io.*;
import java.security.*;
import java.security.spec.*;
import java.security.PublicKey;
import java.security.PrivateKey;
import java.security.KeyFactory;
import javax.crypto.Cipher.*;
/**
*
Title:
*
Description:
*
Copyright: Copyright (c) 2005
*
Company:
* @author not attributable
* @version 1.0
*/
public class RsaKeyGen {
public RsaKeyGen() {
}
/**
* 生成RSA密钥对
* @return
*/
int generateRsaKeyPair() {
//generate an RSA key pair
try {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(1024);
KeyPair pair = keyGen.generateKeyPair();
System.out.println(pair.getPublic().getFormat());
System.out.println(pair.getPublic().getAlgorithm());
System.out.println("\nRSA public Key:");
byte[] bPubKey = pair.getPublic().getEncoded();
System.out.println(bPubKey.length);
for (int i = 0; i < bPubKey.length; i++) {
System.out.print(bPubKey[i] + " ");
}
System.out.println("\nRSA private Key:");
byte[] bPriKey = pair.getPrivate().getEncoded();
System.out.println(bPriKey.length);
for (int i = 0; i < bPriKey.length; i++) {
System.out.print(bPriKey[i] + " ");
}
}
catch (Exception e) {
e.printStackTrace();
}
return 0;
}
/**
* 从公钥数据取得公钥
* @param bPubKeyInput
* @return
*/
PublicKey getRsaPubKey(byte[] bPubKeyInput) {
byte[] bX509PubKeyHeader = {
48, -127, -97, 48, 13, 6, 9, 42, -122, 72, -122, -9, 13, 1, 1, 1, 5, 0,
3, -127, -115, 0};
try {
byte[] bPubKey = new byte[bPubKeyInput.length + bX509PubKeyHeader.length];
System.arraycopy(bX509PubKeyHeader, 0, bPubKey, 0,
bX509PubKeyHeader.length);
System.arraycopy(bPubKeyInput, 0, bPubKey, bX509PubKeyHeader.length,
bPubKeyInput.length);
X509EncodedKeySpec rsaKeySpec = new X509EncodedKeySpec(bPubKey);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
rsaPubKey = keyFactory.generatePublic(rsaKeySpec);
}
catch (Exception e) {
e.printStackTrace();
}
return rsaPubKey;
}
/**
* 从私钥数据取得私钥
* @param bPriKeyInput
* @return
*/
PrivateKey getRsaPriKey(byte[] bPriKeyInput) {
byte[] bX509PriKeyHeader = {
48, -126, 2, 117, 2, 1, 0, 48, 13, 6, 9, 42,
-122, 72, -122, -9, 13, 1, 1, 1, 5, 0, 4, -126, 2, 95};
try {
byte[] bPriKey = new byte[bX509PriKeyHeader.length + bPriKeyInput.length];
System.arraycopy(bX509PriKeyHeader, 0, bPriKey, 0,
bX509PriKeyHeader.length);
System.arraycopy(bPriKeyInput, 0, bPriKey, bX509PriKeyHeader.length,
bPriKeyInput.length);
PKCS8EncodedKeySpec rsaKeySpec = new PKCS8EncodedKeySpec(bPriKey);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
rsaPriKey = keyFactory.generatePrivate(rsaKeySpec);
}
catch (Exception e) {
e.printStackTrace();
}
return rsaPriKey;
}
/**
* 从文件里取得数据
* @param strFileName
* @param bFBytes
* @return
*/
int getBytesbyFileName(String strFileName, byte[] bFBytes) {
int fSize = 0;
try {
FileInputStream fIn = new FileInputStream(strFileName);
fSize = fIn.available();
System.out.print("file's size: ");
System.out.println(fSize);
fSize = fIn.read(bFBytes);
fIn.close();
}
catch (Exception e) {
e.printStackTrace();
}
return fSize;
}
/**
* 公钥加密
* @param bKey
* @return
*/
byte[] rsaPubKeyEncrypt(byte[] bKey) {
try {
// Provider prvd = Security.getProvider("BouncyCastle");
javax.crypto.Cipher rsaPKenc = javax.crypto.Cipher.getInstance(
"RSA/ECB/PKCS1Padding");
rsaPKenc.init(javax.crypto.Cipher.ENCRYPT_MODE, rsaPubKey);
bEncryptedData = rsaPKenc.doFinal(bKey);
}
catch (Exception e) {
e.printStackTrace();
}
return bEncryptedData;
}
/**
* 私钥解密
* @param bEncryptedKey
* @return
*/
byte[] rsaPriKeyDecrypt(byte[] bEncryptedKey) {
try {
javax.crypto.Cipher rsaSKDec = javax.crypto.Cipher.getInstance(
"RSA/ECB/PKCS1Padding");
rsaSKDec.init(javax.crypto.Cipher.DECRYPT_MODE, rsaPriKey);
byte[] bDecrypt = rsaSKDec.doFinal(bEncryptedKey);
// System.out.println("rsa decrypted result[before clean]:");
// for (int i = 0; i < bDecrypt.length; i++) {
// System.out.print(bDecrypt[i] + " ");
// }
// System.out.println();
int i = 0;
// for (i = bDecrypt.length; i > 1; i--) {
// if (bDecrypt[i-1] == 0) {
// System.out.println("i=" + i);
// break;
// }
// } //for
bDecryptedData = new byte[bDecrypt.length - i];
System.arraycopy(bDecrypt, i, bDecryptedData, 0, bDecrypt.length - i);
}
catch (Exception e) {
e.printStackTrace();
}
return bDecryptedData;
}
public static void main(String[] args) {
RsaKeyGen rsaKeyGen1 = new RsaKeyGen();
// rsaKeyGen1.generateRsaKeyPair();
// byte[] bPubKey = new byte[140];
// int len = rsaKeyGen1.getBytesbyFileName("pubkey.key", bPubKey);
// rsaKeyGen1.getRsaPubKey(bPubKey);
byte[] bPriKey = new byte[607];
int len2 = rsaKeyGen1.getBytesbyFileName("prikey.key", bPriKey);
rsaKeyGen1.getRsaPriKey(bPriKey);
// byte[] bKey = {
// 1, 2, 3, 4, 5, 6, 7, 8};
// //encrypt:
// byte[] bEncKey = rsaKeyGen1.rsaPubKeyEncrypt(bKey);
// System.out.println("rsa encrypted result:");
// for (int i = 0; i < bEncKey.length; i++) {
// System.out.print(bEncKey[i] + " ");
// }
// System.out.println();
byte[] bEncKey = new byte[128];
int len0 = rsaKeyGen1.getBytesbyFileName("ucKey.data", bEncKey);
byte[] bDecKey = rsaKeyGen1.rsaPriKeyDecrypt(bEncKey);
System.out.println("rsa decrypted result:");
for (int i = 0; i < bDecKey.length; i++) {
System.out.print(bDecKey[i] + " ");
}
System.out.println();
}
PublicKey rsaPubKey;
PrivateKey rsaPriKey;
byte[] bEncryptedData;
byte[] bDecryptedData;
}
在C++下,我使用OPENSSL库生成了RSA的公私钥对与DES加密之用的会话密钥,并将这三者及加密结果写入文件以备在Java环境下使用。
在C++程序中,我使用使用公钥来加密了DES的会话密钥,然后在Java下使用私钥来解密会话密钥;在运算结果中,我未做其它方面的码制转换,即按密钥的初始格式DER编码,数学运算结果也是按DER编码来实现。
在Java程序中,我从之前所存储的几个文件中取得密钥与加密结果来做解密。我使用了BC的JCE,即bcprov-jdk14-119.jar,在使用之前,需要先安装此JCE:
假设JDK:jdk1.4\jre\
把BC包放到JRE下的ext:jdk1.4\jre\lib\ext
修改文件jdk1.4\jre\lib\security\java.security:
#
# List of providers and their preference orders (see above):
#
security.provider.1=sun.security.provider.Sun
security.provider.2=com.sun.net.ssl.internal.ssl.Provider
security.provider.3=com.sun.rsajca.Provider
security.provider.4=com.sun.crypto.provider.SunJCE
security.provider.5=sun.security.jgss.SunProvider
security.provider.6=org.bouncycastle.jce.provider.BouncyCastleProvider
======================================================================
C++程序源码:
#include
#include
#include
//#define _RSA_KEY_PAIR_GENERATE_//密钥是否要生成 只需要在第一次运行时打开此宏
#define _RSA_KEY_PAIR_TOFILE_//密钥对是否要写入文件
#define MAX_RSA_KEY_LENGTH 512 //密钥的最大长度是512字节
#define PUBKEY_ENCRYPT
#define PRIKEY_DECRYPT
#pragma comment(lib, "../lib/libeay32.lib")
static const char * PUBLIC_KEY_FILE = "pubkey.key";
static const char * PRIVATE_KEY_FILE = "prikey.key";
int RsaKeyPairGen(void)
{
RSA *rsa = NULL;
#ifdef _RSA_KEY_PAIR_GENERATE_
//生成RSA密钥对:
rsa = RSA_new();
rsa = RSA_generate_key(1024, 0x10001, NULL, NULL);
#endif
//把密钥对写入文件,以后从文件里读取
#ifdef _RSA_KEY_PAIR_TOFILE_
unsigned char ucPubKey[MAX_RSA_KEY_LENGTH] = {0}, ucPriKey[MAX_RSA_KEY_LENGTH] = {0};
int len = i2d_RSAPublicKey(rsa,NULL);
unsigned char* pt = ucPubKey;
len = i2d_RSAPublicKey(rsa, &pt);
FILE *fpubkey = NULL;
fpubkey = fopen(PUBLIC_KEY_FILE, "wb");
if(fpubkey == NULL)
{
cout << "fopen pubkey.key failed!" << endl;
return 0x01;
}
fwrite(ucPubKey, 1, len, fpubkey);
fclose(fpubkey);
len = i2d_RSAPrivateKey(rsa,NULL);
unsigned char* pt2 = ucPriKey;
len = i2d_RSAPrivateKey(rsa,&pt2);
FILE *fprikey = NULL;
fprikey = fopen(PRIVATE_KEY_FILE, "wb");
if(fprikey == NULL)
{
cout << "fopen prikey.key failed!" << endl;
return 0x02;
}
fwrite(ucPriKey, 1, len, fprikey);
fclose(fprikey);
#endif
if(rsa != NULL)
{
RSA_free(rsa);
rsa = NULL;
}
return 0;
}
//从文件里读取私钥的数据,取得RSA格式的私钥:
int GetPriKey(unsigned char *pucPriKeyData, unsigned long KeyDataLen, RSA* *priRsa)
{
unsigned char *Pt = pucPriKeyData;
*priRsa = d2i_RSAPrivateKey(NULL, &Pt, KeyDataLen);
if(priRsa == NULL)
{
cout << "priRsa == NULL!" << endl;
return 0x22;
}
return 0;
}
//取得RSA格式的公钥:
int GetPubKey(unsigned char *pucPubKeyData,unsigned long KeyDataLen, RSA* *pubRsa)
{
unsigned char *Pt = pucPubKeyData;
*pubRsa = d2i_RSAPublicKey(NULL, &Pt, KeyDataLen);
if(pubRsa == NULL)
{
cout << "pubRsa == NULL!" << endl;
return 0x31;
}
return 0;
}
//公钥加密会话密钥:
int encSessionKeybyRsaPubKey(RSA *rsa, unsigned char *ucKey, unsigned long ulKeyLen,
unsigned char *outData, unsigned long *pulOutLen)
{
return (*pulOutLen = RSA_public_encrypt(ulKeyLen, ucKey, outData, rsa, 1));
}
//私钥解密会话密钥:
int decSessionKeybyRsaPriKey(RSA *rsa, unsigned char *InData, unsigned long ulDataLen,
unsigned char *ucKey, unsigned long *pulKeyLen)
{
return (*pulKeyLen = RSA_private_decrypt(ulDataLen, InData, ucKey, rsa, 1));
}
int main(int argc, char* argv[])
{
unsigned char ucKey[8] = {0x01, 0x03, 0x99, 0x4, \
0x80, 0x65, 0x34, 0x08};
unsigned char ucEncryptedKey[512] = {0}, ucDecryptedKey[512] = {0};
unsigned long encrypted_len = 0, decrypted_len = 0;
#ifdef _RSA_KEY_PAIR_GENERATE_
RsaKeyPairGen();
#endif
//取得公钥:
unsigned char ucPubKey[MAX_RSA_KEY_LENGTH] = {0};
FILE *fpubkey = NULL;
fpubkey = fopen(PUBLIC_KEY_FILE, "rb");
if(fpubkey == NULL)
{
cout << "fopen pubkey.key failed!" << endl;
return 0x03;
}
fseek(fpubkey, 0, SEEK_END);
int len_PK = ftell(fpubkey);
fseek(fpubkey, 0, SEEK_SET);
fread(ucPubKey, 1, len_PK, fpubkey);
fclose(fpubkey);
#ifdef PUBKEY_ENCRYPT
RSA *pRsaPubKey = NULL;
pRsaPubKey = RSA_new();
GetPubKey(ucPubKey, len_PK, &pRsaPubKey);
//公钥加密:
encSessionKeybyRsaPubKey(pRsaPubKey, ucKey, sizeof(ucKey), ucEncryptedKey, &encrypted_len);
//write to file:
FILE *fp = NULL;
fp = fopen("ucKey.data", "wb");
fwrite(ucEncryptedKey, 1, encrypted_len, fp);
fclose(fp);
if(pRsaPubKey != NULL)
{
RSA_free(pRsaPubKey); pRsaPubKey = NULL;
}
#endif
//取得私钥:
unsigned char ucPriKey[MAX_RSA_KEY_LENGTH] = {0};
FILE *fprikey = NULL;
fprikey = fopen(PRIVATE_KEY_FILE, "rb");
if(fprikey == NULL)
{
cout << "fopen prikey.key failed!" << endl;
return 0x02;
}
fseek(fprikey, 0, SEEK_END);
int len_SK = ftell(fprikey);
fseek(fprikey, 0, SEEK_SET);
fread(ucPriKey, 1, len_SK, fprikey);
fclose(fprikey);
#ifdef PRIKEY_DECRYPT
RSA *pRsaPriKey = NULL;
pRsaPriKey = RSA_new();
GetPriKey(ucPriKey, len_SK, &pRsaPriKey);
//私钥解密:
FILE *fp1 = NULL;
fp1 = fopen("ucKey.data", "rb");
int len = ftell(fp1);
fseek(fp1, 0, SEEK_SET);
fread(ucPriKey, 1, len_SK, fp1);
fclose(fp1);
decSessionKeybyRsaPriKey(pRsaPriKey, ucEncryptedKey, encrypted_len, ucDecryptedKey, &decrypted_len);
if(pRsaPriKey != NULL)
{
RSA_free(pRsaPriKey); pRsaPriKey = NULL;
}
//数据对比:
if(0 == memcmp(ucKey, ucDecryptedKey, decrypted_len))
{
cout << "OK!" << endl;
}
else
{
cout << "FAILED!" << endl;
}
#endif
return 0;
}
======================================================================
Java程序源码:
======================================================================
package jrsaencrypt;
import java.io.*;
import java.security.*;
import java.security.spec.*;
import java.security.PublicKey;
import java.security.PrivateKey;
import java.security.KeyFactory;
import javax.crypto.Cipher.*;
/**
*
Title:
*
Description:
*
Copyright: Copyright (c) 2005
*
Company:
* @author not attributable
* @version 1.0
*/
public class RsaKeyGen {
public RsaKeyGen() {
}
/**
* 生成RSA密钥对
* @return
*/
int generateRsaKeyPair() {
//generate an RSA key pair
try {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(1024);
KeyPair pair = keyGen.generateKeyPair();
System.out.println(pair.getPublic().getFormat());
System.out.println(pair.getPublic().getAlgorithm());
System.out.println("\nRSA public Key:");
byte[] bPubKey = pair.getPublic().getEncoded();
System.out.println(bPubKey.length);
for (int i = 0; i < bPubKey.length; i++) {
System.out.print(bPubKey[i] + " ");
}
System.out.println("\nRSA private Key:");
byte[] bPriKey = pair.getPrivate().getEncoded();
System.out.println(bPriKey.length);
for (int i = 0; i < bPriKey.length; i++) {
System.out.print(bPriKey[i] + " ");
}
}
catch (Exception e) {
e.printStackTrace();
}
return 0;
}
/**
* 从公钥数据取得公钥
* @param bPubKeyInput
* @return
*/
PublicKey getRsaPubKey(byte[] bPubKeyInput) {
byte[] bX509PubKeyHeader = {
48, -127, -97, 48, 13, 6, 9, 42, -122, 72, -122, -9, 13, 1, 1, 1, 5, 0,
3, -127, -115, 0};
try {
byte[] bPubKey = new byte[bPubKeyInput.length + bX509PubKeyHeader.length];
System.arraycopy(bX509PubKeyHeader, 0, bPubKey, 0,
bX509PubKeyHeader.length);
System.arraycopy(bPubKeyInput, 0, bPubKey, bX509PubKeyHeader.length,
bPubKeyInput.length);
X509EncodedKeySpec rsaKeySpec = new X509EncodedKeySpec(bPubKey);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
rsaPubKey = keyFactory.generatePublic(rsaKeySpec);
}
catch (Exception e) {
e.printStackTrace();
}
return rsaPubKey;
}
/**
* 从私钥数据取得私钥
* @param bPriKeyInput
* @return
*/
PrivateKey getRsaPriKey(byte[] bPriKeyInput) {
byte[] bX509PriKeyHeader = {
48, -126, 2, 117, 2, 1, 0, 48, 13, 6, 9, 42,
-122, 72, -122, -9, 13, 1, 1, 1, 5, 0, 4, -126, 2, 95};
try {
byte[] bPriKey = new byte[bX509PriKeyHeader.length + bPriKeyInput.length];
System.arraycopy(bX509PriKeyHeader, 0, bPriKey, 0,
bX509PriKeyHeader.length);
System.arraycopy(bPriKeyInput, 0, bPriKey, bX509PriKeyHeader.length,
bPriKeyInput.length);
PKCS8EncodedKeySpec rsaKeySpec = new PKCS8EncodedKeySpec(bPriKey);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
rsaPriKey = keyFactory.generatePrivate(rsaKeySpec);
}
catch (Exception e) {
e.printStackTrace();
}
return rsaPriKey;
}
/**
* 从文件里取得数据
* @param strFileName
* @param bFBytes
* @return
*/
int getBytesbyFileName(String strFileName, byte[] bFBytes) {
int fSize = 0;
try {
FileInputStream fIn = new FileInputStream(strFileName);
fSize = fIn.available();
System.out.print("file's size: ");
System.out.println(fSize);
fSize = fIn.read(bFBytes);
fIn.close();
}
catch (Exception e) {
e.printStackTrace();
}
return fSize;
}
/**
* 公钥加密
* @param bKey
* @return
*/
byte[] rsaPubKeyEncrypt(byte[] bKey) {
try {
// Provider prvd = Security.getProvider("BouncyCastle");
javax.crypto.Cipher rsaPKenc = javax.crypto.Cipher.getInstance(
"RSA/ECB/PKCS1Padding");
rsaPKenc.init(javax.crypto.Cipher.ENCRYPT_MODE, rsaPubKey);
bEncryptedData = rsaPKenc.doFinal(bKey);
}
catch (Exception e) {
e.printStackTrace();
}
return bEncryptedData;
}
/**
* 私钥解密
* @param bEncryptedKey
* @return
*/
byte[] rsaPriKeyDecrypt(byte[] bEncryptedKey) {
try {
javax.crypto.Cipher rsaSKDec = javax.crypto.Cipher.getInstance(
"RSA/ECB/PKCS1Padding");
rsaSKDec.init(javax.crypto.Cipher.DECRYPT_MODE, rsaPriKey);
byte[] bDecrypt = rsaSKDec.doFinal(bEncryptedKey);
// System.out.println("rsa decrypted result[before clean]:");
// for (int i = 0; i < bDecrypt.length; i++) {
// System.out.print(bDecrypt[i] + " ");
// }
// System.out.println();
int i = 0;
// for (i = bDecrypt.length; i > 1; i--) {
// if (bDecrypt[i-1] == 0) {
// System.out.println("i=" + i);
// break;
// }
// } //for
bDecryptedData = new byte[bDecrypt.length - i];
System.arraycopy(bDecrypt, i, bDecryptedData, 0, bDecrypt.length - i);
}
catch (Exception e) {
e.printStackTrace();
}
return bDecryptedData;
}
public static void main(String[] args) {
RsaKeyGen rsaKeyGen1 = new RsaKeyGen();
// rsaKeyGen1.generateRsaKeyPair();
// byte[] bPubKey = new byte[140];
// int len = rsaKeyGen1.getBytesbyFileName("pubkey.key", bPubKey);
// rsaKeyGen1.getRsaPubKey(bPubKey);
byte[] bPriKey = new byte[607];
int len2 = rsaKeyGen1.getBytesbyFileName("prikey.key", bPriKey);
rsaKeyGen1.getRsaPriKey(bPriKey);
// byte[] bKey = {
// 1, 2, 3, 4, 5, 6, 7, 8};
// //encrypt:
// byte[] bEncKey = rsaKeyGen1.rsaPubKeyEncrypt(bKey);
// System.out.println("rsa encrypted result:");
// for (int i = 0; i < bEncKey.length; i++) {
// System.out.print(bEncKey[i] + " ");
// }
// System.out.println();
byte[] bEncKey = new byte[128];
int len0 = rsaKeyGen1.getBytesbyFileName("ucKey.data", bEncKey);
byte[] bDecKey = rsaKeyGen1.rsaPriKeyDecrypt(bEncKey);
System.out.println("rsa decrypted result:");
for (int i = 0; i < bDecKey.length; i++) {
System.out.print(bDecKey[i] + " ");
}
System.out.println();
}
PublicKey rsaPubKey;
PrivateKey rsaPriKey;
byte[] bEncryptedData;
byte[] bDecryptedData;
}
发表评论
-
Android RSA加密解密
2016-11-11 10:44 668Android RSA加密解密 2014-11-24 10:3 ... -
JAVA和.NET使用DES对称加密的区别
2014-05-10 18:54 828JAVA和.NET的系统类库里都有封装DES对称加密的实现方式 ... -
openssl的man中文文档
2012-05-16 19:06 2331openssl简介-证书 http://bbs.chinau ... -
openssl 证书转换
2012-05-16 19:02 1805证书转换 PKCS 全称是 Public-Key Cryp ... -
JKS、BKS、PKCS12证书之间转换
2012-05-11 12:04 9383常用的证书库: JKS和JCEKS是Java密钥库(KeySt ... -
生成android使用的BKS证书
2012-05-10 12:21 2898生成android使用的BKS证书 android 系统中 ... -
SSL Socket双向认证的实现
2012-05-09 10:57 3235SSL Socket双向认证的实现 ... -
SSL双向认证java实现(转)
2012-03-01 18:18 966本文通过模拟场景,介绍SSL双向认证的java实现 默认的情 ... -
SSL的工作流程简介(转)
2012-03-01 16:47 9631:客户端的浏览器向服务器传送客户端 SSL 协议的版本号,加 ... -
SSL工作原理
2011-07-11 09:58 943SSL 是一个安全协议,它 ...
相关推荐
本文将详细讲解如何在Windows操作系统上利用OpenSSL工具生成RSA公钥和私钥。 首先,OpenSSL是一个强大的安全套接字层密码库,包含各种主要的密码算法、常用的密钥和证书封装管理功能以及SSL协议,并提供丰富的应用...
2. 接下来,使用私钥生成公钥: ``` openssl rsa -pubout -in private_key.pem -out public_key.pem ``` 这将从`private_key.pem`中提取公钥,并将其保存到`public_key.pem`文件中。 对于支付宝RSA应用,私钥通常...
总结来说,“openssl_支付宝私钥公钥生成.rar”提供了生成支付宝兼容密钥对的方法,通过`openssl`工具,商家可以自动生成并管理用于安全通信的公私钥。正确使用这些密钥对于确保在线支付的安全至关重要。
1.go生成rsa证书 自签名证书 ...3.go生成公钥私钥 4.对自已生成的公钥私钥进行签名,得到签名证书crt 5.通过设置云dns账号直接生成freessl证书 6.openssl一些惯用方法介绍 7.如生成pfx格式的证书包文件的方法
CA在验证信息无误后,会使用自己的私钥对用户公钥等信息进行签名,生成证书。证书一旦被吊销,就不能再用于安全通信,CA会把该证书加入到证书吊销列表(CRL)中,或者使用在线证书状态协议(OCSP)来验证证书是否...
"openssl工具(RSA网络通信加密,需要的,生成公钥私钥)" 这个标题提到了两个关键概念,一个是`openssl`工具,另一个是`RSA`加密算法,主要用于在网络通信中生成公钥和私钥对。 **`openssl`工具:** `openssl`是...
C# RSA加密、解密、加签、验签、支持JAVA格式公钥私钥、PEM格式公钥私钥、.NET格式公钥私钥 对应文章: http://blog.csdn.net/gzy11/article/details/54573973
1. OpenSSL官网 官方下载地址: https://www.openssl.org/source/ 2. Windows安装方法 OpenSSL官网没有提供windows版本的安装包,可以选择其他开源平台提供的工具。例如 ...
在命令行中,使用OpenSSL可以轻松生成RSA公钥和私钥对。以下是一般步骤: 1. 打开命令提示符或PowerShell,定位到OpenSSL的bin目录。 2. 输入以下命令生成一个2048位的RSA私钥: ``` openssl genpkey -algorithm ...
在给定的"RSA.zip"压缩包中,可能包含了C++代码示例,用于演示如何使用RSA算法进行加密和解密操作,以及如何生成和管理公钥私钥对。文件名"RSA"可能是源代码文件或执行程序。通过学习和理解这些代码,开发者可以更好...
这个版本的OpenSSL支持RSA密钥生成,是生成和管理RSA公钥和私钥对的重要工具。 **RSA密钥生成** 生成RSA密钥对通常包括以下步骤: 1. **选择两个大素数**:随机选取两个足够大的素数p和q,它们的长度通常以位数...
在Linux下,使用OpenSSL生成RSA密钥的步骤如下: 1. 安装OpenSSL:首先,确保系统已经安装了OpenSSL。如果没有,可以通过包管理器(如`apt-get`或`yum`)来安装。例如,在Ubuntu上,可以运行`sudo apt-get install ...
5. **创建PKEY上下文**:PKEY(Public Key)上下文是OpenSSL中处理公钥和私钥操作的对象。它用于执行加密、解密、签名和验证等操作。 6. **处理数据并写入输出**:根据输入的公钥或私钥,程序可能会执行转换、格式...
4. 密钥和证书管理:OpenSSL提供了命令行工具(如`openssl genpkey`、`openssl req`和`openssl x509`),用于生成、管理和操作密钥对及X.509数字证书。 5. 哈希函数:OpenSSL支持MD5、SHA1、SHA256等哈希函数,哈希...
Java基于command调用openssl生成私钥证书 Java是一种广泛使用的编程语言,而openssl是一种加密工具。今天,我们将介绍如何使用Java基于command调用openssl生成私钥证书。 什么是私钥证书? 私钥证书是一种数字...
用管理员身份打开CMD,进入安装目录的bin目录,CMD里录入openssl.exe genrsa -out rsa.private 1024 生成私钥文件,执行openssl.exe rsa -in rsa.private -out rsa.public -pubout -outform PEM 生成公钥文件。...
本教程将详细讲解如何使用OpenSSL创建CA、生成公私密钥对以及证书,以及如何进行加密解密、加签验签操作。 首先,让我们了解基本概念: 1. **CA(证书颁发机构)**:它是可信的第三方机构,负责验证并签发数字证书...
在本文中,我们将深入探讨如何使用OpenSSL生成公钥和私钥。 ### 公钥和私钥基础 在加密领域,公钥和私钥是基于非对称加密技术的核心概念。非对称加密允许用户拥有两个密钥:一个公钥和一个私钥。公钥可以公开分享...
总的来说,“支付宝生成私钥公钥工具”是支付宝开发者为了实现安全支付接口而必不可少的一步,通过openssl这样的工具,可以轻松生成符合要求的RSA密钥对,为支付宝支付提供坚实的安全基础。同时,了解并正确使用这些...
使用`X509_sign`函数,我们用私钥对证书进行签名。 6. **保存证书和私钥**:最后,我们需要将生成的证书和私钥保存为文件,通常是PEM格式,使用`PEM_write_X509`和`PEM_write_RSAPrivateKey`函数。 在VS2017环境中...