Java implementation of SSH mode encryption principle of the realization of the idea to share.
First, the principle of SSH encryption
SSH is the first through asymmetric encryption to tell the service end of a symmetric encryption password, and then authenticate the user name and password, using both sides already know encryption password for encryption and decryption, see the following figure:
Explanation: Why does SSH use asymmetric encryption, and symmetric encryption, what is the use? Is it safe or not? Since the use of symmetric encryption later, why do you want to use asymmetric encryption at the beginning? Conversely, why use symmetric encryption if you use asymmetric encryption?
Asymmetric encryption, is to the client generated 256-bit random password to the server, then in the process of transmission, the use of public key encryption, so that this 256-bit encryption password is difficult to be cracked on the network.
Symmetric encryption, because the frequent use of asymmetric encryption is very wasteful performance, then SSH is using the 256-bit length of the password as the next password to pass passwords encrypted password, the difficulty of cracking, presumably everyone knows, each has 0-9 kinds of changes.
This is safe, I think it is very good, specific use is easy to understand.
second, my SSH encryption principle
①, use the scene
The project I'm developing is the bulk of futures trading, mainly for the exchange, which creates a demand that we need to control the cycle in which the Exchange uses our software. Which means our project has a back door, to control the cycle of the project, if the Exchange uses the software cycle to, then if he does not renew, and the project's code is deployed on the other people's servers, at this time we will be difficult to control, but with this backdoor, will automatically stop the software after expiration, This is not to worry that the exchange will not give us money.
②, use Way
Our project code for the transaction contains a backdoor that sends a request to the Web service via WebService client.
Once the Web service receives the request, it returns the information that the client needs.
In the process above, there will be a request for SSH encryption, please allow me to use a poor graph to represent.
Third, my SSH specific implementation
Now that you want to use WebService, you need to set up a Web service and a Web service client. In this regard, I do not want to say too much, there are many ways, I do not mislead people here. I've done it through eclipse, and I can refer to WebService communication.
Next, I will introduce the code, but given the space problem, some unnecessary code I do not post it, the key is to explain the principle of clarity.
①, Service
Exchangeservice.java
Public byte[] Request (string param, String resulttype) {logger.info ("Request parameter:" + param);
Returns the object Keyresult Keyresult = new Keyresult (); try {//First get public key if (Resulttype.equals (Public_key_result_type)) {map<string, object> keymap = Rsacoder
. Initkey ();
Generate public and private key Privatekey = Rsacoder.getprivatekey (keymap);
Keyresult.setkey (Rsacoder.getpublickey (keymap));
Logger.info ("Public key string:" + Keyresult.getkey ());
Logger.info ("Private key string:" + Privatekey); else if (resulttype.equals (Echostr_result_type)) {//Set password information for client byte[] Parambyte = new Base64decoder (). Dec
Odebuffer (param);
Echostr = new String (Rsacoder.decryptbyprivatekey (Parambyte, Privatekey));
else {//the exchange's corresponding permission information is obtained through the database.
The request is converted to a byte array, then decrypted, and finally converted to a string Exchangeinfo info = ExchangeInfo.dao.getInfoByName (The new string (Cryptutil.decrypt
New Base64decoder (). Decodebuffer (param), echostr.getbytes ()));
String result = ""; //Get System Enable permission if (Resulttype.equals (Privilege_result_type)) {//First judge the use of permissions//In the judgment of Use date//current login with Login
Gets the current date and start date of the login and then calculates the date that can be used long = (new Date (). GetTime ()/1000)-String2dateint (Openday);
Convert to days int day = (int) (Time/(60 * 60 * 24));
You can also use the number of days if (Usedays-day > 0) {//can use result = ' 1 ';
else {//cannot use result = ' 0 ';
} keyresult.setresult (Cryptutil.encrypt (Result.getbytes (), Echostr.getbytes ()));
Return Jsonutil.objecttobyte (Keyresult); The catch (Exception e) {logger.error ("WebService error!!!!
");
Logger.error (E.getmessage (), E);
return null;
}
Let me repeat:
The first decision statement is to generate the public and private keys and return the public key.
The second judgment statement is to save the random string sent by the client, which is critical, and the random string is first encrypted through the public key, which greatly strengthens the depth of the encryption.
The third judgment statement is to encrypt the permissions of the client through a random string.
②, client
Exchangeutil.java
public static Boolean Canrunforexchange (String resulttype) {int i = 1;
Boolean result = false;
while (true) {try {//WebService Invoke class Exchangeserviceproxy proxy = new Exchangeserviceproxy ();
Base64encoder encoder = new Base64encoder ();
Step1. Gets the public key generated by the service Keyresult Keyresult = jsonutil.bytetoobject (Proxy.request (null, Public_key_result_type),
Keyresult.class);
Step2. Produces random strings, sent to Webserivce string echostr = Strutil.getechostrbylength (10);
byte[] Echobyteparam = Rsacoder.encryptbypublickey (Echostr.getbytes (), Keyresult.getkey ());
Proxy.request (Encoder.encode (Echobyteparam), echostr_result_type); Step3. Encrypt client request information and then send to WebService//encrypt as Byte array before converting to string byte[] results = proxy.request (encoder.en
Code (Cryptutil.encrypt (Constants.client_type.getBytes (), Echostr.getbytes ()), resulttype);
Keyresult = Jsonutil.bytetoobject (results, keyresult.class); Step4. ThroughThe Password decryption service side returns a message String response = new String (Cryptutil.decrypt (Keyresult.getresult (), Echostr.getbytes ()));
if (Response.equals ("1")) {result = true;
} break;
catch (Exception e) {logger.debug ("first" + i + "load WebService failed");
i++;
Logger.error (E.getmessage (), E);
if (i >=) {break;
}} return result;
}
To give a little explanation:
Through the loop mainly in order to prevent the network disconnect when the service non-stop to send requests, up to 10 times is enough.
There are four main steps, I would like to explain in the comments can also.
③, shared encryption and decryption public classes
Cryptutil.java
Package com.honzh.socket.util;
Import Javax.crypto.Cipher;
Import Javax.crypto.SecretKey;
Import Javax.crypto.SecretKeyFactory;
Import Javax.crypto.spec.DESKeySpec;
Import Javax.crypto.spec.IvParameterSpec; public class Cryptutil {/** * @Title: Encrypt * @Description: Encrypted * @param data * @param key * @return *
Throws Exception */public static byte[] Encrypt (byte[] data, byte[] key) throws Exception {key = Get8 (key);
Cipher Cipher = cipher.getinstance ("des/cbc/pkcs5padding");
Deskeyspec Deskeyspec = new Deskeyspec (key);
Secretkeyfactory keyfactory = secretkeyfactory.getinstance ("DES");
Secretkey Secretkey = Keyfactory.generatesecret (Deskeyspec);
Ivparameterspec IV = new IVPARAMETERSPEC (key);
Cipher.init (Cipher.encrypt_mode, Secretkey, iv);
return cipher.dofinal (data);
/** * @Title: Decrypt * @Description: Decryption * @param data * @param key * @return * @throws Exception * * public static byte[] Decrypt (byte[] DaTA, byte[] key) throws Exception {key = Get8 (key);
Cipher Cipher = cipher.getinstance ("des/cbc/pkcs5padding");
Deskeyspec Deskeyspec = new Deskeyspec (key);
Secretkeyfactory keyfactory = secretkeyfactory.getinstance ("DES");
Secretkey Secretkey = Keyfactory.generatesecret (Deskeyspec);
Ivparameterspec IV = new IVPARAMETERSPEC (key);
Cipher.init (Cipher.decrypt_mode, Secretkey, iv);
return cipher.dofinal (data);
private static byte[] Get8 (byte[] key) {byte[] Key1 = new Byte[8];
for (int i = 0; i < 8; i++) {key1[i] = Key[i];
return key1;
public static string tohexstring (byte[] Data {String s = "";
for (int i = 0; i < Data.length i++) {s + + integer.tohexstring (Data[i] & 0xFF) + "-";
return s;
}
}
Generally, SHA and MD5 two kinds of encryption is enough for us to use!
As for other auxiliary classes I do not introduce more, the internet has a lot of resources, I hope you can combine learning.