這幾天一直做安全登錄
具體實現思路如下
這其中有一個關鍵是解決服務端的公鑰
此文即為實現此步而作
加密算法為RSA
/**
*
*/
package com
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import javax
/**
* RSA 工具類
* 需要到下載bcprov
*
*/
public class RSAUtil {
/**
* * 生成密鑰對 *
*
* @return KeyPair *
* @throws EncryptException
*/
public static KeyPair generateKeyPair() throws Exception {
try {
KeyPairGenerator keyPairGen = KeyPairGenerator
new org
final int KEY_SIZE =
keyPairGen
KeyPair keyPair = keyPairGen
saveKeyPair(keyPair);
return keyPair;
} catch (Exception e) {
throw new Exception(e
}
}
public static KeyPair getKeyPair()throws Exception{
FileInputStream fis = new FileInputStream(
ObjectInputStream oos = new ObjectInputStream(fis);
KeyPair kp= (KeyPair) oos
oos
fis
return kp;
}
public static void saveKeyPair(KeyPair kp)throws Exception{
FileOutputStream fos = new FileOutputStream(
ObjectOutputStream oos = new ObjectOutputStream(fos);
//生成密鑰
oos
oos
fos
}
/**
* * 生成公鑰 *
*
* @param modulus *
* @param publicExponent *
* @return RSAPublicKey *
* @throws Exception
*/
public static RSAPublicKey generateRSAPublicKey(byte[] modulus
byte[] publicExponent) throws Exception {
KeyFactory keyFac = null;
try {
keyFac = KeyFactory
new org
} catch (NoSuchAlgorithmException ex) {
throw new Exception(ex
}
RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(
modulus)
try {
return (RSAPublicKey) keyFac
} catch (InvalidKeySpecException ex) {
throw new Exception(ex
}
}
/**
* * 生成私鑰 *
*
* @param modulus *
* @param privateExponent *
* @return RSAPrivateKey *
* @throws Exception
*/
public static RSAPrivateKey generateRSAPrivateKey(byte[] modulus
byte[] privateExponent) throws Exception {
KeyFactory keyFac = null;
try {
keyFac = KeyFactory
new org
} catch (NoSuchAlgorithmException ex) {
throw new Exception(ex
}
RSAPrivateKeySpec priKeySpec = new RSAPrivateKeySpec(new BigInteger(
modulus)
try {
return (RSAPrivateKey) keyFac
} catch (InvalidKeySpecException ex) {
throw new Exception(ex
}
}
/**
* * 加密 *
*
* @param key
* 加密的密鑰 *
* @param data
* 待加密的明文數據 *
* @return 加密後的數據 *
* @throws Exception
*/
public static byte[] encrypt(PublicKey pk
try {
Cipher cipher = Cipher
new org
cipher
int blockSize = cipher
// 加密塊大小為
// byte
// byte第二個為
int outputSize = cipher
int leavedSize = data
int blocksSize = leavedSize !=
: data
byte[] raw = new byte[outputSize * blocksSize];
int i =
while (data
if (data
cipher
* outputSize);
else
cipher
* blockSize
// 這裡面doUpdate方法不可用
// ByteArrayOutputStream中
// OutputSize所以只好用dofinal方法
i++;
}
return raw;
} catch (Exception e) {
throw new Exception(e
}
}
/**
* * 解密 *
*
* @param key
* 解密的密鑰 *
* @param raw
* 已經加密的數據 *
* @return 解密後的明文 *
* @throws Exception
*/
public static byte[] decrypt(PrivateKey pk
try {
Cipher cipher = Cipher
new org
cipher
int blockSize = cipher
ByteArrayOutputStream bout = new ByteArrayOutputStream(
int j =
while (raw
bout
j++;
}
return bout
} catch (Exception e) {
throw new Exception(e
}
}
/**
* * *
*
* @param args *
* @throws Exception
*/
public static void main(String[] args) throws Exception {
RSAPublicKey rsap = (RSAPublicKey) RSAUtil
String test =
byte[] en_test = encrypt(getKeyPair()
byte[] de_test = decrypt(getKeyPair()
System
}
}
/**
*
*/
package com
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import javax
/**
* RSA 工具類
* 需要到下載bcprov
*
*/
public class RSAUtil {
/**
* * 生成密鑰對 *
*
* @return KeyPair *
* @throws EncryptException
*/
public static KeyPair generateKeyPair() throws Exception {
try {
KeyPairGenerator keyPairGen = KeyPairGenerator
new org
final int KEY_SIZE =
keyPairGen
KeyPair keyPair = keyPairGen
saveKeyPair(keyPair);
return keyPair;
} catch (Exception e) {
throw new Exception(e
}
}
public static KeyPair getKeyPair()throws Exception{
FileInputStream fis = new FileInputStream(
ObjectInputStream oos = new ObjectInputStream(fis);
KeyPair kp= (KeyPair) oos
oos
fis
return kp;
}
public static void saveKeyPair(KeyPair kp)throws Exception{
FileOutputStream fos = new FileOutputStream(
ObjectOutputStream oos = new ObjectOutputStream(fos);
//生成密鑰
oos
oos
fos
}
/**
* * 生成公鑰 *
*
* @param modulus *
* @param publicExponent *
* @return RSAPublicKey *
* @throws Exception
*/
public static RSAPublicKey generateRSAPublicKey(byte[] modulus
byte[] publicExponent) throws Exception {
KeyFactory keyFac = null;
try {
keyFac = KeyFactory
new org
} catch (NoSuchAlgorithmException ex) {
throw new Exception(ex
}
RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(
modulus)
try {
return (RSAPublicKey) keyFac
} catch (InvalidKeySpecException ex) {
throw new Exception(ex
}
}
/**
* * 生成私鑰 *
*
* @param modulus *
* @param privateExponent *
* @return RSAPrivateKey *
* @throws Exception
*/
public static RSAPrivateKey generateRSAPrivateKey(byte[] modulus
byte[] privateExponent) throws Exception {
KeyFactory keyFac = null;
try {
keyFac = KeyFactory
new org
} catch (NoSuchAlgorithmException ex) {
throw new Exception(ex
}
RSAPrivateKeySpec priKeySpec = new RSAPrivateKeySpec(new BigInteger(
modulus)
try {
return (RSAPrivateKey) keyFac
} catch (InvalidKeySpecException ex) {
throw new Exception(ex
}
}
/**
* * 加密 *
*
* @param key
* 加密的密鑰 *
* @param data
* 待加密的明文數據 *
* @return 加密後的數據 *
* @throws Exception
*/
public static byte[] encrypt(PublicKey pk
try {
Cipher cipher = Cipher
new org
cipher
int blockSize = cipher
// 加密塊大小為
// byte
// byte第二個為
int outputSize = cipher
int leavedSize = data
int blocksSize = leavedSize !=
: data
byte[] raw = new byte[outputSize * blocksSize];
int i =
while (data
if (data
cipher
* outputSize);
else
cipher
* blockSize
// 這裡面doUpdate方法不可用
// ByteArrayOutputStream中
// OutputSize所以只好用dofinal方法
i++;
}
return raw;
} catch (Exception e) {
throw new Exception(e
}
}
/**
* * 解密 *
*
* @param key
* 解密的密鑰 *
* @param raw
* 已經加密的數據 *
* @return 解密後的明文 *
* @throws Exception
*/
public static byte[] decrypt(PrivateKey pk
try {
Cipher cipher = Cipher
new org
cipher
int blockSize = cipher
ByteArrayOutputStream bout = new ByteArrayOutputStream(
int j =
while (raw
bout
j++;
}
return bout
} catch (Exception e) {
throw new Exception(e
}
}
/**
* * *
*
* @param args *
* @throws Exception
*/
public static void main(String[] args) throws Exception {
RSAPublicKey rsap = (RSAPublicKey) RSAUtil
String test =
byte[] en_test = encrypt(getKeyPair()
byte[] de_test = decrypt(getKeyPair()
<A title=system target=_blank>system</A>
}
}
IndexAction
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass
*/
package com
import java
import java
import javax
import javax
import org
import org
import org
import org
import com
/**
* MyEclipse Struts
* Creation date:
*
* XDoclet definition:
* @struts
*/
public class IndexAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping
HttpServletRequest request
RSAPublicKey rsap = (RSAPublicKey) RSAUtil
String module = rsap
String empoent = rsap
System
System
System
System
request
request
return mapping
}
}
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass
*/
package com
import java
import java
import javax
import javax
import org
import org
import org
import org
import com
/**
* MyEclipse Struts
* Creation date:
*
* XDoclet definition:
* @struts
*/
public class IndexAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping
HttpServletRequest request
RSAPublicKey rsap = (RSAPublicKey) RSAUtil
String module = rsap
String empoent = rsap
<A title=system target=_blank>system</A>
<A title=system target=_blank>system</A>
<A title=system target=_blank>system</A>
<A title=system target=_blank>system</A>
request
request
return mapping
}
}
通過此action進入登錄頁面
<%@ page language=
<%@ taglib uri=
<%@ taglib uri=
<%@ taglib uri=
<%@ taglib uri=
<!DOCTYPE HTML PUBLIC
<html:html lang=
<head>
<html:base />
<title>login</title>
<meta http
<meta http
<meta http
<meta http
<meta http
<!
<link rel=
<script type=
<script type=
<script type=
<script type=
function rsalogin()
{
bodyRSA();
var result = encryptedString(key
//alert(result);
loginForm
loginForm
}
var key ;
function bodyRSA()
{
setMaxDigits(
key = new RSAKeyPair(
de
}
</script>
</head>
<body >
<html:form action=
<table border=
<tr>
<td>Login:</td>
<td><html:text property=
</tr>
<tr>
<td>Password:</td>
<td><html:password property=
</tr>
<tr>
<td colspan=
</tr>
</table>
</html:form>
</body>
</html:html>
<%@ page language=
<%@ taglib uri=
<%@ taglib uri=
<%@ taglib uri=
<%@ taglib uri=
<!DOCTYPE HTML PUBLIC
<html:html lang=
<head>
<html:base />
<title>login</title>
<meta http
<meta http
<meta http
<meta http
<meta http
<!
<link rel=
<script type=
<script type=
<script type=
<script type=
function rsalogin()
{
bodyRSA();
var result = encryptedString(key
//alert(result);
loginForm
loginForm
}
var key ;
function bodyRSA()
{
setMaxDigits(
key = new RSAKeyPair(
}
</script>
</head>
<body >
<html:form action=
<table border=
<tr>
<td>Login:</td>
<td><html:text property=
</tr>
<tr>
<td>Password:</td>
<td><html:password property=
</tr>
<tr>
<td colspan=
</tr>
</table>
</html:form>
</body>
</html:html>
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass
*/
package com
import java
import javax
import javax
import org
import org
import org
import org
import com
/**
* MyEclipse Struts
* Creation date:
*
* XDoclet definition:
* @struts
* @struts
* @struts
*/
public class LoginAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping
HttpServletRequest request
//LoginForm loginForm = (LoginForm) form;
String result = request
System
System
byte[] en_result = new BigInteger(result
System
byte[] de_result = RSAUtil
System
System
StringBuffer sb = new StringBuffer();
sb
System
return mapping
}
}
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass
*/
package com
import java
import javax
import javax
import org
import org
import org
import org
import com
/**
* MyEclipse Struts
* Creation date:
*
* XDoclet definition:
* @struts
* @struts
* @struts
*/
public class LoginAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping
HttpServletRequest request
//LoginForm loginForm = (LoginForm) form;
String result = request
<A title=system target=_blank>system</A>
<A title=system target=_blank>system</A>
byte[] en_result = new BigInteger(result
<A title=system target=_blank>system</A>
byte[] de_result = RSAUtil
<A title=system target=_blank>system</A>
<A title=system target=_blank>system</A>
StringBuffer sb = new StringBuffer();
sb
<A title=system target=_blank>system</A>
return mapping
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/27165.html