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');