问题描述
- 关于java自带RSA密钥包生成的私钥公钥加密不了,求大神帮忙看一下
-
我用JAVA生成了一个公钥如下:
System.out.println(publicKey.getPublicKey());输出:modulus:154308594144468705348294760484396264219304223307125368116140288659005422830
11489867478404495635728307309845313276126541903154766024976823588585215138754477992
96802915396931308077347778973425837411602815233405546695183536389616670156153124753
50767041053961957188628650343640790505255765999004862716823611888529public exponent: 65537
用这个modulus的值加密不了,我看网上的公钥都是这样子的5q4F3crOuN4OG68Dko7EZl5wYn+FokohvMm6m/NFmMJIGBhYnJ9/8JKQMxndka8GafQ9BU/LASFtvbrfNsyA6TfP2wMalewfWF0GBT5AviGouGqt9I9BMrvBfTttU5OMqD2rF+fQ23WMTi+UYzY/9kZB7Rys7A1oIFWnsnB0Jnc=AQAB
请问这个modulus是不是要转换一下 怎么转换呢?有没懂的大神帮下忙
解决方案
模加密是:BigInteger n=pbk.getModulus();
System.out.println("e= "+e);
System.out.println("n= "+n);
// 明文 m
byte ptext[]=s.getBytes("UTF8");
BigInteger m=new BigInteger(ptext);
// 计算密文c,打印
BigInteger c=m.modPow(e,n);
System.out.println("c= "+c);
下面的格式是Base64格式的:
RSAPublicKey pbk=(RSAPublicKey) x509.getPublicKey();
System.out.println(Base64.encode(pbk.getEncoded()));