搜了下网上的RSA的java实现,大都是直接生成一对密钥,然后加解密 但是我的情况是要根据指数e和模m来加解密,根据网上的资料 设A为明文,B为密文,则:A=B^e1 mod n;B=A^e2 mod n;e1和e2可以互换使用,即:A=B^e2 mod n;B=A^e1 mod n; 下面是我的java编码: public static void main(String[] args) throws Exception{
//模
String mStr = "00e85d00a5ab79000966815148c023e5f78a273fdb36b6ab245644e851295532d7821b13e3f935f965e56b354ac8c91255ec441c5970f306e2d86b227e81670c40b7dbcd51334c9729cd0062ad55f32631841e9c11981dd19cc8b99fbb7d84717b287cc069a72a5a5bfa2101250686e4af4a6f55c96257758aac9594452212fec9";
//指数e(加密)
String eStr = "00a2f68efca1dd13dc83ea902270b6fc92f66e7e8774c0fa185af4aae63911cabd803f135d20abed04bcb8cac06f3f2dbfdb1b0db2e0d760edf99071b9bd719311fe820dd67fd1226222be45eed388620f42a39526e5d58ee5d8accab218df4a28e7c36dff89bb52d331d590c071759c877934abab13154ce28059d669498fd801";
//明文
String str = "0921177345413973874761739001010010D10121200012339900031";
System.out.println("原始明文为:"+ str);
//指数和模转asc码
String mASC = toASC(mStr);
String eASC = toASC(eStr);
BigInteger mBig = new BigInteger(mASC);
BigInteger eBig = new BigInteger(eASC);
String s = toASC(str);
BigInteger sBig = new BigInteger(s);
BigInteger result2 = sBig.modPow(eBig, mBig);
System.out.println("生成密文为: "+result2);
}
//字符串,各字符转成asc码
public static String toASC(String s) {
char[] c = s.toCharArray();
String str = "";
for (int i = 0; i < c.length; i++) {
str += (int) c[i];
}
return str;
}
问题: 1、指数、模、以及明文,都是字符串形式的,是先将每个字符转换成ASC码,再根据公式计算么 2、根据要求,我出来的RSA密文要是128个字节,我上面的编码就是出来一大串数字,怎么改 3、我的代码计算RSA,正确么?要生成128字节的密文,应该怎么写?
//模
String mStr = "00e85d00a5ab79000966815148c023e5f78a273fdb36b6ab245644e851295532d7821b13e3f935f965e56b354ac8c91255ec441c5970f306e2d86b227e81670c40b7dbcd51334c9729cd0062ad55f32631841e9c11981dd19cc8b99fbb7d84717b287cc069a72a5a5bfa2101250686e4af4a6f55c96257758aac9594452212fec9";
//指数e(加密)
String eStr = "00a2f68efca1dd13dc83ea902270b6fc92f66e7e8774c0fa185af4aae63911cabd803f135d20abed04bcb8cac06f3f2dbfdb1b0db2e0d760edf99071b9bd719311fe820dd67fd1226222be45eed388620f42a39526e5d58ee5d8accab218df4a28e7c36dff89bb52d331d590c071759c877934abab13154ce28059d669498fd801";
//明文
String str = "0921177345413973874761739001010010D10121200012339900031";
System.out.println("原始明文为:"+ str);
//指数和模转asc码
String mASC = toASC(mStr);
String eASC = toASC(eStr);
BigInteger mBig = new BigInteger(mASC);
BigInteger eBig = new BigInteger(eASC);
String s = toASC(str);
BigInteger sBig = new BigInteger(s);
BigInteger result2 = sBig.modPow(eBig, mBig);
System.out.println("生成密文为: "+result2);
}
//字符串,各字符转成asc码
public static String toASC(String s) {
char[] c = s.toCharArray();
String str = "";
for (int i = 0; i < c.length; i++) {
str += (int) c[i];
}
return str;
}
问题: 1、指数、模、以及明文,都是字符串形式的,是先将每个字符转换成ASC码,再根据公式计算么 2、根据要求,我出来的RSA密文要是128个字节,我上面的编码就是出来一大串数字,怎么改 3、我的代码计算RSA,正确么?要生成128字节的密文,应该怎么写?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货