Now we're going to review the algorithm again, so let's look at the encryption algorithm.
abbreviation | Full
name |
Properties |
Description |
Notes |
Des (Data Encryption Standard) |
Data encryption algorithm |
Key, symmetric encryption algorithm |
Simple encryption algorithm |
- |
Rsa |
A public key algorithm to support variable-length keys |
Public-private key, asymmetric encryption algorithm |
Public key encryption, private key decryption |
- |
Aes (Advanced encryption) |
Advanced Encryption Standard |
Key, symmetric encryption algorithm |
Next Generation encryption algorithm standard |
- |
MD5 (Message Digest algorithm 5) |
Message digest algorithm |
Abstract algorithm |
Used to verify the correctness of the information and not be modified |
- |
SHA (Secure Hash algorithm) |
Secure Hash Algorithm |
Digital signatures |
Used to verify the correctness of the information and not be modified |
With a lot of extensions |
About the Java implementation: Key key
Basically, Des,rsa,aes and so on encryption algorithm, in order to ensure the use of the scene under the encryption method is different, need to use the key.
A key can be seen as a method by which the "plaintext" of the method becomes "ciphertext" for secure transmission. The receiving party then uses the method to convert the ciphertext to clear text. The process of secure encryption to do this.
For Java, the key is the key (Java.security.Key interface), and when created, it is generated using Keygenerator or a Startup key generator. The following AES key generation:
public static Key makeKey(String key) throws Exception { KeyGenerator keyGen = KeyGenerator.getInstance("AES"); // AES生成器 keyGen.init(128, new SecureRandom(key.getBytes())); // AES生成器初始化 SecretKey secretKey = keyGen.generateKey(); // 生成密钥 return new SecretKeySpec(secretKey.getEncoded(),"AES"); // 转为AES密钥}
However, for RSA, a pair of public key keys is used, so the KeyPair (Java.security.KeyPair interface) is used in particular, using a pair of keys:
public static KeyPair makeKey(int keyLength) throws Exception{ KeyPairGenerator keyPairGenerator=KeyPairGenerator.getInstance("RSA"); // RSA生成器 keyPairGenerator.initialize(keyLength); // 根据长度生成,每次生成不一致 return keyPairGenerator.generateKeyPair(); // 生成密钥}
Encrypt and decrypt cipher
In Java, encryption and decryption using Cipher (javax.crypto.Cipher), whether Des,rsa,aes, is just the type of Cipher. In short, use the type (DES,RSA,AES) and key, determine the mode, you can encrypt the decryption, for example, AES:
/** * 加密 */public static byte[] encrypt(String msg,Key key) throws Exception { Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, key); return cipher.doFinal(msg.getBytes());}/** * 解密 */public static byte[] decrypt(byte[] content,Key key) throws Exception { Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, key); return cipher.doFinal(content);}
Information Digest MessageDigest
Abstract or signature, simply put, is to convert certain brief information into easy-to-judge genetic code. Transmission encoding at the same time, used to determine the correctness of the information, the receiving party will be the summary processing, if the results and transmitted information inconsistent, the judgment information is incorrect, not processed.
Digest processing in Java uses messagedigest:
MessageDigest digest = MessageDigest.getInstance("md5");byte[] result = digest.digest(msg.getBytes());
Note that the results of the straight down output, which need to be processed by adding salt or conversion, can be turned into a visible result.
Algorithm (i) _ Encryption algorithm