
Java Cryptography Programming Overview 2017
Explore a comprehensive overview of Java Cryptography Programming in 2017, covering topics such as JCA/JCE, PKI, RSA, DSA, ECDSA, and more. Learn about digital signature algorithms like DSA and RSA, and delve into the world of encryption and security protocols. Discover the significance of cryptographic techniques in Java programming and enhance your knowledge in this specialized domain.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
1 JAVA CRYPTOGRAPHY PROGRAMMING 2017. 3.
2 1. 2. 3. 4. 5. JCA/JCE 6. , MAC, 7. , / 8. 9. 10. (PKI) 11. /
9. 3
1. ? 4 ?
? 5
10 RSA ElGamal DSA NIST 1991 Nyberg-Rueppel KCDSA ECDSA DSA GQ Schnorr
RSA 11 RSA RSA (sign) : (verify) : . 1 ?
RSA 12 https://kjur.github.io/jsrsasign/sample-rsasign.html
DSA (Digital Signature Algorithm) 13 1991 NIST DSS DSA . DSS DSA . ( ) Y=g^x mod p Y, g, p x
ECDSA 14 (Elliptic Curve Digital Signature Algorithm)
2. JCA/JCE 15 java.security.Signature / getInstance( ) < >with< > Signature signature = Signature.getInstance( SHA1withRSA );
Signature 16 Signature
Signature 17
18 DSA (Digital Signature Algorithm) RSA (Rivest Shamir Adleman) EC (Elliptic Curve, )
: SignatureExample.java 20 // public static byte[] sign(PrivateKey privateKey, byte[] plainData) throws GeneralSecurityException { Signature signature = Signature.getInstance("SHA256withRSA"); signature.initSign(privateKey); signature.update(plainData); byte[] signatureData = signature.sign(); return signatureData; } // public static boolean verify(PublicKey publicKey, byte[] signatureData, byte[] plainData) throws GeneralSecurityException { Signature signature = Signature.getInstance("SHA256withRSA"); signature.initVerify(publicKey); signature.update(plainData); return signature.verify(signatureData); }
RSA 21 : SignatureExample.java KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); generator.initialize(2048); KeyPair pair = generator.generateKeyPair(); Signature signature = Signature.getInstance("SHA256withRSA"); signature.initSign(privateKey); signature.update(plainData); byte[] signatureData = signature.sign(); RSA
DSA 22 : SignatureExampleDSA.java KeyPairGenerator generator = KeyPairGenerator.getInstance( DSA"); generator.initialize(2048); KeyPair pair = generator.generateKeyPair(); Signature signature = Signature.getInstance("SHA256withDSA"); signature.initSign(privateKey); signature.update(plainData); byte[] signatureData = signature.sign(); DSA
ECDSA () 23 : SignatureExampleECDSA.java KeyPairGenerator generator = KeyPairGenerator.getInstance( EC"); generator.initialize(256); KeyPair pair = generator.generateKeyPair(); Key size must be at most 571 bits Signature signature = Signature.getInstance(" SHA256withECDSA"); signature.initSign(privateKey); signature.update(plainData); byte[] signatureData = signature.sign(); ECDSA
RSA 24 RSA . RSA/PSS Probabilistic Signature Scheme Salt Salt
RSA/PSS 25 Bouncy Castle RSA/PSS : SignatureExampleRSAPSS.java , SHA256withRSA/PSS SHA256withRSAandMGF1 BC import org.bouncycastle.jce.provider.BouncyCastleProvider; Security.addProvider(new BouncyCastleProvider()); Signature signature = Signature.getInstance("SHA256withRSA/PSS");
Bouncycastle 26 Jar https://www.bouncycastle.org/latest_releases.html bcprov-jdk15on-159.jar
Bouncycastle 27 JAR : -> -> (Properties)->Java Build Path->Libraries->Add External JARs-> jar ->OK
RSA/PSS 28 : SignatureExampleRSAPSS.java : 30820122300d06092a864886f70d01010105000382010f003082010a0282010100a23fe711474a008dbd258dd1a51ed5010a0f83d5e0a1930f110684e15 : 308204bc020100300d06092a864886f70d0101010500048204a6308204a20201000282010100a23fe711474a008dbd258dd1a51ed5010a0f83d5e0a1930 : . : 829d875834653dc879722f2b9db12393fc0e7e80af53c9f67c8d936a022e3f2bf52800e3823079046d179581dab7b173c3106ac7c50c86616706b97d12b = true : 30820122300d06092a864886f70d01010105000382010f003082010a0282010100a23fe711474a008dbd258dd1a51ed5010a0f83d5e0a1930f110684e15 : 308204bc020100300d06092a864886f70d0101010500048204a6308204a20201000282010100a23fe711474a008dbd258dd1a51ed5010a0f83d5e0a1930 : . : 02da554201b075edb51e5a74933f121260cc46ed85b871ee6641bf12b2a187b516e7995815ff3412a720cf98a0f29da8d1fccda88ffb613c3a83e7df4df = true : 30820122300d06092a864886f70d01010105000382010f003082010a0282010100a23fe711474a008dbd258dd1a51ed5010a0f83d5e0a1930f110684e15 : 308204bc020100300d06092a864886f70d0101010500048204a6308204a20201000282010100a23fe711474a008dbd258dd1a51ed5010a0f83d5e0a1930 : . : 59e1d46d1ea5a301e7e24c8cd637f40f587b43f5830d28cbaba33e1a64aa4156e92bb59ac40878bfb1832916e6c0b6446d9522d42fc044fd89649eb7b51 = true
(Digital Envelope) 29 3 : : : 1. 2. 3. 1. 2. 3.
30 Randomly Generated Session Key Bob s Public Key/Private Key Alice s Public Key Symmetric Key Cryptosystem Encrypted Message Encryption Encrypted Session Key Encrypted Message Hash Algorithm Signature Hash SEND Signing Encrypted Session Key Public Key Cryptosystem Encryption Signature
31 Bob s Public Key Alice s Public Key/Private Key Encrypted Session Key Signature Decryption Verifying Encrypted Session Key Encrypted Message Hash Algorithm Hash1 Hash2 RECEIVE Encrypted Message Decryption Signature
32 AES / // AES public static byte[] encrypt(SecretKey secretKey, byte[] plainData) throws GeneralSecurityException { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); byte[] encryptData = cipher.doFinal(plainData); return encryptData; } // AES public static byte[] decrypt(SecretKey secretKey, byte[] encryptData) throws GeneralSecurityException { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, secretKey); byte[] plainData = cipher.doFinal(encryptData); return plainData; }
33 RSA / // RSA public static byte[] encrypt(PublicKey publicKey, byte[] plainData) throws GeneralSecurityException { Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithMD5AndMGF1Padding"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] encryptData = cipher.doFinal(plainData); return encryptData; } // RSA public static byte[] decrypt(PrivateKey privateKey, byte[] encryptData) throws GeneralSecurityException { Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithMD5AndMGF1Padding"); cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] plainData = cipher.doFinal(encryptData); return plainData; }
34 RSA / // RSA public static byte[] sign(PrivateKey privateKey, byte[] plainData) throws GeneralSecurityException { Signature signature = Signature.getInstance("SHA256withRSA/PSS"); signature.initSign(privateKey); signature.update(plainData); byte[] signatureData = signature.sign(); return signatureData; } // RSA public static boolean verify(PublicKey publicKey, byte[] signatureData, byte[] plainData) throws GeneralSecurityException { Signature signature = Signature.getInstance("SHA256withRSA/PSS"); signature.initVerify(publicKey); signature.update(plainData); return signature.verify(signatureData); }
35 System.out.println("3. A "); String plainText = " ."; System.out.println(" : "+plainText); Charset charset = Charset.forName("UTF-8"); // byte[] signature = sign(privateKeyA, plainText.getBytes(charset)); System.out.println(" : "+bytesToHex(signature)); // AES KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); keyGenerator.init(128); SecretKey secretKey = keyGenerator.generateKey(); System.out.println(" : "+bytesToHex(secretKey.getEncoded())); // byte[] encryptData = encrypt(secretKey, plainText.getBytes(charset)); byte[] encryptSig = encrypt(secretKey, signature); System.out.println("AES : "+bytesToHex(encryptData)); System.out.println("AES : "+bytesToHex(encryptSig)); // B byte[] encryptKey = encrypt(publicKeyB, secretKey.getEncoded()); System.out.println("RSA : "+bytesToHex(encryptKey)); System.out.println();
36 System.out.println("4. B "); // B byte[] decryptKey = decrypt(privateKeyB, encryptKey); System.out.println(" : "+bytesToHex(decryptKey)); // SecretKeySpec SecretKey recoveredKey = new SecretKeySpec(decryptKey, "AES"); // , byte[] plainData = decrypt(recoveredKey, encryptData); String plain = new String(plainData, charset); byte[] sig = decrypt(recoveredKey, encryptSig); System.out.println(" :"+plain); System.out.println(" :"+bytesToHex(sig)); // A boolean verified = verify(publicKeyA, sig, plainData); System.out.println(" = " + verified);