Various cryptographic algorithms for Java

Source: Internet
Author: User
Tags base64 md5 encryption rfc asymmetric encryption

Various cryptographic algorithms for Java

Java provides us with a wealth of encryption technology, can be basically divided into one-way encryption and asymmetric encryption

1. One-way encryption algorithm

One-way encryption algorithm is mainly used to verify the data transmission process, whether it has been tampered with.

    • BASE64 strictly speaking, it belongs to the encoding format, not the encryption algorithm

    • MD5 (Message Digest algorithm 5, Information Digest algorithm)

    • SHA (Secure Hash algorithm, security hashing algorithm)

    • HMAC (Hash message authentication code, hash messages authentication Code

2. Symmetric and asymmetric encryption algorithms

Symmetric and asymmetric encryption algorithms mainly use the form of public and private keys to encrypt data.
    • DES (Data Encryption standard, encryption algorithm)

    • PBE (password-based encryption, password-based authentication)

    • RSA (the name of the algorithm is named after the inventor: Ron Rivest, Adishamir and Leonard Adleman)

    • DH (Diffie-hellman algorithm, key-consistent protocol)

    • DSA (digital Signature algorithm, digitally signed)

    • ECC (Elliptic Curves cryptography, Elliptic curve cipher coding)

Specific details about the cryptographic algorithm: 1.1 BASE64Base is one of the most common encoding methods for transmitting bit byte code on the network, and you can view RFC~RFC, which has a detailed specification of MIME. Base encoding can be used to pass longer identity information in an HTTP environment. For example, in the Java Persistence System hibernate, base is used to encode a long unique identifier (typically the-bit uuid) as a string that is used as a parameter in the HTTP form and the HTTP GET URL. In other applications, it is often necessary to encode binary data as appropriate in the form of URLs (including hidden form fields). At this point, the use of base encoding is not readable, that is, the encoded data will not be directly visible to the naked eye. Java implementation code:
//one-way encryption Packagecom.cn.BaseTest;ImportSun.misc.BASEDecoder;ImportSun.misc.BASEEncoder;/*The encryption and decryption of base is bidirectional and can be reverse-solved. Baseencoder and Basedecoder are unofficial JDK implementation classes. Although it can be found and used in the JDK, it is not available in the API. The classes in the JRE, Sun and Com.sun, are not documented, they belong to the Java, Javax Class library, where implementations are mostly related to the underlying platform and are generally deprecated. BASE strictly speaking, belong to the encoding format, but not the encryption algorithm is mainly Baseencoder, basedecoder two classes, we just need to know the corresponding method can be used. Also, the number of bytes generated after base encryption is multiple, if the number of digits is not enough to fill with the = symbol. Base as defined by RFC, base is defined as: the base content transfer code is designed to describe any sequence of bits as a form that is not easy to be recognized directly. (The Base content-transfer-encoding is designed to represent arbitrary sequences of octets in a form that need not being Huma nly readable.) common in mail, HTTP encryption, interception of HTTP information, you will find the login operation of the user name, password fields are encrypted by base. */
Public classBASE {
/*** Base Decryption * *@paramKey *@return * @throwsException*/ Public Static byte[] Decryptbase (String key)throwsException {return(NewBasedecoder ()). Decodebuffer (key); }
/*** Base Encryption * *@paramKey *@return * @throwsException*/ Public StaticString Encryptbase (byte[] key)throwsException {return(NewBaseencoder ()). Encodebuffer (key); }
Public Static voidMain (string[] args) {String str= "123456"; Try{String result=base.encryptbase (Str.getbytes ()); System.out.println ("Encrypted data >>>>:" +result); byteresult[]=base.decryptbase (Result); String Str=NewString (Result); System.out.println ("Decrypt data >>>>:" +str); } Catch(Exception e) {e.printstacktrace (); }}}

1.2 MD5 Encryption

MD5 is the message-digest algorithm (Information-digest algorithm), which is used to ensure complete and consistent information transmission. is one of the widely used hashing algorithms (also translation digest algorithm, hashing algorithm), mainstream programming language has been widely MD5 implemented. Computing data (such as Chinese characters) as another fixed-length value is the fundamental principle of the hashing algorithm. Widely used in encryption and decryption technology, often used for file verification. Check? No matter how large the file is, a unique MD5 value can be generated after MD5. Like now the ISO check, all is MD5 check. How to use it? It is of course the value of MD5 after the ISO has been MD5. General Download Linux-iso friends have seen the download link next to the MD5 string. is used to verify that the files are consistent.

Java implementation code:
1 //one-way encryption2  Packagecom.cn.MD5Test;3 ImportJava.math.BigInteger;4 Importjava.security.MessageDigest;5 /*6 MD5 (Message Digest algorithm, Information digest algorithm)7 usually we do not use the above MD5 encryption directly. Usually the byte array produced by the MD5 is given to base and then encrypted to get the corresponding string8 Digest: Compilation9 */Ten  Public classMD { One  A      Public Static FinalString KEY_MD = "MD5";  -  - Public Staticstring GetResult (String inputstr) { theSYSTEM.OUT.PRINTLN ("Data >>>&gt before encryption;:" +inputstr); -BigInteger biginteger=NULL; -Try { -MessageDigest MD =messagedigest.getinstance (KEY_MD); + byte[] Inputdata =inputstr.getbytes (); - md.update (inputdata); +BigInteger =NewBigInteger (Md.digest ()); A}Catch(Exception e) {e.printstacktrace ();} atSystem.out.println ("MD5 after encryption >>>>:" +biginteger.tostring ());  -returnbiginteger.tostring (); -         } -     } -  - Public Static voidMain (String args[]) { in Try { -String inputstr = "123456";  to GetResult (INPUTSTR); +}Catch(Exception e) { - e.printstacktrace (); the         } *     } $}

Various cryptographic algorithms for Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.