List of usage examples for java.security PrivateKey getAlgorithm
public String getAlgorithm();
From source file:MainClass.java
public static void main(String args[]) throws Exception { MainClass kpge = new MainClass(); KeyPair kp = kpge.generateKeyPair(999); System.out.println("\n-- Private Key ----"); PrivateKey priKey = kp.getPrivate(); System.out.println(" Algorithm=" + priKey.getAlgorithm()); System.out.println(" Encoded=" + priKey.getEncoded()); System.out.println(" Format=" + priKey.getFormat()); }
From source file:Main.java
public static void main(String[] argv) throws Exception { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA"); keyGen.initialize(1024);/* ww w .j a va 2s . co m*/ KeyPair keypair = keyGen.genKeyPair(); DSAPrivateKey privateKey = (DSAPrivateKey) keypair.getPrivate(); DSAPublicKey publicKey = (DSAPublicKey) keypair.getPublic(); DSAParams dsaParams = privateKey.getParams(); BigInteger p = dsaParams.getP(); BigInteger q = dsaParams.getQ(); BigInteger g = dsaParams.getG(); BigInteger x = privateKey.getX(); BigInteger y = publicKey.getY(); // Create the DSA key factory KeyFactory keyFactory = KeyFactory.getInstance("DSA"); // Create the DSA private key KeySpec privateKeySpec = new DSAPrivateKeySpec(x, p, q, g); PrivateKey privateKey1 = keyFactory.generatePrivate(privateKeySpec); byte[] buffer = new byte[1024]; Signature sig = Signature.getInstance(privateKey1.getAlgorithm()); sig.initSign(privateKey1); sig.update(buffer, 0, buffer.length); }
From source file:Main.java
public static void main(String[] argv) throws Exception { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA"); keyGen.initialize(1024);/*from ww w .j a va 2 s . c o m*/ KeyPair keypair = keyGen.genKeyPair(); PrivateKey privateKey = keypair.getPrivate(); PublicKey publicKey = keypair.getPublic(); Serializable o = new MyClass(); Signature sig = Signature.getInstance(privateKey.getAlgorithm()); SignedObject so = new SignedObject(o, privateKey, sig); sig = Signature.getInstance(publicKey.getAlgorithm()); boolean b = so.verify(publicKey, sig); o = (MyClass) so.getObject(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA"); keyGen.initialize(1024);/* w ww .j a va 2 s . c o m*/ KeyPair keypair = keyGen.genKeyPair(); DSAPrivateKey privateKey = (DSAPrivateKey) keypair.getPrivate(); DSAPublicKey publicKey = (DSAPublicKey) keypair.getPublic(); DSAParams dsaParams = privateKey.getParams(); BigInteger p = dsaParams.getP(); BigInteger q = dsaParams.getQ(); BigInteger g = dsaParams.getG(); BigInteger x = privateKey.getX(); BigInteger y = publicKey.getY(); KeyFactory keyFactory = KeyFactory.getInstance("DSA"); KeySpec publicKeySpec = new DSAPublicKeySpec(y, p, q, g); PublicKey publicKey1 = keyFactory.generatePublic(publicKeySpec); KeySpec privateKeySpec = new DSAPrivateKeySpec(x, p, q, g); PrivateKey privateKey1 = keyFactory.generatePrivate(privateKeySpec); byte[] buffer = new byte[1024]; Signature sig = Signature.getInstance(privateKey1.getAlgorithm()); sig.initSign(privateKey1); sig.update(buffer, 0, buffer.length); byte[] signature = sig.sign(); sig = Signature.getInstance(publicKey1.getAlgorithm()); sig.initVerify(publicKey1); sig.update(buffer, 0, buffer.length); sig.verify(signature); }
From source file:prototype.samples.ExternalSigning.java
/** * External signing example/*from ww w.jav a2 s. c om*/ */ public static void main(String[] args) throws Exception { System.setProperty("digidoc4j.mode", "TEST"); Configuration configuration = new Configuration(Configuration.Mode.TEST); Container container = ContainerBuilder.aContainer().withConfiguration(configuration) .withDataFile("testFiles/test.txt", "text/plain").build(); SignatureToken externalSigner = new ExternalSigner(getSignerCert()) { @Override public byte[] sign(DigestAlgorithm digestAlgorithm, byte[] dataToSign) { // IMPLEMENT YOUR EXTERNAL SIGNING HERE try { KeyStore keyStore = KeyStore.getInstance("PKCS12"); try (FileInputStream stream = new FileInputStream("testFiles/signout.p12")) { keyStore.load(stream, "test".toCharArray()); } PrivateKey privateKey = (PrivateKey) keyStore.getKey("1", "test".toCharArray()); final String javaSignatureAlgorithm = "NONEwith" + privateKey.getAlgorithm(); return AsyncSigning.encrypt(javaSignatureAlgorithm, privateKey, addPadding(dataToSign)); } catch (Exception e) { throw new DigiDoc4JException("Loading private key failed"); } } private byte[] addPadding(byte[] digest) { return ArrayUtils.addAll(SHA256.digestInfoPrefix(), digest); } }; Signature signature = SignatureBuilder.aSignature(container).withSignatureToken(externalSigner) .invokeSigning(); container.addSignature(signature); container.save("prototype.bdoc"); }
From source file:org.digidoc4j.testutils.TestSigningHelper.java
public static byte[] sign(byte[] dataToSign, DigestAlgorithm digestAlgorithm) { try {//from www . ja v a2 s.c om KeyStore keyStore = KeyStore.getInstance("PKCS12"); try (FileInputStream stream = new FileInputStream("testFiles/signout.p12")) { keyStore.load(stream, "test".toCharArray()); } PrivateKey privateKey = (PrivateKey) keyStore.getKey("1", "test".toCharArray()); final String javaSignatureAlgorithm = "NONEwith" + privateKey.getAlgorithm(); return AsyncSigning.encrypt(javaSignatureAlgorithm, privateKey, addPadding(dataToSign, digestAlgorithm)); } catch (Exception e) { throw new DigiDoc4JException("Loading private key failed"); } }
From source file:hh.learnj.test.license.test.rsa.RSATest.java
/** * ?//from w w w . ja va2s .c om * * @param target * @throws Exception */ static void decryptionByPrivateKey(String target) throws Exception { PrivateKey privateKey = getPrivateKey(); Cipher cipher = Cipher.getInstance(privateKey.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, privateKey); cipher.update(decodeBase64(target)); String source = new String(cipher.doFinal(), "UTF-8"); System.out.println("???\r\n" + source); }
From source file:hh.learnj.test.license.test.rsa.RSATest.java
/** * ?/*from ww w . ja v a2 s. c o m*/ * * @param data * @return * @throws Exception */ static String encryptionByPrivateKey(String source) throws Exception { PrivateKey privateKey = getPrivateKey(); Cipher cipher = Cipher.getInstance(privateKey.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, privateKey); cipher.update(source.getBytes("UTF-8")); String target = encodeBase64(cipher.doFinal()); System.out.println("???\r\n" + target); return target; }
From source file:ee.sk.hwcrypto.demo.signature.TestSigningData.java
private static byte[] sign(byte[] dataToSign, DigestAlgorithm digestAlgorithm) { try {/*from www . j a v a 2 s.c o m*/ KeyStore keyStore = KeyStore.getInstance("PKCS12"); try (FileInputStream stream = new FileInputStream(TEST_PKI_CONTAINER)) { keyStore.load(stream, TEST_PKI_CONTAINER_PASSWORD.toCharArray()); } PrivateKey privateKey = (PrivateKey) keyStore.getKey("1", TEST_PKI_CONTAINER_PASSWORD.toCharArray()); final String javaSignatureAlgorithm = "NONEwith" + privateKey.getAlgorithm(); return encrypt(javaSignatureAlgorithm, privateKey, addPadding(dataToSign, digestAlgorithm)); } catch (Exception e) { throw new DigiDoc4JException("Loading private key failed"); } }
From source file:prototype.samples.AsyncSigning.java
private static byte[] getExternalSignature(X509Certificate signerCert, DataToSign dataToSign) { SignatureToken externalSigner = new ExternalSigner(signerCert) { @Override//from w w w .j a va 2 s . c om public byte[] sign(DigestAlgorithm digestAlgorithm, byte[] dataToSign) { try { KeyStore keyStore = KeyStore.getInstance("PKCS12"); try (FileInputStream stream = new FileInputStream("testFiles/signout.p12")) { keyStore.load(stream, "test".toCharArray()); } PrivateKey privateKey = (PrivateKey) keyStore.getKey("1", "test".toCharArray()); final String javaSignatureAlgorithm = "NONEwith" + privateKey.getAlgorithm(); return encrypt(javaSignatureAlgorithm, privateKey, addPadding(dataToSign)); } catch (Exception e) { throw new DigiDoc4JException("Loading private key failed"); } } private byte[] addPadding(byte[] digest) { return ArrayUtils.addAll(SHA256.digestInfoPrefix(), digest); } }; return externalSigner.sign(dataToSign.getDigestAlgorithm(), dataToSign.getDigestToSign()); }