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.
89 lines
3.7 KiB
89 lines
3.7 KiB
package com.fr.password.controller; |
|
|
|
import com.fr.password.Constants; |
|
import com.fr.password.service.StorageService; |
|
import com.fr.password.tool.SecurityToolbox; |
|
import org.bouncycastle.crypto.InvalidCipherTextException; |
|
import org.springframework.beans.factory.annotation.Autowired; |
|
import org.springframework.web.bind.annotation.RequestMapping; |
|
import org.springframework.web.bind.annotation.RequestParam; |
|
import org.springframework.web.bind.annotation.RestController; |
|
|
|
import java.io.IOException; |
|
import java.security.NoSuchAlgorithmException; |
|
import java.security.NoSuchProviderException; |
|
import java.security.spec.InvalidKeySpecException; |
|
|
|
@RestController |
|
@RequestMapping("/storage") |
|
public class StorageController { |
|
|
|
@Autowired |
|
private StorageService storageService; |
|
|
|
@RequestMapping("/encrypt/sm2") |
|
private String sm2Encrypt(@RequestParam String plainText, @RequestParam String key) throws InvalidCipherTextException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException, IOException { |
|
if (key.equals(Constants.DEFAULT_KEY)) { |
|
return storageService.sm2Encrypt(filter(plainText), key, false); |
|
} |
|
return storageService.sm2Encrypt(filter(plainText), key); |
|
} |
|
|
|
@RequestMapping("/decrypt/sm2") |
|
private String sm2Decrypt(@RequestParam String cipherText, @RequestParam String key) throws Exception { |
|
if (key.equals(Constants.DEFAULT_KEY)) { |
|
return storageService.sm2Decrypt(filter(cipherText), key, false); |
|
} |
|
return storageService.sm2Decrypt(filter(cipherText), key); |
|
} |
|
|
|
@RequestMapping("/encrypt/sm2/custom") |
|
private String sm2CustomEncrypt(@RequestParam String plainText) throws Exception { |
|
return storageService.sm2CustomEncrypt(filter(plainText)); |
|
} |
|
|
|
@RequestMapping("/decrypt/sm2/custom") |
|
private String sm2CustomDecrypt(@RequestParam String cipherText) throws Exception { |
|
return storageService.sm2CustomDecrypt(filter(cipherText)); |
|
} |
|
|
|
@RequestMapping("/encrypt/sm2/seed") |
|
private String sm2EncryptWithSeed(@RequestParam String plainText,@RequestParam String seed1,@RequestParam String seed2,@RequestParam String seed3) throws IOException { |
|
return storageService.sm2EncryptWithSeed(plainText,filter(seed1),filter(seed2),filter(seed3)); |
|
} |
|
|
|
@RequestMapping("/decrypt/sm2/seed") |
|
private String sm2DecryptWithSeed(@RequestParam String cipherText,@RequestParam String seed1,@RequestParam String seed2,@RequestParam String seed3) throws IOException { |
|
return storageService.sm2DecryptWithSeed(cipherText,filter(seed1),filter(seed2),filter(seed3)); |
|
} |
|
|
|
@RequestMapping("/encrypt/rsa") |
|
private String rsaEncrypt(@RequestParam String plainText, @RequestParam String key) throws IOException { |
|
if (key.equals(Constants.DEFAULT_KEY)) { |
|
return storageService.rsaEncrypt(filter(plainText), key, false); |
|
} |
|
return storageService.rsaEncrypt(filter(plainText), key, true); |
|
} |
|
|
|
@RequestMapping("/decrypt/rsa") |
|
private String rsaDecrypt(@RequestParam String cipherText, @RequestParam String key) throws IOException { |
|
if (key.equals(Constants.DEFAULT_KEY)) { |
|
return storageService.rsaDecrypt(filter(cipherText), key, false); |
|
} |
|
return storageService.rsaDecrypt(filter(cipherText), key, true); |
|
} |
|
|
|
@RequestMapping("/encrypt/rsa/seed") |
|
private String rsaEncryptWithSeed() { |
|
return null; |
|
} |
|
|
|
@RequestMapping("/decrypt/rsa/seed") |
|
private String rsaDecryptWithSeed() { |
|
return null; |
|
} |
|
|
|
private String filter(String origin) throws IOException { |
|
return new String(SecurityToolbox.getInstance().base642Byte(origin)).trim().replace("\\n","\n"); |
|
} |
|
}
|
|
|