資源簡介
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; } } }
代碼片段和文件信息
package?vpn;
import?java.io.UnsupportedEncodingException;
import?java.security.InvalidKeyException;
import?java.security.Key;
import?java.security.NoSuchAlgorithmException;
import?javax.crypto.BadPaddingException;
import?javax.crypto.Cipher;
import?javax.crypto.IllegalBlockSizeException;
import?javax.crypto.Mac;
import?javax.crypto.NoSuchPaddingException;
import?javax.crypto.spec.SecretKeySpec;
import?org.apache.commons.codec.binary.base64;
public?class?aes?{
private?static?String?encryptKey;
/*public?static?void?main(String?args[])?{
????aes?t?=?new?aes(“password92343456“);
????String?encrypt?=?t.encrypt(“mypasswor42342dftgyhu“);
????System.out.println(“decrypted?value:“?+?t.decrypt(encrypt));
}*/
public?aes(String?key){
this.encryptKey?=?key;
}
public?String?computemac
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2015-11-07?18:32??VPN-master\
?????文件?????????797??2015-10-22?13:33??VPN-master\.classpath
?????文件?????????467??2015-10-22?13:33??VPN-master\.gitignore
?????文件?????????555??2015-10-22?13:33??VPN-master\.project
?????目錄???????????0??2015-11-07?18:32??VPN-master\.settings\
?????文件?????????736??2015-10-22?13:33??VPN-master\.settings\org.eclipse.jdt.core.prefs
?????文件??????????12??2015-10-22?13:33??VPN-master\README.md
?????目錄???????????0??2015-11-07?18:32??VPN-master\bin\
?????目錄???????????0??2015-11-07?18:32??VPN-master\bin\vpn\
?????文件????????1423??2015-10-22?13:33??VPN-master\bin\vpn\Client.class
?????文件????????1128??2015-10-22?13:33??VPN-master\bin\vpn\Server.class
?????文件????????3052??2015-10-22?13:33??VPN-master\bin\vpn\VPN.class
?????文件?????????832??2015-10-22?13:33??VPN-master\pom.xm
?????目錄???????????0??2015-11-07?18:32??VPN-master\src\
?????目錄???????????0??2015-11-07?18:32??VPN-master\src\vpn\
?????文件????????2256??2015-10-22?13:33??VPN-master\src\vpn\MessageAuthentication.java
?????文件????????4446??2015-10-22?13:33??VPN-master\src\vpn\MutualAuthentication.java
?????文件????????1070??2015-10-22?13:33??VPN-master\src\vpn\TwoWayVPN.java
?????文件????????3601??2015-10-22?13:33??VPN-master\src\vpn\aes.java
?????文件????????1390??2015-10-22?13:33??VPN-master\src\vpn\client.java
?????文件?????????268??2015-10-22?13:33??VPN-master\src\vpn\coordinates.java
?????文件????????5113??2015-10-22?13:33??VPN-master\src\vpn\ecdh.java
?????文件????????1263??2015-10-22?13:33??VPN-master\src\vpn\server.java
- 上一篇:android 天氣預報
- 下一篇:ImageDemo網絡圖片查看
評論
共有 條評論