Java Tutorial/Security/Key Generator
Содержание
Encryption and Decryption using Symmetric Keys
import java.security.InvalidKeyException;
import java.security.Key;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
public class Main {
static String algorithm = "DESede";
static Key key = KeyGenerator.getInstance(algorithm).generateKey();
static Cipher cipher = Cipher.getInstance(algorithm);
public static void main(String[] args) throws Exception {
byte[] encryptionBytes = encrypt("input");
System.out.println("Recovered: " + decrypt(encryptionBytes));
}
private static byte[] encrypt(String input) throws InvalidKeyException, BadPaddingException,
IllegalBlockSizeException {
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] inputBytes = input.getBytes();
return cipher.doFinal(inputBytes);
}
private static String decrypt(byte[] encryptionBytes) throws InvalidKeyException,
BadPaddingException, IllegalBlockSizeException {
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] recoveredBytes = cipher.doFinal(encryptionBytes);
String recovered = new String(recoveredBytes);
return recovered;
}
}
Generate a key for the HMAC-SHA1 keyed-hashing algorithm
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
public class Main {
public static void main(String[] argv) throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("HmacMD5");
SecretKey key = keyGen.generateKey();
// Generate a key for the HMAC-SHA1 keyed-hashing algorithm
keyGen = KeyGenerator.getInstance("HmacSHA1");
key = keyGen.generateKey();
}
}
Generating a Symmetric Key
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
public class Main {
public static void main(String[] argv) throws Exception {
// Generate a DES key
KeyGenerator keyGen = KeyGenerator.getInstance("DES");
SecretKey key = keyGen.generateKey();
// Generate a Blowfish key
keyGen = KeyGenerator.getInstance("Blowfish");
key = keyGen.generateKey();
// Generate a triple DES key
keyGen = KeyGenerator.getInstance("DESede");
key = keyGen.generateKey();
}
}
Get the bytes of the public and private keys
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
public class Main {
public static void main(String[] argv) throws Exception {
String algorithm = "DSA"; // or RSA, DH, etc.
// Generate a 1024-bit Digital Signature Algorithm (DSA) key pair
KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm);
keyGen.initialize(1024);
KeyPair keypair = keyGen.genKeyPair();
PrivateKey privateKey = keypair.getPrivate();
PublicKey publicKey = keypair.getPublic();
byte[] privateKeyBytes = privateKey.getEncoded();
byte[] publicKeyBytes = publicKey.getEncoded();
}
}
Get the formats of the encoded bytes
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
public class Main {
public static void main(String[] argv) throws Exception {
String algorithm = "DSA"; // or RSA, DH, etc.
// Generate a 1024-bit Digital Signature Algorithm (DSA) key pair
KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm);
keyGen.initialize(1024);
KeyPair keypair = keyGen.genKeyPair();
PrivateKey privateKey = keypair.getPrivate();
PublicKey publicKey = keypair.getPublic();
String format = privateKey.getFormat(); // PKCS#8
format = publicKey.getFormat(); // X.509
}
}
Getting the Bytes of a Generated Symmetric Key
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class Main {
public static void main(String[] argv) throws Exception {
// Generate a key
KeyGenerator keyGen = KeyGenerator.getInstance("DESede");
SecretKey key = keyGen.generateKey();
// Get the bytes of the key
byte[] keyBytes = key.getEncoded();
int numBytes = keyBytes.length;
// The bytes can be converted back to a SecretKey
SecretKey key2 = new SecretKeySpec(keyBytes, "DESede");
boolean b = key.equals(key2); // true
}
}
Key Generator Demo
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
public class MainClass {
public static void main(String[] args) {
if (args.length != 2) {
String err = "Usage: KeyGeneratorApp algorithmName keySize";
System.out.println(err);
System.exit(0);
}
int keySize = (new Integer(args[1])).intValue();
SecretKey skey = null;
KeyPair keys = null;
String algorithm = args[0];
try {
KeyPairGenerator kpg = KeyPairGenerator.getInstance(algorithm);
kpg.initialize(keySize);
keys = kpg.genKeyPair();
} catch (NoSuchAlgorithmException ex1) {
try {
KeyGenerator kg = KeyGenerator.getInstance(algorithm);
kg.init(keySize);
skey = kg.generateKey();
} catch (NoSuchAlgorithmException ex2) {
System.out.println("Algorithm not supported: " + algorithm);
System.exit(0);
}
}
}
}