提交 c8dc506b 编写于 作者: B baiy 提交者: ninecents

加解密 TripleDES 优化

上级 26f3694e
<template> <template>
<div> <heightResize ignore :append="['.page-option-block']" @resize="resize">
<Input v-model="current.input" :rows="7" type="textarea" placeholder="内容"></Input> <autoHeightTextarea v-model="current.input" :height="inputHeight" :placeholder="$t('encrypt_input')"/>
<option-block> <option-block class="page-option-block">
<FormItem> <FormItem>
<Select v-model="current.type" style="width:200px"> <Select v-model="current.type" style="width:200px">
<Option v-for="v in type" :value="v" :key="v">{{ v }}</Option> <Option v-for="v in type" :value="v" :key="v">{{ v }}</Option>
</Select> </Select>
</FormItem> </FormItem>
<FormItem> <FormItem>
<Input v-model="current.password" placeholder="密码/秘钥"></Input> <Input v-model="current.password" :placeholder="$t('encrypt_password')"></Input>
</FormItem> </FormItem>
<FormItem v-if="current.type === 'SM2'"> <FormItem v-if="current.type === 'SM2'">
<Select v-model="current.sm2CipherMode" style="width:100px"> <Select v-model="current.sm2CipherMode" style="width:100px">
...@@ -18,39 +18,49 @@ ...@@ -18,39 +18,49 @@
</FormItem> </FormItem>
<FormItem> <FormItem>
<ButtonGroup> <ButtonGroup>
<Button type="primary" @click="handle('encrypt')">加密</Button> <Button type="primary" @click="handle('encrypt')">{{ $t('encrypt_encrypt') }}</Button>
<Button type="primary" @click="handle('decrypt')">解密</Button> <Button type="primary" @click="handle('decrypt')">{{ $t('encrypt_decrypt') }}</Button>
<Button type="primary" @click="sm2Generate()" v-if="current.type === 'SM2'">生成密钥对</Button> <Button type="primary" @click="sm2Generate()" v-if="current.type === 'SM2'">
{{ $t('encrypt_generate_secret_key') }}
</Button>
</ButtonGroup> </ButtonGroup>
</FormItem> </FormItem>
</option-block> </option-block>
<Input v-model="current.output" :rows="7" type="textarea" placeholder="结果"></Input> <autoHeightTextarea :value="current.output" :height="outputHeight" :placeholder="$t('encrypt_output')"/>
</div> </heightResize>
</template> </template>
<script> <script>
import crypto from "crypto-js" import crypto from "crypto-js"
export default { import heightResize from "./components/heightResize";
created() { import autoHeightTextarea from "./components/autoHeightTextarea";
this.current = Object.assign(this.current,this.$getToolData("input"))
}, export default {
methods: { components: {
handle(v) { heightResize,
const sm2 = require('sm-crypto').sm2 autoHeightTextarea
if (this.current.input) { },
created() {
this.$initToolData('input')
},
methods: {
handle(v) {
const sm2 = require('sm-crypto').sm2
if (this.current.input) {
try {
let output = ""
switch (this.current.type) { switch (this.current.type) {
case "AES": case "AES":
case "DES": case "DES":
case "RC4": case "RC4":
case "Rabbit": case "Rabbit":
case "TripleDes": case "TripleDES":
if (v === "encrypt") { if (v === "encrypt") {
this.current.output = crypto[this.current.type].encrypt( output = crypto[this.current.type].encrypt(
this.current.input, this.current.input,
this.current.password this.current.password
).toString(); ).toString();
} } else {
else{ output = crypto[this.current.type].decrypt(
this.current.output = crypto[this.current.type].decrypt(
this.current.input, this.current.input,
this.current.password this.current.password
).toString(crypto.enc.Utf8); ).toString(crypto.enc.Utf8);
...@@ -58,60 +68,77 @@ ...@@ -58,60 +68,77 @@
break; break;
case "SM2": case "SM2":
if (v === "encrypt") { if (v === "encrypt") {
this.current.output =sm2.doEncrypt( output = sm2.doEncrypt(
this.current.input, this.current.input,
this.current.password, this.current.password,
this.current.sm2CipherMode this.current.sm2CipherMode
); );
} } else {
else{ output = sm2.doDecrypt(
this.current.output =sm2.doDecrypt(
this.current.input, this.current.input,
this.current.password, this.current.password,
this.current.sm2CipherMode this.current.sm2CipherMode
); );
} }
break; break;
default:
return;
} }
this.current.operation = v; if (!output) {
this.$clipboardCopy(this.current.output); throw new Error("output null")
this.$saveToolData(this.current); }
this.current.output = output
} catch (e) {
return this.$Message.error(
this.$t('encrypt_failed', [e.message]).toString()
)
} }
}, this.current.operation = v;
sm2Generate(){ this.$clipboardCopy(this.current.output);
const sm2 = require('sm-crypto').sm2 this.$saveToolData(this.current);
let keypair = sm2.generateKeyPairHex() }
let string = "公钥:\n"+keypair.publicKey+"\n"+"私钥:\n"+keypair.privateKey+"\n\n"+
"请及时保存秘钥对, 关闭对话框后无法恢复当前秘钥数据"
this.$Modal.info({
render: (h) => {
return h('Input', {
props: {
value: string,
type:"textarea",
rows:9
}
})
},
okText:"关闭",
width:600
})
},
}, },
data() { sm2Generate() {
return { const sm2 = require('sm-crypto').sm2
current:{ let keypair = sm2.generateKeyPairHex()
input: "", let string = [
password:"", this.$t('encrypt_public_key'),
sm2CipherMode:"C1C3C2", keypair.publicKey,
output: "", this.$t('encrypt_private_key'),
type:"AES", keypair.privateKey, '',
operation:"" this.$t('encrypt_secret_key_prompt')
].join("\n");
this.$Modal.info({
render: (h) => {
return h('Input', {
props: {
value: string,
type: "textarea",
rows: 9
}
})
}, },
type: ["AES","DES","RC4","Rabbit","TripleDes","SM2"], okText: this.$t('encrypt_close'),
} width: 600
})
}, },
} resize(height) {
</script> this.inputHeight = Math.min(160, Math.ceil(height / 2))
\ No newline at end of file this.outputHeight = height - this.inputHeight
}
},
data() {
return {
current: {
input: "",
password: "",
sm2CipherMode: "C1C3C2",
output: "",
type: "AES",
operation: ""
},
type: ["AES", "DES", "RC4", "Rabbit", "TripleDES", "SM2"],
inputHeight: 100,
outputHeight: 100
}
},
}
</script>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册