⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 generatekeypair.java

📁 是一个用VB编写得银行ATM机交易的程序
💻 JAVA
字号:
/*
 * GenerateKeyPair.java
 *
 * Created on 2008年2月23日, 下午3:10
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

/**
 *
 * @author human
 */
package com.myapp.struts;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
public class GenerateKeyPair {
    
private String priKey;
private String pubKey;
public String[] run() {
    String[] strKey=new String[2];
    PublicKey pubkey;
    PrivateKey prikey;
try {
java.security.KeyPairGenerator keygen = java.security.KeyPairGenerator
.getInstance("RSA");
SecureRandom secrand = new SecureRandom();
secrand.setSeed("21cn".getBytes()); // 初始化随机产生器
keygen.initialize(1024, secrand);
KeyPair keys = keygen.genKeyPair();

 pubkey = keys.getPublic();
 prikey = keys.getPrivate();

pubKey = bytesToHexStr(pubkey.getEncoded()); 
priKey = bytesToHexStr(prikey.getEncoded());


strKey[0] = pubKey.substring(0,30);
strKey[1] = priKey.substring(0,10);
} catch (java.lang.Exception e) {
    e.printStackTrace();
}
return strKey;

}


/**
* Transform the specified byte into a Hex String form.
*/
public static final String bytesToHexStr(byte[] bcd) {
StringBuffer s = new StringBuffer(bcd.length * 2);

for (int i = 0; i < bcd.length; i++) {
s.append(bcdLookup[(bcd[i] >>> 4) & 0x0f]);
s.append(bcdLookup[bcd[i] & 0x0f]);
}

return s.toString();
}

/**
* Transform the specified Hex String into a byte array.
*/
public static final byte[] hexStrToBytes(String s) {
byte[] bytes;

bytes = new byte[s.length() / 2];

for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) Integer.parseInt(s.substring(2 * i, 2 * i + 2),
16);
}

return bytes;
}

private static final char[] bcdLookup = { '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
GenerateKeyPair n = new GenerateKeyPair();
n.run();
}
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -