Using AES, java encryption and decryption js

com.zoki.security Package;
 
Import java.security.Key;


Import javax.crypto.Cipher;
Import javax.crypto.spec.IvParameterSpec;
Import javax.crypto.spec.SecretKeySpec;
 
/ **
 * the AES encryption and decryption
 * @author zhoukai
 * /
public class the AES {
 
 
public static String the encrypt (Content String, String Key) throws Exception {
        the try {


        Key = keySpec new new SecretKeySpec (key.getBytes ( "UTF-. 8"), "the AES"); // two parameters, The first private key is a byte array, the second encryption is AES or the DES


            Key String = IV; // initialization vector parameter, AES is 16bytes DES is 8bytes..


            IvParameterSpec ivSpec = new new IvParameterSpec (iv.getBytes ( "UTF -8 "));


            the cipher cipher = Cipher.getInstance (" the AES / the CBC / PKCS5Padding ");
            cipher.init(Cipher.ENCRYPT_MODE, keySpec,ivSpec);


            byte[] byteResult = cipher.doFinal(content.getBytes("UTF-8"));
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < byteResult.length; i++) {
                String hex = Integer.toHexString(byteResult[i] & 0xFF);
                if (hex.length() == 1) {
                    hex = '0' + hex;
                }
                sb.append(hex.toUpperCase());
            }
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}

 

需要 <script type="text/javascript" src="aes.js"></script>

var encryptedBase64Str = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Hex.parse(data));
        alert(encryptedBase64Str);
        var decrypted1 = CryptoJS.AES.decrypt(encryptedBase64Str, CryptoJS.enc.Utf8.parse("1234567890123456"),
                {
                    iv: CryptoJS.enc.Utf8.parse("1234567890123456"),
                    mode:CryptoJS.mode.CBC,
                    padding:CryptoJS.pad.Pkcs7
                });
                alert(decrypted1);
        $("#tf").val(CryptoJS.enc.Utf8.stringify(decrypted1).toString());

 

Guess you like

Origin blog.csdn.net/luyinxing1/article/details/77089676