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.
96 lines
5.0 KiB
96 lines
5.0 KiB
package com.fr.password.service; |
|
|
|
import com.fr.password.tool.SecurityToolbox; |
|
import com.fr.password.tool.factory.SecretHelper; |
|
import com.fr.password.tool.keys.CustomSM2Keys; |
|
import com.fr.password.tool.keys.LoadSeedKeys; |
|
import com.fr.password.tool.keys.RSAKeysHandler; |
|
import com.fr.password.tool.keys.SM2KeysHandler; |
|
import org.bouncycastle.crypto.InvalidCipherTextException; |
|
import org.springframework.stereotype.Service; |
|
|
|
import java.io.IOException; |
|
import java.security.InvalidAlgorithmParameterException; |
|
import java.security.NoSuchAlgorithmException; |
|
import java.security.NoSuchProviderException; |
|
import java.security.spec.InvalidKeySpecException; |
|
|
|
@Service |
|
public class StorageService { |
|
|
|
public String sm2Encrypt(String plainText, String key) throws InvalidCipherTextException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException, IOException { |
|
return sm2Encrypt(plainText, key, true); |
|
} |
|
|
|
public String sm2Encrypt(String plainText, String key, boolean isCustom) throws InvalidKeySpecException, InvalidCipherTextException, NoSuchAlgorithmException, NoSuchProviderException, IOException { |
|
if (isCustom) { |
|
return SecurityToolbox.getInstance().sm2Encrypt(plainText, SecretHelper.loadFromText(filter(key)).get(SecretHelper.PRIVATE_KEY)); |
|
} |
|
return SecurityToolbox.getInstance().sm2Encrypt(plainText, SM2KeysHandler.getInstance().getDefaultKey()); |
|
} |
|
|
|
public String sm2Decrypt(String cipherText, String key) throws Exception { |
|
return sm2Decrypt(cipherText, key, true); |
|
} |
|
|
|
public String sm2Decrypt(String cipherText, String key, boolean isCustom) throws Exception { |
|
if (isCustom) { |
|
return SecurityToolbox.getInstance().sm2Decrypt(cipherText, SecretHelper.loadFromText(filter(key)).get(SecretHelper.PRIVATE_KEY)); |
|
} |
|
return SecurityToolbox.getInstance().sm2Decrypt(cipherText, SM2KeysHandler.getInstance().getDefaultKey()); |
|
} |
|
|
|
|
|
public String sm2EncryptWithSeed(String plainText, String seed1, String seed2, String seed3) throws InvalidKeySpecException, InvalidCipherTextException, NoSuchAlgorithmException, NoSuchProviderException, IOException, InvalidAlgorithmParameterException { |
|
byte[] random = LoadSeedKeys.getInstance().loadSeedFile(seed1, seed2, seed3); |
|
return SecurityToolbox.getInstance().sm2Encrypt(plainText, LoadSeedKeys.getInstance().generateSM2Keys(random)); |
|
} |
|
|
|
public String sm2DecryptWithSeed(String cipherText, String seed1, String seed2, String seed3) throws Exception { |
|
byte[] random = LoadSeedKeys.getInstance().loadSeedFile(seed1, seed2, seed3); |
|
return SecurityToolbox.getInstance().sm2Decrypt(cipherText, LoadSeedKeys.getInstance().generateSM2Keys(random)); |
|
} |
|
|
|
|
|
public String sm2CustomEncrypt(String plainText, String key, boolean isCustom) throws Exception { |
|
if (isCustom) { |
|
return CustomSM2Keys.getInstance().encrypt(plainText, SecretHelper.loadFromText(filter(key)).get(SecretHelper.PUBLIC_KEY)); |
|
} |
|
return CustomSM2Keys.getInstance().encrypt(plainText); |
|
} |
|
|
|
public String sm2CustomDecrypt(String cipherText, String key, boolean isCustom) throws Exception { |
|
if (isCustom) { |
|
return CustomSM2Keys.getInstance().decrypt(cipherText, SecretHelper.loadFromText(filter(key)).get(SecretHelper.PRIVATE_KEY)); |
|
} |
|
return CustomSM2Keys.getInstance().decrypt(cipherText); |
|
} |
|
|
|
public String rsaEncrypt(String plainText, String key, boolean isCustom) throws IOException { |
|
if (isCustom) { |
|
return SecurityToolbox.getInstance().rsaEncrypt(plainText, SecretHelper.loadFromText(filter(key)).get(SecretHelper.PUBLIC_KEY)); |
|
} |
|
return SecurityToolbox.getInstance().rsaEncrypt(plainText, RSAKeysHandler.getInstance().getDefaultPublicKey()); |
|
} |
|
|
|
public String rsaDecrypt(String cipherText, String key, boolean isCustom) throws IOException { |
|
if (isCustom) { |
|
return SecurityToolbox.getInstance().rsaDecrypt(cipherText, SecretHelper.loadFromText(filter(key)).get(SecretHelper.PRIVATE_KEY)); |
|
} |
|
return SecurityToolbox.getInstance().rsaDecrypt(cipherText, RSAKeysHandler.getInstance().getDefaultPrivateKey()); |
|
} |
|
|
|
public String rsaEncryptWithSeed(String plainText, String seed1, String seed2, String seed3) throws InvalidKeySpecException, InvalidCipherTextException, NoSuchAlgorithmException, NoSuchProviderException, IOException, InvalidAlgorithmParameterException { |
|
byte[] random = LoadSeedKeys.getInstance().loadSeedFile(seed1, seed2, seed3); |
|
return SecurityToolbox.getInstance().rsaEncrypt(plainText, LoadSeedKeys.getInstance().generateRSAKeys(random)[0]); |
|
} |
|
|
|
public String rsaDecryptWithSeed(String cipherText, String seed1, String seed2, String seed3) throws Exception { |
|
byte[] random = LoadSeedKeys.getInstance().loadSeedFile(seed1, seed2, seed3); |
|
return SecurityToolbox.getInstance().rsaDecrypt(cipherText, LoadSeedKeys.getInstance().generateRSAKeys(random)[1]); |
|
} |
|
|
|
private String filter(String origin) throws IOException { |
|
return origin; |
|
} |
|
}
|
|
|