医保局加解密方法工具类
时间: 2023-08-03 09:07:19 浏览: 79
医保局的加解密方法工具类需要根据具体的加解密算法来实现,以下是一些常见的加解密算法及其Java实现:
1. 对称加密算法
对称加密算法指的是加密和解密使用相同的密钥的加密算法,例如DES、3DES、AES等。常见的Java实现如下:
- DES加解密:
```
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public class DesUtil {
private static final String DES_ALGORITHM = "DES";
public static byte[] encrypt(byte[] data, byte[] key) {
try {
DESKeySpec dks = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES_ALGORITHM);
SecretKey secretKey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance(DES_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return cipher.doFinal(data);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static byte[] decrypt(byte[] data, byte[] key) {
try {
DESKeySpec dks = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES_ALGORITHM);
SecretKey secretKey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance(DES_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return cipher.doFinal(data);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
String data = "hello world";
String key = "12345678";
byte[] encryptedData = encrypt(data.getBytes(), key.getBytes());
System.out.println("encrypted data: " + new String(encryptedData));
byte[] decryptedData = decrypt(encryptedData, key.getBytes());
System.out.println("decrypted data: " + new String(decryptedData));
}
}
```
- 3DES加解密:
```
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
public class Des3Util {
private static final String DES3_ALGORITHM = "DESede";
public static byte[] encrypt(byte[] data, byte[] key) {
try {
DESedeKeySpec dks = new DESedeKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES3_ALGORITHM);
SecretKey secretKey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance(DES3_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return cipher.doFinal(data);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static byte[] decrypt(byte[] data, byte[] key) {
try {
DESedeKeySpec dks = new DESedeKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES3_ALGORITHM);
SecretKey secretKey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance(DES3_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return cipher.doFinal(data);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
String data = "hello world";
String key = "123456781234567812345678";
byte[] encryptedData = encrypt(data.getBytes(), key.getBytes());
System.out.println("encrypted data: " + new String(encryptedData));
byte[] decryptedData = decrypt(encryptedData, key.getBytes());
System.out.println("decrypted data: " + new String(decryptedData));
}
}
```
- AES加解密:
```
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class AesUtil {
private static final String AES_ALGORITHM = "AES";
public static byte[] encrypt(byte[] data, byte[] key) {
try {
SecretKeySpec secretKeySpec = new SecretKeySpec(key, AES_ALGORITHM);
Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
return cipher.doFinal(data);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static byte[] decrypt(byte[] data, byte[] key) {
try {
SecretKeySpec secretKeySpec = new SecretKeySpec(key, AES_ALGORITHM);
Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
return cipher.doFinal(data);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
String data = "hello world";
String key = "1234567812345678";
byte[] encryptedData = encrypt(data.getBytes(), key.getBytes());
System.out.println("encrypted data: " + new String(encryptedData));
byte[] decryptedData = decrypt(encryptedData, key.getBytes());
System.out.println("decrypted data: " + new String(decryptedData));
}
}
```
2. 非对称加密算法
非对称加密算法指的是加密和解密使用不同的密钥的加密算法,例如RSA、DSA等。常见的Java实现如下:
- RSA加解密:
```
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.Cipher;
public class RsaUtil {
private static final String RSA_ALGORITHM = "RSA";
public static byte[] encrypt(byte[] data, PublicKey publicKey) {
try {
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(data);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static byte[] decrypt(byte[] data, PrivateKey privateKey) {
try {
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(data);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
String data = "hello world";
try {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(RSA_ALGORITHM);
keyPairGenerator.initialize(1024);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
byte[] encryptedData = encrypt(data.getBytes(), publicKey);
System.out.println("encrypted data: " + new String(encryptedData));
byte[] decryptedData = decrypt(encryptedData, privateKey);
System.out.println("decrypted data: " + new String(decryptedData));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
```
3. 摘要算法
摘要算法指的是将任意长度的数据转换成固定长度的数据的算法,例如MD5、SHA-1、SHA-256等。常见的Java实现如下:
- MD5摘要:
```
import java.security.MessageDigest;
public class Md5Util {
private static final String MD5_ALGORITHM = "MD5";
public static byte[] digest(byte[] data) {
try {
MessageDigest messageDigest = MessageDigest.getInstance(MD5_ALGORITHM);
return messageDigest.digest(data);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
String data = "hello world";
byte[] digest = digest(data.getBytes());
System.out.println("md5 digest: " + new String(digest));
}
}
```
- SHA-1摘要:
```
import java.security.MessageDigest;
public class Sha1Util {
private static final String SHA1_ALGORITHM = "SHA-1";
public static byte[] digest(byte[] data) {
try {
MessageDigest messageDigest = MessageDigest.getInstance(SHA1_ALGORITHM);
return messageDigest.digest(data);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
String data = "hello world";
byte[] digest = digest(data.getBytes());
System.out.println("sha1 digest: " + new String(digest));
}
}
```
- SHA-256摘要:
```
import java.security.MessageDigest;
public class Sha256Util {
private static final String SHA256_ALGORITHM = "SHA-256";
public static byte[] digest(byte[] data) {
try {
MessageDigest messageDigest = MessageDigest.getInstance(SHA256_ALGORITHM);
return messageDigest.digest(data);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
String data = "hello world";
byte[] digest = digest(data.getBytes());
System.out.println("sha256 digest: " + new String(digest));
}
}
```
以上是一些常见的加解密算法及其Java实现,具体的实现方式需要根据医保局的具体需求来进行调整。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)