C OPENSSL RSA 与JAVA RSA对接

bobocai 2017-08-09 08:11:58
手头有个项目,我们用对方提供的公钥加密,他们解密,我在linux下用openssl加密,对方总是解密不了,后来我把他们的加密
代码也拿过来试试,结果加密结果也不一样

---------------------------------------------------------------------------------
现在发现点眉目,在java端对c的“”123“”加密后结果串做解析,发现解析的结果是313233后面跟122个0,而正确的解析结果应该是125个0后面跟123,网上有搜到要在加密串的后面填充'\0',
但是没效果,这个怎么玩?望各位指点,在线等
---------------------------------------------------------------------------------

由于都是纯打字出来的,所以可能有些错误,排版也乱,望见谅,具体如下:

c代码:

#define MODULUS /*对方给的公钥key,太长,纯打字没法打出来*/
#define PRIVATE_EXPONENT /*私钥*/
#define PUBLIC_EXPONENT RSA_F4

int main()
{
int ret,flen;
BIGNUMN *bnn,*bne,*bnd;
unsigned char in[257]={'\0'};
unsigned char *out;

memset(in,0,sizeof(in));
strcpy(in,"12345678");


bnn=BN_new();
bne=BN_new();
bnd=BN_new();

BN_hex2bn(&bnn,MODULUS);
BN_set_word(bne,PUBLIC_EXPONENT);

RSA *r=RSA_new();
r->n=bnn;
r->e=bne;
r->d=bnd;

flen=RSA_size(r);
out=(char*)malloc(flen);
memset(out,0,flen);

ret=RSA_public_encrypt(flen,in,out,r,RSA_NO_PADDING);
if(ret<0)
{
return -1;
}
int i=0;
for(i=0;i<ret;i++)
{
printf("0x%02x,",*out);
out++;
}
printf("\n");
return 0;
}


对方给的java的部分代码:


import java.match.BigInteger;
import java.security.Key;
import java.security.KeyFactory;
import java.security.MessageDigest;
import java.security.Signature;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.KeySpec;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.security.Key;


import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.Cipher;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SecurityUtil{
/**
N:rsa模modulus
E:rsa公钥指数
D:rsa私钥指数
data:输入数据
mode:0-加密 1-解密
type:0-私钥加密 公钥解密 1-公钥加密 私钥解密
**/
public static String generateRSA(String n,String e,String d,String data,int mode,int type)
{
try
{
//判断加密还是解密
int opmode=(mode==0)?Cipher.ENCRYPT_MODE:Cipher.DECRYPT_MODE;
KeyFactor keyFactory=KeyFactory.getInstance("RSA");
BigInteger bigN=new BigInteger(n);
Key key=null;
if(mode !=type)
{//生成公钥
BigInteger bigE=new BigInteger(e);
KeySpec keySpec=new RSAPublicKeySpec(bigN,bigE);
key=keyFactory.generatePublic(keySpec);
}
else//生成私钥
{
BigInteger bigD=new BigInteger(d);
KeySpec keySpec=new RSAPrivateKeySpec(bigN,bigD);
key=keyFactory.generatePrivate(keySpec);
}

Cipher cipher=Cipher.getInstance("RSA/ECB/NoPadding");
cipher.init(opmode,key);
return (Converts.bytesToHex(cipher.doFinal(Converts.stringToBytes(data)))).toUpperCase();//Converts的转化函数就不打字出来了
}catch(Exception e1)
{
logger.debug(e1.getMessage());
}
}

}

public static void main(String args[])
{
String A="12345678";
String privateKeyN= "aaaaa" ;//公钥,太长,纯打字没法打出来
String privateKeyE="10001";
BigInteger xe=new BigInteger(privateKeyE,16);
BigInteger xn=new BigInteger(privateKeyN,16);
String rand=SecurityUtil.generateRSA(xn+"",xe+"",xe+"",A,0,1);
}






...全文
237 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
bobocai 2017-08-10
  • 打赏
  • 举报
回复
引用 2楼luminji 的回复:
既然有代码就好办啊,有的时候不同的语言环境下加密的串就会尾部加上些自己平台的东西,比如填充些没必须要的数据,但是主体没有问题。 如果对方的代码不能改,就只有改你的代码以符合对方的要求。 建议你用java写一版解密的,然后调试成功后改成c版本的。
谢谢
  • 打赏
  • 举报
回复
既然有代码就好办啊,有的时候不同的语言环境下加密的串就会尾部加上些自己平台的东西,比如填充些没必须要的数据,但是主体没有问题。 如果对方的代码不能改,就只有改你的代码以符合对方的要求。 建议你用java写一版解密的,然后调试成功后改成c版本的。
bobocai 2017-08-10
  • 打赏
  • 举报
回复

50,528

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧