实例介绍
vpn代码
直接连接vpn
调用android代码实现。
【核心代码】
package vpn;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
import java.util.Random;
import org.apache.commons.codec.binary.Base64;
public class MutualAuthentication {
//Based on Stamp Textbook, pg 323 Figure 9.12
public static BigInteger getNonce(String OddEven){
BigInteger bi;
BigInteger moduloResult = (OddEven.toLowerCase().equals("odd")) ? BigInteger.ONE : BigInteger.ZERO;
//System.out.println("moduloResult: " moduloResult); //testing
Boolean isCorrectNonce = false;
int bitLength = 64; //based on http://security.stackexchange.com/questions/1952/how-long-should-a-random-nonce-be
Random random = new Random();
bi = BigInteger.probablePrime(bitLength, random);
isCorrectNonce = bi.mod(new BigInteger("2")).equals(moduloResult);
if(moduloResult.equals(BigInteger.ZERO) && (isCorrectNonce == false)){
bi = bi.add(new BigInteger("1"));
}
//System.out.println("bi: " bi); //testing
return bi;
}
public static BigInteger IncrementNonce(BigInteger nonce) {
return nonce.add(new BigInteger("2")); //ensure nonce remains odd or even
}
public static String GetEncryptedMessage(String userIdentity, BigInteger nonce, coordinates computedG, String sharedKey){
aes AES = new aes(sharedKey);
return AES.encrypt(userIdentity computedG.toString() nonce.toString());
}
public static String DecryptChallenge(String challenge, String sharedKey){
aes AES = new aes(sharedKey);
System.out.println(challenge);
String message = AES.decrypt(challenge);
return message;
}
public static String GetChallenge(BigInteger nonce, String message){
return (message);
}
public static boolean muAuth(int type, ObjectOutputStream out, ObjectInputStream in, aes AES) throws ClassNotFoundException, IOException{
if (type == TwoWayVPN.SERVER){
String clientSentence;
boolean auth = false;
System.out.println("Start Mutual Authentication.");
//1) get I'm Alice from Client
clientSentence = (String) in.readObject();
System.out.println("From Client> " clientSentence);
//2) Send Challenge to Client: Encrypt Rb
BigInteger nonce_B = getNonce("Even");
// System.out.println("Nonce_B: " nonce_B); //testing
out.writeObject(nonce_B);
//3) Receive encrypted Rb and verify
String encryptedNonce_B = (String) in.readObject();
// System.out.println("From Client> EncryptedNonce_B: " encryptedNonce_B);
String answer = AES.decrypt(encryptedNonce_B);
if (!answer.equals(nonce_B.toString())) {
System.out.println("Client failed authentication");
return auth;
}
//4) Get Ra from Client, Return Encrypt Ra
String nonce_A = in.readObject().toString();
// System.out.println("From Client> Nonce_A: " nonce_A);
String encryptedNonce_A = AES.encrypt(nonce_A);
out.writeObject(encryptedNonce_A);
// System.out.println("To Client> EncryptedNonce_A: " encryptedNonce_A);
auth = true;
System.out.println("Authentication Success");
return auth;
}
else {
boolean auth = false;
System.out.println("Start Mutual Authentication.");
// 1) I'm Alice
out.writeObject("I'm Alice");
// 2) Get Rb from Server, Return Encrypt Rb
String nonce_B = in.readObject().toString();
//System.out.println("From Server> Nonce_B: " nonce_B);
String encryptedNounce_B = AES.encrypt(nonce_B);
out.writeObject(encryptedNounce_B);
//System.out.println("To Server> EncryptedNounce_B: " encryptedNounce_B);
// 3) Send Challenge to Server: Encrypt Ra
BigInteger nonce_A = getNonce("Odd");
//System.out.println("Nonce_A: " nonce_A);
out.writeObject(nonce_A);
//4) Receive encrypted Ra and verify
String encryptedNounce_A = (String) in.readObject();
//System.out.println("From Server> EncryptedNounce_A: " encryptedNounce_A);
String answer = AES.decrypt(encryptedNounce_A);
if (!answer.equals(nonce_A.toString())) {
System.out.println("Server failed authentication");
return auth;
}
auth = true;
System.out.println("Authentication Success");
return auth;
}
}
}
标签: VPN
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


支持(0) 盖楼(回复)