提供测试使用的加解密验证工具🔧
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

124 lines
5.7 KiB

package com.fr.password.controller;
import com.fr.password.service.StorageService;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.spec.InvalidKeySpecException;
import java.util.Map;
@RestController
@RequestMapping("/storage")
public class StorageController {
@Autowired
private StorageService storageService;
@PostMapping("/encrypt/sm2")
private String sm2Encrypt(@RequestBody Map<String, Object> params) throws InvalidCipherTextException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException, IOException {
String key = (String) params.get("key");
String plainText = (String) params.get("text");
if (key == null) {
return storageService.sm2Encrypt(filter(plainText), key, false);
}
return storageService.sm2Encrypt(filter(plainText), key);
}
@PostMapping("/decrypt/sm2")
private String sm2Decrypt(@RequestBody Map<String, Object> params) throws Exception {
String cipherText = (String) params.get("text");
String key = (String) params.get("key");
if (key == null) {
return storageService.sm2Decrypt(filter(cipherText), key, false);
}
return storageService.sm2Decrypt(filter(cipherText), key);
}
@PostMapping("/encrypt/sm2/custom")
private String sm2CustomEncrypt(@RequestBody Map<String, Object> params) throws Exception {
String plainText = (String) params.get("text");
String key = (String) params.get("key");
if (key == null) {
return storageService.sm2CustomEncrypt(filter(plainText), key, false);
}
return storageService.sm2CustomEncrypt(filter(plainText), key, true);
}
@PostMapping("/decrypt/sm2/custom")
private String sm2CustomDecrypt(@RequestBody Map<String, Object> params) throws Exception {
String key = (String) params.get("key");
String cipherText = (String) params.get("text");
if (key == null) {
return storageService.sm2CustomDecrypt(filter(cipherText), key, false);
}
return storageService.sm2CustomDecrypt(filter(cipherText), key, true);
}
@PostMapping("/encrypt/sm2/seed")
private String sm2EncryptWithSeed(@RequestBody Map<String, Object> params) throws IOException, InvalidCipherTextException, InvalidAlgorithmParameterException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException {
String plainText = (String) params.get("plainText");
String seed1 = (String) params.get("seed1");
String seed2 = (String) params.get("seed2");
String seed3 = (String) params.get("seed3");
return storageService.sm2EncryptWithSeed(filter(plainText), filter(seed1), filter(seed2), filter(seed3));
}
@PostMapping("/decrypt/sm2/seed")
private String sm2DecryptWithSeed(@RequestBody Map<String, Object> params) throws Exception {
String cipherText = (String) params.get("plainText");
String seed1 = (String) params.get("seed1");
String seed2 = (String) params.get("seed2");
String seed3 = (String) params.get("seed3");
return storageService.sm2DecryptWithSeed(filter(cipherText), filter(seed1), filter(seed2), filter(seed3));
}
@PostMapping("/encrypt/rsa")
private String rsaEncrypt(@RequestBody Map<String, Object> params) throws IOException {
String key = (String) params.get("key");
String plainText = (String) params.get("text");
if (key == null) {
return storageService.rsaEncrypt(filter(plainText), key, false);
}
return storageService.rsaEncrypt(filter(plainText), key, true);
}
@PostMapping("/decrypt/rsa")
private String rsaDecrypt(@RequestBody Map<String, Object> params) throws IOException {
String key = (String) params.get("key");
String cipherText = (String) params.get("text");
if (key == null) {
return storageService.rsaDecrypt(filter(cipherText), key, false);
}
return storageService.rsaDecrypt(filter(cipherText), key, true);
}
@PostMapping("/encrypt/rsa/seed")
private String rsaEncryptWithSeed(@RequestBody Map<String, Object> params) throws IOException, InvalidCipherTextException, InvalidAlgorithmParameterException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException {
String plainText = (String) params.get("plainText");
String seed1 = (String) params.get("seed1");
String seed2 = (String) params.get("seed2");
String seed3 = (String) params.get("seed3");
return storageService.rsaEncryptWithSeed(filter(plainText), filter(seed1), filter(seed2), filter(seed3));
}
@PostMapping("/decrypt/rsa/seed")
private String rsaDecryptWithSeed(@RequestBody Map<String, Object> params) throws Exception {
String cipherText = (String) params.get("plainText");
String seed1 = (String) params.get("seed1");
String seed2 = (String) params.get("seed2");
String seed3 = (String) params.get("seed3");
return storageService.rsaDecryptWithSeed(filter(cipherText), filter(seed1), filter(seed2), filter(seed3));
}
private String filter(String origin) throws IOException {
return origin;
}
}