Java 中公钥密码体制 API
-
Upload
priscilla-vazquez -
Category
Documents
-
view
47 -
download
6
description
Transcript of Java 中公钥密码体制 API
Java 中公钥密码体制 API
RSA中的密钥生成通过学习 , 我们已经知道要生成 RSA 的密钥,需要做如下工作 :
确定两个大素数 p,q( 至少 512bit)此处涉及到素数的判定(该算法是比较费时的).
计算 n=pq; 计算 选定一个 e, 使
最后得到公钥 : n,e 最后得到私钥 : d ( )
gcd( , (n) )=1e
-1(mod ( ))d e n
( ) ( 1)( 1)n p q
Java 中公钥密码体制 API KeyPairGenerator
The KeyPairGenerator class is an engine class used to generate pairs of public and private keys
1. 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());}
Java 中公钥密码体制 API
2. Initializing the Key Pair Generator
SecureRandom random = SecureRandom.getInstance(“SHA1PRNG”, “SUN”); random.setSeed(userSeed); //这两条语句可省略 ,省略时系统采用 keyGen.initialize(1024, random); //默认值
The KeyPair Class
KeyPair类是用于封装密钥对的类.KeyPair pair = keyGen.generateKeyPair();
Java 中公钥密码体制 API
Java 中公钥密码体制 API
The PublicKey Class
封装了公钥体制中的公钥 (n,e)
Java 中公钥密码体制 API
The PrivateKey Class
封装了公钥体制中的私钥 (n,d,p,q)
Java 中公钥密码体制 API
KeyFactory
用于实现密钥对象与密钥的标准表达方式之间的转换.
// 取得 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());
用于测试 RSA密钥对的代码
Java 中公钥密码体制 API Sun RSA public key, 1024 bits modulus( n) : 145334580766753352968271624143227642903180026858070538178093842654655131896488508859021
170467602039725331815700222176862989556902870844823932940664684208523041952356061062674190324733884210594543025749003442008772730874286810845100551163211606567048423518752061867186661617099735966587084128589872797083364747
public exponent( e): 65537
Sun RSA private CRT(中国剩余定理 ) key, 1024 bits modulus(n ): 14533458076675335296827162414322764290318002685807053817809384265465513189648850885
9021170467602039725331815700222176862989556902870844823932940664684208523041952356061062674190324733884210594543025749003442008772730874286810845100551163211606567048423518752061867186661617099735966587084128589872797083364747
public exponent( e ): 65537 private exponent( d ) : 12365092831489693162881818652978058410727398442985115489375977802738781908565089
5913303346874330868563479816159248796247973430327100346926738740536218117730278249237671789995775819474273027581511787552426991635086254315333676872987600640893099168403376116367516517690236495972293965947291133168132306425021585
prime p: 12836193223793558297927305428496631019420276059582307483685094836716895113592759865558860520415443996103674401559926195951777679843538568226126094329161913
prime q: 11322249379773806480799329481584580801659487779713145666093610874564424578061919183694263286905364957495175786438352752223906032465534494985244811289566819
• prime exponent p: 9148115978816938941686233038570458596274819322137278722560991875406808674040267864575090381418803770786186738386857391005381389023790468073571745545755305
• prime exponent q: 6844970668111417772796286576893398781490616984604338685571432266675573587551937073365433353532774844453001508071379135387084866751864476949057550209865067
• crt coefficient: 12215131375839181768650789125852251387959893277517099645583985397510200970338820228475780242328195082955827984916897355413623358654311131059893783646946460
Java 中公钥密码体制 API
RSA用于加密和解密RSA 算法用于加密和解密时 , 用法同对称加密体制 . 具体请参照 Cipher 类
Cipher rsaCipher;// Create the cipherrsaCipher = Cipher.getInstance("RSA");// Initialize the cipher for encryptionrsaCipher.init(Cipher.ENCRYPT_MODE, pair.getPublic());// Our cleartextbyte[] cleartext = "This is just an example,******************".getBytes();// Encrypt the cleartextbyte[] ciphertext = rsaCipher.doFinal(cleartext);System.out.println("ciphertext");System.out.println(Tools.toHexString(ciphertext));// Initialize the cipher for decryptionrsaCipher.init(Cipher.DECRYPT_MODE, pair.getPrivate());// Dncrypt the cleartextbyte[] 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));
用于测试 RSA加密及解密方法的代码
public class Tools { /* Converts a byte array to hex string-将字符串转换成 16进制字符
System.out.println(Tools.toHexString(ciphertext));--具体用法 */
public static 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();
}
Tools.java
private static void 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]);
}
/* * Convnvert a hex string to byte array*/
public static void 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);
}}
}