1 / 14

Java 中公钥密码体制 API

Java 中公钥密码体制 API. RSA 中的密钥生成 通过学习 , 我们已经知道要生成 RSA 的密钥,需要做如下工作 : 确定两个大素数 p,q( 至少 512bit) 此处涉及到素数的判定(该算法是比较费时的). 计算 n=pq; 计算 选定一个 e, 使 最后得到公钥 : n,e 最后得到私钥 : d ( ). Java 中公钥密码体制 API. KeyPairGenerator

Download Presentation

Java 中公钥密码体制 API

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java中公钥密码体制API • RSA中的密钥生成 通过学习,我们已经知道要生成RSA的密钥,需要做如下工作: • 确定两个大素数p,q(至少512bit) • 此处涉及到素数的判定(该算法是比较费时的). • 计算n=pq; • 计算 • 选定一个e,使 • 最后得到公钥: n,e • 最后得到私钥: d ( )

  2. Java中公钥密码体制API • 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()); }

  3. Java中公钥密码体制API • Initializing the Key Pair Generator SecureRandom random = SecureRandom.getInstance(“SHA1PRNG”, “SUN”); random.setSeed(userSeed); //这两条语句可省略,省略时系统采用 keyGen.initialize(1024, random); //默认值

  4. Java中公钥密码体制API • The KeyPair Class KeyPair类是用于封装密钥对的类. KeyPair pair = keyGen.generateKeyPair();

  5. Java中公钥密码体制API • The PublicKey Class 封装了公钥体制中的公钥(n,e)

  6. Java中公钥密码体制API • The PrivateKey Class 封装了公钥体制中的私钥(n,d,p,q)

  7. Java中公钥密码体制API • KeyFactory 用于实现密钥对象与密钥的标准表达方式之间的转换.

  8. 用于测试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());

  9. 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

  10. Java中公钥密码体制API • RSA用于加密和解密 RSA算法用于加密和解密时,用法同对称加密体制. 具体请参照Cipher类

  11. 用于测试RSA加密及解密方法的代码 Cipher rsaCipher; // Create the cipher rsaCipher = Cipher.getInstance("RSA"); // Initialize the cipher for encryption rsaCipher.init(Cipher.ENCRYPT_MODE, pair.getPublic()); // Our cleartext byte[] cleartext = "This is just an example,******************".getBytes(); // Encrypt the cleartext byte[] ciphertext = rsaCipher.doFinal(cleartext); System.out.println("ciphertext"); System.out.println(Tools.toHexString(ciphertext)); // Initialize the cipher for decryption rsaCipher.init(Cipher.DECRYPT_MODE, pair.getPrivate()); // Dncrypt the cleartext byte[] cleartext1 = rsaCipher.doFinal(ciphertext); System.out.println("****************** old clear text is *****************"); System.out.println(new String(cleartext)); System.out.println("************* new decryption cleartext **************"); System.out.println(new String(cleartext1));

  12. Tools.java publicclass Tools { /* Converts a byte array to hex string-将字符串转换成16进制字符 System.out.println(Tools.toHexString(ciphertext));--具体用法 */ publicstatic String toHexString(byte[] block) { StringBuffer buf = new StringBuffer(); int len = block.length; for (int i = 0; i < len; i++) { byte2hex(block[i], buf); if (i < len-1) { buf.append(":"); } } return buf.toString(); }

  13. privatestaticvoid byte2hex(byte b, StringBuffer buf) { char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; int high = ((b & 0xf0) >> 4); int low = (b & 0x0f); buf.append(hexChars[high]); buf.append(hexChars[low]); }

  14. /* * Convnvert a hex string to byte array */ publicstaticvoid hex2byte(String buf,byte[] block){ int len; int i; byte high,low; byte [] ch; len=buf.length(); ch=buf.getBytes(); for(i=0;i<len/2;i++){ high=(byte)(ch[i*2]<<4); low=(byte)(ch[i*2+1]& 0x0f); block[i]=(byte)(high+low); } } }

More Related