Java Tutorial/Security/SecretKey

Материал из Java эксперт
Перейти к: навигация, поиск

Create a 192 bit secret key from raw bytes

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class MainClass {
  public static void main(String[] args) throws Exception {
    byte[] data = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
    Cipher c = Cipher.getInstance("Blowfish/ECB/NoPadding");
    SecretKey key192 = new SecretKeySpec(new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
        0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
        0x16, 0x17 }, "Blowfish");
    c.init(Cipher.ENCRYPT_MODE, key192);
    c.doFinal(data);
  }
}





Create a 64 bit secret key from raw bytes

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class MainClass {
  public static void main(String[] args) throws Exception {
    byte[] data = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
    SecretKey key64 = new SecretKeySpec(
        new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }, "Blowfish");
    Cipher c = Cipher.getInstance("Blowfish/ECB/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, key64);
    c.doFinal(data);
  }
}





Create SecretKey

import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
public class MainClass {
  public static void main(String[] args) throws Exception {
    KeyGenerator kg = KeyGenerator.getInstance("DESede");
    Key sharedKey = kg.generateKey();
    String password = "password";
    byte[] salt = "salt1234".getBytes();
    PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 20);
    PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
    SecretKeyFactory kf = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    SecretKey passwordKey = kf.generateSecret(keySpec);
    Cipher c = Cipher.getInstance("PBEWithMD5AndDES");
    c.init(Cipher.WRAP_MODE, passwordKey, paramSpec);
    byte[] wrappedKey = c.wrap(sharedKey);
    c = Cipher.getInstance("DESede");
    c.init(Cipher.ENCRYPT_MODE, sharedKey);
    byte[] input = "input".getBytes();
    byte[] encrypted = c.doFinal(input);
    c = Cipher.getInstance("PBEWithMD5AndDES");
    c.init(Cipher.UNWRAP_MODE, passwordKey, paramSpec);
    Key unwrappedKey = c.unwrap(wrappedKey, "DESede", Cipher.SECRET_KEY);
    c = Cipher.getInstance("DESede");
    c.init(Cipher.DECRYPT_MODE, unwrappedKey);
    System.out.println(new String(c.doFinal(encrypted)));
  }
}





implements SecretKey

import javax.crypto.SecretKey;
public class XORKey implements SecretKey {
  int rotValue;
  XORKey(int value) {
    rotValue = value;
  }
  public String getAlgorithm() {
    return "XOR";
  }
  public String getFormat() {
    return "XOR Special Format";
  }
  public byte[] getEncoded() {
    byte b[] = new byte[4];
    b[3] = (byte) ((rotValue << 24) & 0xff);
    b[2] = (byte) ((rotValue << 16) & 0xff);
    b[1] = (byte) ((rotValue << 8) & 0xff);
    b[0] = (byte) ((rotValue << 0) & 0xff);
    return b;
  }
}