Java Code Examples for java.security.interfaces.DSAPublicKey#getEncoded()
The following examples show how to use
java.security.interfaces.DSAPublicKey#getEncoded() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DSAKeyUtil.java From quarkus with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws Exception { Provider provider = Security.getProvider("SUN"); System.out.println(provider.getInfo()); for (Provider.Service service : provider.getServices()) { System.out.println(service); } KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA"); kpg.initialize(2048); System.out.println("DSA.provider: " + kpg.getProvider()); KeyFactory keyFactory = KeyFactory.getInstance("DSA"); System.out.println("DSA.provider: " + keyFactory.getProvider()); KeyPair pair = kpg.genKeyPair(); DSAPublicKey publicKey = (DSAPublicKey) pair.getPublic(); byte[] encoded = publicKey.getEncoded(); X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encoded); DSAPublicKey publicKey1 = (DSAPublicKey) keyFactory.generatePublic(publicKeySpec); System.out.printf("keys are equal: %s%n", publicKey1.equals(publicKey)); String base64 = Base64.getEncoder().encodeToString(encoded); System.out.println(base64); }
Example 2
Source File: DSAPublicKeyObjectSubstitution.java From quarkus with Apache License 2.0 | 5 votes |
@Override public KeyProxy serialize(DSAPublicKey obj) { log.info("DSAPublicKeyObjectSubstitution.serialize"); byte[] encoded = obj.getEncoded(); KeyProxy proxy = new KeyProxy(); proxy.setContent(encoded); return proxy; }
Example 3
Source File: DSATest.java From java_security with MIT License | 5 votes |
/** * * @author timliu * 说明: 用java的jdk里面相关方法实现dsa的签名及签名验证 */ public static void jdkDSA() { try { // 1.初始化密钥 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DSA"); keyPairGenerator.initialize(512); KeyPair keyPair = keyPairGenerator.generateKeyPair(); DSAPublicKey dsaPublicKey = (DSAPublicKey)keyPair.getPublic(); DSAPrivateKey dsaPrivateKey = (DSAPrivateKey)keyPair.getPrivate(); // 2.进行签名 PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(dsaPrivateKey.getEncoded()); KeyFactory keyFactory = KeyFactory.getInstance("DSA"); PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec); Signature signature = Signature.getInstance("SHA1withDSA"); signature.initSign(privateKey); signature.update(src.getBytes()); byte[] result = signature.sign(); System.out.println("jdk dsa sign:" + Hex.encodeHexString(result) ); // 3.验证签名 X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(dsaPublicKey.getEncoded()); keyFactory = KeyFactory.getInstance("DSA"); PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec); signature = Signature.getInstance("SHA1withDSA"); signature.initVerify(publicKey); signature.update(src.getBytes()); boolean bool = signature.verify(result); System.out.println("jdk dsa verify:" + bool); } catch (Exception e) { System.out.println(e.toString()); } }