190 likes | 365 Views
RSA 中的密钥对生成. 通过前几天的学习 , 我们已经知道要生成 RSA 的密钥,需要做如下工作 : 确定两个大素数 p,q( 到少 512bit) 此处涉及到素数的判定(该算法是比较费时的). 计算 n=pq; 计算 选定一个 e, 使 最后得到公钥 : n,e 最后得到私钥 : d ( ). RSA 中的密钥对生成. KeyPairGenerator
E N D
RSA中的密钥对生成 通过前几天的学习,我们已经知道要生成RSA的密钥,需要做如下工作: • 确定两个大素数p,q(到少512bit) • 此处涉及到素数的判定(该算法是比较费时的). • 计算n=pq; • 计算 • 选定一个e,使 • 最后得到公钥: n,e • 最后得到私钥: d ( )
RSA中的密钥对生成 • KeyPairGenerator • The KeyPairGenerator class is an engine class used to generate pairs of public and private keys • Creating the Key Pair Generator try{ //取得KeyPairGenerator实例 KeyPairGenerator keyGen=KeyPairGenerator.getInstance("RSA"); System.out.println("Key Test program is ok"); } catch(NoSuchAlgorithmException e){ System.out.println("Error: "+e.getMessage()); }
RSA中的密钥对生成 • Initializing the Key Pair Generator SecureRandom random = SecureRandom.getInstance(“SHA1PRNG”, “SUN”); random.setSeed(userSeed); //这两条语句可省略,省略时系统采用 keyGen.initialize(1024, random); //默认值
RSA中的密钥对生成 • The KeyPair Class KeyPair类是用于封装密钥对的类. KeyPair pair = keyGen.generateKeyPair();
RSA中的密钥对生成 • The PublicKey Class 封装了公钥体制中的公钥(n,e)
RSA中的密钥对生成 • The PrivateKey Class 封装了公钥体制中的私钥(n,d,p,q)
RSA中的密钥对的保不存 • 因数字签名及验证要需要重复使用密钥对,因此需将密钥对存入文件中备用(存10进制数)。 • KeyFactory 用于实验密钥对象与密钥的标准表达方式之间的转换. //通过KeyFactory 类读取密钥的详细信息 RSAPublicKeySpec; RSAPrivateKeySpec;
用于测试RSA密钥对的代码 // 取得KeyPairGenerator实例 KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); // 生成随机数对象 SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN"); // 初始化keyGen为产生一对1024位的密钥的对象 keyGen.initialize(1024, random); // 生成密钥对 KeyPair pair = keyGen.generateKeyPair(); // 读取公钥对象和私钥对象 PublicKey puKey = pair.getPublic(); PrivateKey prKey = pair.getPrivate(); //将公钥和私钥输出 System.out.println(puKey.toString()); System.out.println(prKey.toString());
Java中公钥密码体制API • Sun RSA public key, 1024 bits • modulus( n) : 145334580766753352968271624143227642903180026858070538178093842654655131896488508859021170467602039725331815700222176862989556902870844823932940664684208523041952356061062674190324733884210594543025749003442008772730874286810845100551163211606567048423518752061867186661617099735966587084128589872797083364747 • public exponent( e): 65537 • Sun RSA private CRT(中国剩余定理) key, 1024 bits • modulus(n ): 145334580766753352968271624143227642903180026858070538178093842654655131896488508859021170467602039725331815700222176862989556902870844823932940664684208523041952356061062674190324733884210594543025749003442008772730874286810845100551163211606567048423518752061867186661617099735966587084128589872797083364747 • public exponent( e ): 65537 • private exponent( d ) : 123650928314896931628818186529780584107273984429851154893759778027387819085650895913303346874330868563479816159248796247973430327100346926738740536218117730278249237671789995775819474273027581511787552426991635086254315333676872987600640893099168403376116367516517690236495972293965947291133168132306425021585 • prime p: 12836193223793558297927305428496631019420276059582307483685094836716895113592759865558860520415443996103674401559926195951777679843538568226126094329161913 • prime q: 11322249379773806480799329481584580801659487779713145666093610874564424578061919183694263286905364957495175786438352752223906032465534494985244811289566819 • prime exponent p: 9148115978816938941686233038570458596274819322137278722560991875406808674040267864575090381418803770786186738386857391005381389023790468073571745545755305 • prime exponent q: 6844970668111417772796286576893398781490616984604338685571432266675573587551937073365433353532774844453001508071379135387084866751864476949057550209865067 • crt coefficient: 12215131375839181768650789125852251387959893277517099645583985397510200970338820228475780242328195082955827984916897355413623358654311131059893783646946460
用于保存公钥(或私钥)的代码 //通过KeyFactory 类读取密钥的详细信息 KeyFactory keyFactory; RSAPublicKeySpec rsaPublicKeySpec; RSAPrivateKeySpec rsaPrivateKeySpec; //读取KeyFactory实例 keyFactory = KeyFactory.getInstance("RSA"); //取得KeySpec对象 rsaPublicKeySpec =(RSAPublicKeySpec)keyFactory.getKeySpec(puKey,RSAPublicKeySpec.class); rsaPrivateKeySpec =(RSAPrivateKeySpec)keyFactory.getKeySpec(prKey,RSAPrivateKeySpec.class);
用于保存公钥(或私钥)的代码 //读取RSA体制中的n,e,d BigInteger n=rsaPublicKeySpec.getModulus(); BigInteger e=rsaPublicKeySpec.getPublicExponent(); BigInteger d=rsaPrivateKeySpec.getPrivateExponent(); byte[] array_n=n.toByteArray(); byte[] array_e=e.toByteArray(); byte[] array_d=d.toByteArray();
用于保存公钥(或私钥)的代码 FileOutputStream os=new FileOutputStream("keypair.dat"); BufferedOutputStream bos=new BufferedOutputStream(os); bos.write(array_n.length); bos.write(array_n); bos.write(array_e.length); bos.write(array_e); bos.write(array_d.length); bos.write(array_d); bos.close();
RSA中的密钥对的读取代码 //测试读取密钥对 FileInputStream is=new FileInputStream("keypair.dat"); BufferedInputStream bis=new BufferedInputStream(is); int rn=bis.read(); byte[] arr_n=newbyte[rn]; bis.read(arr_n); rn=bis.read(); byte[] arr_e=newbyte[rn]; bis.read(arr_e); rn=bis.read(); byte[] arr_d=newbyte[rn]; bis.read(arr_d); BigInteger bnd=new BigInteger(arr_d); 依此类推,其它几个大整数也可生成。
package digitalSignature; import java.security.*; import java.security.spec.*; import java.math.*; publicclass TestKeyFactory { /** * @param args */ publicstaticvoid main(String[] args) { // TODO Auto-generated method stub KeyFactory keyFactory; RSAPublicKeySpec rsaPublicKeySpec; RSAPrivateKeySpec rsaPrivateKeySpec; PublicKey puKey; PrivateKey prKey; BigInteger modulus; BigInteger publicExponent; BigInteger privateExponent; try { // 读取KeyFactory实例 keyFactory = KeyFactory.getInstance("RSA"); // 生成大素数n对象(bigInteger) modulus = new BigInteger( "99088426942020350338040575167799354215521985930809728927072210589276544908798704067227074003143482215405484723700901818239870700389911837519983198665770826399629190926127255679465937072030430667011751449223370297974871901357097820367416435228232213549910308078793020749883422710315131220984840513736926929187"); // 生成公钥指数e publicExponent = new BigInteger("65537"); // 生成私钥d privateExponent = new BigInteger( "35889070148507515693029420522118090101985830600113379244425485798215623945254662838454141257802719644583670154057834604262627383236268631264962405766657027829284982577266629738492444164319964326342566063539272771497026747971277605799490434921044951013674084477470337244317547033260901498781303801882308400609");
// 生RSAPublicKeySpec 对象 rsaPublicKeySpec = new RSAPublicKeySpec(modulus, publicExponent); // 生RSAPrivateKeySpec对象 rsaPrivateKeySpec = new RSAPrivateKeySpec(modulus, privateExponent); // 生成PublicKey对象 puKey = keyFactory.generatePublic(rsaPublicKeySpec); // 生成PrivateKey对象 prKey = keyFactory.generatePrivate(rsaPrivateKeySpec); System.out.println(puKey.toString());
System.out.println(prKey.toString()); System.out.println(keyFactory.getProvider().getName()); System.out.println("bit count :" + modulus.bitCount()); System.out.println("bit length: " + modulus.bitLength()); } catch (NoSuchAlgorithmException e) { System.out.println(e.getMessage()); } catch (InvalidKeySpecException e) { System.out.println(e.getMessage()); } } }
SHA数字摘要的计算 import java.io.*; import java.security.*; import tool.Tools; publicclass TestSHA { publicstaticvoid main(String[] args) { try { byte[] a = newbyte[512]; int len; // 打开相应的文件? FileInputStream in = new FileInputStream("GEF-ALL-3.2M3.zip"); BufferedInputStream read = new BufferedInputStream(in);
SHA数字摘要的计算 // 构造MessageDigest对象 MessageDigest sha = MessageDigest.getInstance("SHA"); while ((len = read.read(a, 0, 512)) > 0) { sha.update(a, 0, len); } byte[] hash = sha.digest(); System.out .println("计算出的摘要为:" + sha.getDigestLength() * 8 + " bits"); System.out.println(Tools.toHexString(hash)); // 关闭相应的文件 in.close();
SHA数字摘要的计算 System.out.println(sha.getAlgorithm()); } catch (FileNotFoundException e) { System.out.println(e.getMessage()); } catch (NoSuchAlgorithmException e) { System.out.println(e.getMessage()); } catch (IOException e) { System.out.println(e.getMessage()); } } }