Java and JavaScript Cryptojs for HmacSHA1 encryption

Source: Internet
Author: User
Tags sha1

Importjava.io.IOException;Importjava.io.UnsupportedEncodingException;Importjava.security.InvalidKeyException;ImportJava.security.Key;Importjava.security.NoSuchAlgorithmException;ImportJavax.crypto.Mac;ImportJavax.crypto.spec.SecretKeySpec;/*** * HMACSHA1 Encryption class **/ Public classSHA1 { Public Staticstring getHmacSHA1 (String password,string loginname, string algorithm) {byte[] Keybytes =password.getbytes (); Key Key=NewSecretkeyspec (keybytes, 0, keybytes.length, algorithm); Mac Mac=NULL; Try{mac=mac.getinstance (algorithm);        Mac.init (key); } Catch(nosuchalgorithmexception e) {e.printstacktrace (); }Catch(InvalidKeyException e) {e.printstacktrace (); }        returnBytearraytohex (Mac.dofinal (Loginname.getbytes ())); }            /*** 16 Binary encryption *@paramA *@return     */    protected StaticString Bytearraytohex (byte[] a) {intHN, LN, CX; String Hexdigitchars= "0123456789abcdef"; StringBuffer buf=NewStringBuffer (A.length * 2);  for(cx = 0; cx < a.length; cx++) {HN= ((int) (A[cx]) & 0X00FF)/16 ; ln= ((int) (A[cx]) & 0x000f);            Buf.append (Hexdigitchars.charat (HN));        Buf.append (Hexdigitchars.charat (LN)); }        returnbuf.tostring (); }         Public Static voidMain (string[] args)throwsnosuchalgorithmexception, Unsupportedencodingexception, IOException {String Loginkey= getHmacSHA1 ("password", "username", "HmacSHA1"); System.out.println (Loginkey);//53b3a8413cf49e939d8711a0ba34916b2ec2db75String loginkey2= getHmacSHA1 ("123456", "admin", "HmacSHA1"); System.out.println (LoginKey2);//3c39afa93e0b12c28f1f08b18488ebd4ad2e5858            }}

Html+javascript Code:

<HTML>    <Head>        <Scriptsrc= "./hmac-sha1.js"></Script>        <Scripttype= "Text/javascript">            functionGenkey () {varUserName=document.getElementById ("UserName"). Value; varPassword=document.getElementById ("Password"). Value; varHash=cryptojs.hmacsha1 (userName, password); document.getElementById ("Key"). Value=Hash;        }; </Script>    </Head>    <Body>User name:<inputID= "UserName"value=""type= "text">Password:<inputID= "Password"value=""type= "text"><BR>40-bit character key:<inputID= "Key"value=""type= "text"style= "width:400px"><BR>        <inputID= "Genkey"value= "Generate Key"type= "button"onclick= "Genkey ()">    </Body></HTML>
Java and JS, HTML source code download: Link: http://pan.baidu.com/s/1c0pTIes Password: j77s

Java and JavaScript Cryptojs for HmacSHA1 encryption

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.