简单粗暴的前端加解密方法

这里的加密的key值是写死的。也是该加密方法的局限处。


加密方法:
function Encrypt(word){
    var key = CryptoJS.enc.Utf8.parse("HubbleStarMetaGT");
    var iv  = CryptoJS.enc.Utf8.parse("0102030405060708");
    var srcs = CryptoJS.enc.Utf8.parse(word);
    var encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode:CryptoJS.mode.CBC});
    var encryptedStr = encrypted.ciphertext.toString();
    console.log(encryptedStr)
    var encryptedHexStr = CryptoJS.enc.Hex.parse(encryptedStr);
    var encryptedBase64Str = CryptoJS.enc.Base64.stringify(encryptedHexStr);
    return encryptedBase64Str;
}
解密方法:
function Decrypt(word){
    var key = CryptoJS.enc.Utf8.parse("HubbleStarMetaGT");
    var iv  = CryptoJS.enc.Utf8.parse('0102030405060708');
    var decrypted = CryptoJS.AES.decrypt(word, key, { iv: iv,mode:CryptoJS.mode.CBC});
    return CryptoJS.enc.Utf8.stringify(decrypted).toString();
}
var encryptedStr = Encrypt("zhengbiao")
console.log(encryptedStr + "     MessageMessageMessageMessage")
console.log(Decrypt(encryptedStr) + "    MessageMessage")
HTML片段代码:

<script src="js/cryptojslib/aes-min.js">script>
<script src="js/cryptojslib/cipher-core-min.js">script>
<script src="js/cryptojslib/core-min.js">script>
<script src="js/cryptojslib/enc-base64-min.js">script>
<script src="js/cryptojslib/core-min.js">script>
<script src="js/cryptojslib/cipher-core-min.js">script>
<script src="js/cryptojslib/aes-min.js">script>
<script src="js/cryptojslib/enc-base64-min.js">script>
<script src="js/cryptojslib/mode-cfb-min.js">script>
<script src="js/cryptojslib/mode-ctr-min.js">script>
<script src="js/cryptojslib/mode-ecb-min.js">script>
<script src="js/cryptojslib/tripledes-min.js">script>
<script src="js/main/main.js">script>
依赖文件地址: https://github.com/Garengogogo/passwordFiles

你可能感兴趣的:(前端)