-
-
[已解决]
[悬赏]一个RSA的c++和Java版本不能互通,头发都白了
100.00雪花
-
发表于:
2017-8-31 14:31
5547
-
[已解决] [悬赏]一个RSA的c++和Java版本不能互通,头发都白了
100.00雪花
这是客户端代码,公钥加密,私钥解密。公钥由服务器端提供,加密后数据用Base64编码发送出去。用Java代码是没有问题的,用C++版本对方就解密失败了。
C++平台使用的是OPenSSL库。都使用PKCS1填充模式。
找了几天没找出问题,什么大端,填充都看了,貌似都不是这些问题,确实找不出原因了
Java平台的代码如下(只贴核心代码,或者称为伪代码)
//这是公钥
byte[] arrayOfBytePublic = Base64.decode("
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw+D4pTERUemPtGIsteri\r
eWoTbRTNHgZ0DN9Z4qfrS03eWP339/JBkGyKwlBykCzYDt8L0YMW/1Xg1appYGvf\r
o4mnVvxzIbc1tuanH1Z+wnXbrHqgW5ndlMjZ328mDyDIRHPmzA8FMYSvPGJtnrv4\r
H9Tc+CeKrmiWskbzxBg4T/MUxYsk/+WWg6y5dUU61rwr19HyBZKq93xH8IOctgeR\r
xsf6oItQJ1y1Nd55XVEul2Lh5KtTZzZMWAwreIq/EhZJH0XdbOeHhy78/fMmQtkK\r
pJqLpRMTas1ZBWmVnqHxgBQcvheOu6cNGJYbadisJDk7Yo6MW3M7kRbybeU7C9yf\r
7QIDAQAB", 0);
X509EncodedKeySpec localX509EncodedKeySpec = new X509EncodedKeySpec(arrayOfBytePublic);
PublicKey PublicKey = KeyFactory.getInstance("RSA").generatePublic(localX509EncodedKeySpec);
Cipher localCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
localCipher.init(1, PublicKey);
Object localObject = arraymingwen.length;//明文
ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
//下面的是分段加密,2048bit的。最大长度是256
int m = 0;
if (localObject - n > 0)
{
if (localObject - n > (256-11));
int i3;
for (localObject1 = localCipher.doFinal(arrayOfByte1, n, (256-11)); ; localObject1 = localCipher.doFinal(arrayOfByte1, localObject1, i3))
{
int n = localObject1.length;
localByteArrayOutputStream.write(localObject1, 0, n);
int i1 = m + 1;
Object localObject3 = i1 * (256-11);
int i2 = i1;
localObject1 = localObject3;
m = i2;
break;
i3 = localObject - localObject1;
}
}
localObject1 = localByteArrayOutputStream.toByteArray();
localByteArrayOutputStream.close();
//返回Base64的字符串
return Base64.encodeToString(localObject1, 2);
windwos7平台的c++版本,Intel处理器,加密后用Base64编码,代码如下
int RsaEncrypt(CString str,char* encrypted)
{
CString key="-----BEGIN PUBLIC KEY-----\n" \
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw+D4pTERUemPtGIsteri\n" \
"eWoTbRTNHgZ0DN9Z4qfrS03eWP339/JBkGyKwlBykCzYDt8L0YMW/1Xg1appYGvf\n" \
"o4mnVvxzIbc1tuanH1Z+wnXbrHqgW5ndlMjZ328mDyDIRHPmzA8FMYSvPGJtnrv4\n" \
"H9Tc+CeKrmiWskbzxBg4T/MUxYsk/+WWg6y5dUU61rwr19HyBZKq93xH8IOctgeR\n" \
"xsf6oItQJ1y1Nd55XVEul2Lh5KtTZzZMWAwreIq/EhZJH0XdbOeHhy78/fMmQtkK\n" \
"pJqLpRMTas1ZBWmVnqHxgBQcvheOu6cNGJYbadisJDk7Yo6MW3M7kRbybeU7C9yf\n" \
"7QIDAQAB\n" \
"-----END PUBLIC KEY-----\n";
int padding = RSA_PKCS1_PADDING;
RSA * rsa = createRSA((unsigned char *)key.GetBuffer(0), 1);
//分段加密
int inputLen=str.GetLength();
char* pSource=str.GetBuffer(0);
str.ReleaseBuffer();
int offSet = 0;
int i = 0;
int result=0;
while (inputLen - offSet > 0)
{
if (inputLen - offSet > (256-11))
{
int retLen = RSA_public_encrypt(245, (const unsigned char*)(pSource+offSet),(unsigned char*) encrypted, rsa, padding);
encrypted=encrypted+retLen;
result+=retLen;
}
else
{
int retLen = RSA_public_encrypt(inputLen - offSet, (const unsigned char*)(pSource+offSet),(unsigned char*) encrypted, rsa, padding);
encrypted=encrypted+retLen;
result+=retLen;
}
i++;
offSet = i * (256-11);
}
return result;
}
//下面的代码用base64编码加密后的数据
CBase64 base;
string strbase= base.Encode((const unsigned char*)encoded,nLen);
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课