Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加RSA加解密 #209

Merged
merged 3 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
rsa
  • Loading branch information
baiy committed Feb 3, 2023
commit d3bff20db441a87fa0e418234f546e154d27908c
8 changes: 6 additions & 2 deletions packages/ctool-config/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export let _tools = {
feature: ['encrypt', 'decrypt'],
parent_directory: "encrypt"
},
rsa: {
feature: ['encrypt', 'decrypt'],
parent_directory: "encrypt"
},
sign: {
feature: ['sign'],
parent_directory: ""
Expand Down Expand Up @@ -152,7 +156,7 @@ export let _tools = {
parent_directory: ""
},
ipcalc: {
feature: ['ipv4','ipv6'],
feature: ['ipv4', 'ipv6'],
parent_directory: ""
},
sqlFillParameter: {
Expand All @@ -171,7 +175,7 @@ export let _tools = {

// 分类 配置
export const _categoryTool: Record<CategoryType, ToolType[]> = {
encryption: ["hash", "aes", "des", "tripleDes", "rc4", "rabbit", "sm2", "sm4", "sign", "base64", "bcrypt"],
encryption: ["hash", "aes", "des", "tripleDes", "rc4", "rabbit", "sm2", "sm4", "rsa", "sign", "base64", "bcrypt"],
check: ["sign", "regex", "diffs", "crontab", "bcrypt", "dataValidation"],
encoder_decoder: ["base64", "url", "unicode", "jwt", "hexString", "html"],
conversion: ["json", "pinyin", "radix", "serialize", "unit", "time", "ascii", "variableConversion", "hexString", "arm", "httpSnippet"],
Expand Down
3 changes: 3 additions & 0 deletions packages/ctool-core/src/i18n/locales/en/tool.i18n.json5
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
"sm4": "SM4",
"sm4_encrypt": "Encrypt",
"sm4_decrypt": "Decrypt",
"rsa": "RSA",
"rsa_encrypt": "Encrypt",
"rsa_decrypt": "Decrypt",
"sign": "Sign",
"base64": "Base64",
"base64_encoder": "Encoder",
Expand Down
3 changes: 3 additions & 0 deletions packages/ctool-core/src/i18n/locales/zh_CN/tool.i18n.json5
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
"sm4": "SM4",
"sm4_encrypt": "加密",
"sm4_decrypt": "解密",
"rsa": "RSA",
"rsa_encrypt": "加密",
"rsa_decrypt": "解密",
"sign": "签名/验签",
"base64": "Base64",
"base64_encoder": "编码",
Expand Down
66 changes: 66 additions & 0 deletions packages/ctool-core/src/tools/encrypt/rsa/Decrypt.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<template>
<HeightResize v-slot="{small,large}" ignore :reduce="5">
<Align direction="vertical">
<div v-row="`1-1`">
<Textarea :height="small" v-model="action.current.key" :placeholder="$t(`rsa_private`)"/>
<TextInput
v-model="action.current.input"
:placeholder="$t(`rsa_decrypt_input`)"
:allow="['base64','hex']"
:height="small"
/>
</div>
<Display :position="'top-right'" toggle>
<TextOutput v-model="action.current.output" :content="output" :height="large"/>
<template #extra>
<Select :size="'small'" v-model="action.current.algName" :options="algNames"/>
</template>
</Display>
</Align>
</HeightResize>
</template>

<script lang="ts" setup>
import {useAction, initialize} from "@/store/action"
import {createTextInput, createTextOutput} from "@/components/text"
import Text from "@/helper/text"
import jsrsasign from "jsrsasign";
import {watch} from "vue";

const algNames = [
{value: 'RSA', label: 'PKCS1'},
{value: 'RSAOAEP', label: 'PKCS1_OAEP'}
]

const action = useAction(await initialize({
input: createTextInput('base64'),
algName: "RSA",
key: "",
output: createTextOutput('text'),
}))

const output = $computed(() => {
if (action.current.input.text.isEmpty() || action.current.key.trim() === "") {
return Text.empty()
}
if (action.current.input.text.isError()) {
return action.current.input.text
}
try {
return Text.fromString(jsrsasign.KJUR.crypto.Cipher.decrypt(
action.current.input.text.toHexString(),
jsrsasign.KEYUTIL.getKey(action.current.key.trim()) as jsrsasign.RSAKey,
action.current.algName
))
} catch (e) {
return Text.fromError($error(e))
}
})

watch(() => output, (output) => {
if (output.isEmpty()) {
return
}
action.save()
}, {immediate: true, deep: true})
</script>
68 changes: 68 additions & 0 deletions packages/ctool-core/src/tools/encrypt/rsa/Encrypt.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<template>
<HeightResize v-slot="{small,large}" ignore :reduce="5">
<Align direction="vertical">
<div v-row="`1-1`">
<Textarea :height="small" v-model="action.current.key" :placeholder="$t(`rsa_public`)"/>
<TextInput
v-model="action.current.input"
:placeholder="$t(`rsa_encrypt_input`)"
:height="small"
/>
</div>
<Display :position="'top-right'" toggle>
<TextOutput v-model="action.current.output" :allow="['base64','hex']" :content="output" :height="large"/>
<template #extra>
<Select :size="'small'" v-model="action.current.algName" :options="algNames"/>
</template>
</Display>
</Align>
</HeightResize>
</template>

<script lang="ts" setup>
import {useAction, initialize} from "@/store/action"
import {createTextInput, createTextOutput} from "@/components/text"
import Text from "@/helper/text"
import jsrsasign from "jsrsasign";
import {watch} from "vue";

const algNames = [
{value: 'RSA', label: 'PKCS1'},
{value: 'RSAOAEP', label: 'PKCS1_OAEP'}
]

const action = useAction(await initialize({
input: createTextInput('text'),
algName: "RSA",
key: "",
output: createTextOutput('base64'),
}))

const output = $computed(() => {
if (action.current.input.text.isEmpty() || action.current.key.trim() === "") {
return Text.empty()
}
if (action.current.input.text.isError()) {
return action.current.input.text
}
try {
if (!action.current.input.text.isText()) {
throw new Error("input content must text / text file")
}
return Text.fromHex(jsrsasign.KJUR.crypto.Cipher.encrypt(
action.current.input.text.toString(),
jsrsasign.KEYUTIL.getKey(action.current.key.trim()) as jsrsasign.RSAKey,
action.current.algName
))
} catch (e) {
return Text.fromError($error(e))
}
})

watch(() => output, (output) => {
if (output.isEmpty()) {
return
}
action.save()
}, {immediate: true, deep: true})
</script>
6 changes: 6 additions & 0 deletions packages/ctool-core/src/tools/encrypt/rsa/i18n/en.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"public": "PEM Public Key",
"private": "PEM Private Key",
"encrypt_input": "Encrypt Content",
"decrypt_input": "Decrypt Content"
}
6 changes: 6 additions & 0 deletions packages/ctool-core/src/tools/encrypt/rsa/i18n/zh_CN.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"private": "私钥(PEM格式)",
"public": "公钥(PEM格式)",
"encrypt_input": "待加密内容",
"decrypt_input": "待解密内容"
}