码迷,mamicode.com
首页 > Web开发 > 详细

CryptoJS文件加密与解密

时间:2018-09-29 00:03:49      阅读:818      评论:0      收藏:0      [点我收藏+]

标签:xtend   ring   uppercase   def   const   ted   fun   ram   UNC   

import React, { Component } from ‘react‘;
import CryptoJS from ‘crypto-js/crypto-js‘
import { Button } from ‘antd‘;

class Encryption extends Component {
constructor(){
super();
this.state={
value2:‘qqqqqqqqqqqq‘,
encryptioned:‘‘,
Decrypted:‘‘
}
}

componentDidMount(){

}

///对文件进行加密
encryption = (e,dataw)=>{
debugger
let data = this.state.value2;
let srcs = CryptoJS.enc.Utf8.parse(data);
let key = CryptoJS.enc.Utf8.parse(‘1111111111111111‘);//Latin1 w8m31+Yy/Nw6thPsMpO5fg==
let encrypted = CryptoJS.AES.encrypt(srcs, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
this.setState({encryptioned: encrypted.toString()});
};
///对文件进行解密
Decrypt=(e,data)=>{
let word = this.state.encryptioned;///o7H8uIM2O5qv65l2
let key = CryptoJS.enc.Utf8.parse(‘1111111111111111‘);//Latin1 w8m31+Yy/Nw6thPsMpO5fg==
let decrypt = CryptoJS.AES.decrypt(word, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
this.setState({Decrypted:CryptoJS.enc.Utf8.stringify(decrypt).toString()});
};

render() {
return(
<div>
<p><input value={this.state.value2} type="text"/></p>
<Button type="primary" onClick={this.encryption.bind(this)}>请输入要加密的内容</Button>
<br/>
<h2>
加密之后的内容:<span>{this.state.encryptioned}</span>
</h2>
<Button type="primary" onClick={this.Decrypt.bind(this)}>对加密文件进行解密</Button>
<br/>
<h2>
解密之后的内容:<span>{this.state.Decrypted}</span>
</h2>
</div>
)
}
}
export default Encryption;
这里的密钥是由十六位十六进制数作为密钥组成
 //十六位十六进制数作为密钥偏移量
const CryptoJS = require(‘crypto-js‘); //引用AES源码js const key = CryptoJS.enc.Utf8.parse("1234123412ABCDEF"); //十六位十六进制数作为密钥 const iv = CryptoJS.enc.Utf8.parse(‘ABCDEF1234123412‘); //十六位十六进制数作为密钥偏移量 //解密方法 function Decrypt(word) { let encryptedHexStr = CryptoJS.enc.Hex.parse(word); let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr); let decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8); return decryptedStr.toString(); } //加密方法 function Encrypt(word) { let srcs = CryptoJS.enc.Utf8.parse(word); let encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); return encrypted.ciphertext.toString().toUpperCase(); } export default { Decrypt , Encrypt }
 
 

CryptoJS文件加密与解密

标签:xtend   ring   uppercase   def   const   ted   fun   ram   UNC   

原文地址:https://www.cnblogs.com/boonook/p/9721470.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!