使用JSONObject处理List<T>和String互相转换

1、List转String

List<xxxDTO> list = new ArrayList<>();
String st = JSONObject.toJSONString(list )

2、String还原List

List<xxxDTO> list= JSONObject.parseArray(order.getOrderHistory(), xxxDTO.class);
JKS(Java KeyStore)文件是一种Java密钥库,用于存储密钥和证书,而JWKS(JSON Web Key Set)文件是一种用于描述一组公共密钥的JSON格式。 下面是Java代码实现JKS文件和JWKS文件的互相转换: 1. 将JKS文件转换为JWKS文件: ```java import java.io.FileInputStream; import java.security.Key; import java.security.KeyStore; import java.security.cert.Certificate; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import org.json.JSONObject; public class JksToJwksConverter { public static void main(String[] args) throws Exception { String jksFile = "path/to/your/jks/file"; String jksPassword = "your_jks_password"; String jksAlias = "your_key_alias"; KeyStore keyStore = KeyStore.getInstance("JKS"); FileInputStream fis = new FileInputStream(jksFile); keyStore.load(fis, jksPassword.toCharArray()); Key key = keyStore.getKey(jksAlias, jksPassword.toCharArray()); if (key != null) { Certificate cert = keyStore.getCertificate(jksAlias); JSONObject jwks = new JSONObject(); jwks.put("keys", new JSONArray()); JSONObject jwk = new JSONObject(); jwk.put("kty", "RSA"); // or "EC" for ECDSA keys jwk.put("use", "sig"); // or "enc" for encryption keys jwk.put("kid", jksAlias); // get public key from private key String publicKey = Base64.getEncoder().encodeToString(cert.getPublicKey().getEncoded()); jwk.put("n", publicKey); JSONArray keys = jwks.getJSONArray("keys"); keys.put(jwk); System.out.println(jwks.toString(4)); } } } ``` 2. 将JWKS文件转换为JKS文件: ```java import java.io.FileOutputStream; import java.math.BigInteger; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.KeyStore; import java.security.PrivateKey; import java.security.cert.Certificate; import java.security.cert.X509Certificate; import java.security.spec.RSAPublicKeySpec; import java.util.Date; import javax.security.auth.x500.X500Principal; import org.bouncycastle.asn1.x500.X500Name; import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; import org.bouncycastle.cert.X509CertificateHolder; import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter; import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.openssl.jcajce.JcaPEMWriter; import org.bouncycastle.util.io.pem.PemObject; public class JwksToJksConverter { public static void main(String[] args) throws Exception { String jwksFile = "path/to/your/jwks/file"; String jksFile = "path/to/your/jks/file"; String jksPassword = "your_jks_password"; String jksAlias = "your_key_alias"; KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(2048); KeyPair keyPair = keyPairGenerator.generateKeyPair(); // create X509 certificate X500Name subject = new X500Name("CN=example.com"); BigInteger serial = BigInteger.valueOf(System.currentTimeMillis()); Date notBefore = new Date(System.currentTimeMillis() - 1000L * 3600 * 24 * 30); Date notAfter = new Date(System.currentTimeMillis() + 1000L * 3600 * 24 * 365); X509CertificateHolder certHolder = new X509CertificateHolder( SubjectPublicKeyInfo.getInstance(keyPair.getPublic().getEncoded()), subject, serial, notBefore, notAfter, subject); X509Certificate cert = new JcaX509CertificateConverter().setProvider(new BouncyCastleProvider()) .getCertificate(certHolder); // create JKS keystore KeyStore keyStore = KeyStore.getInstance("JKS"); keyStore.load(null, jksPassword.toCharArray()); keyStore.setKeyEntry(jksAlias, keyPair.getPrivate(), jksPassword.toCharArray(), new Certificate[] { cert }); FileOutputStream fos = new FileOutputStream(jksFile); keyStore.store(fos, jksPassword.toCharArray()); fos.close(); } } ``` 注意:上述代码需要引入Bouncy Castle库,可以通过Maven或Gradle添加依赖: Maven: ```xml <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk15on</artifactId> <version>1.69</version> </dependency> <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcpkix-jdk15on</artifactId> <version>1.69</version> </dependency> ``` Gradle: ```groovy implementation 'org.bouncycastle:bcprov-jdk15on:1.69' implementation 'org.bouncycastle:bcpkix-jdk15on:1.69' ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值