Cipher
Cipher类为加密和解密提供密码功能。它构成了JCE框架的核心(引擎类)。
NullCiper是用来验证程序的有效性,并不提供具体的加密和解密实现。
方法详解
//实例化
getInstance(String transformation)
getInstance(String transformation,Provider provider)
getInstance(String transformation,String provider)
//注意:transFormation的表达形式--"算法/工作模式/填充模式"
Cipher cipher = Cipher.getInstance("AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
DECRYPT_MODE //解密模式
ENCRYPT_MODE //加密模式
WRAP_MODE //包装操作
UNWRAP_MODE //解包装操作
//初始化
init(int opmode,Key key) //opmode对应上面常量
init(int opmode,Key key,AlgorithmParameters params)
init(int opmode,Key key,AlgorithmParameterSpec params)
init(int opmode,Key key,AlgorithmParameters params,SecureRandom random)
init(int opmode,Key key,AlgorithmParameterSpec params,SecureRandom random)
init(int opmode,Certificate certificate)
init(int opmode,Certificate certificate,SecureRandom random)
//输入给定的字节数组完成更新
update(byte[] input)
update(byte[] input,int off,int len)
//将更新结果输出到参数中
update(byte[] input,int off,int len,byte[] output)
update(byte[] input,int off,int len,byte[] output,int outputoffset)
//使用缓冲的方式
update(ByteBuffer input,ByteBuffer output)
//结束多部分加密和解密操作
doFinal()
...
//包装密钥
wrap(Key key)
//解包装
unwrap(byte[] wrappedKey,String wrappedKeyAlgorithm,int wrappedKeyType)
//wrappedKeyType对应以下常量
PRIVATE_KEY //私钥
PUBLIC_KEY //公钥
SECRET_KEY //秘密密钥
//返回缓冲区中的初始化向量
getIV()
//返回指定转换的最大密钥长度
getMaxAllowedKeyLength(String transformation)
//分组加密中,每一组都有固定的长度,称为块
getBlockSize()
//获取缓冲区字节长度
getOutputSize(int intputLen)
//返回最大参数相关信息
getMaxAllowedParameterSpec(String transformation)
//获取参数
getParameters()
//获取豁免机制对象
getExemptionMechanism()
//获取算法
getAlgorithm()
//获取提供者
getProvider()
实现实例
//包装密钥
KeyGenerator kg = KeyGenerator.getInstance("DES");
SecretKey secretKey = kg.generateKey();
Cipher cipher = Cipher.getInstance(kg.getAlgorithm());
cipher.init(Cipher.WRAP_MODE,secretKey);
byte[] bytes = cipher.wrap(secretKey);
//解包装
Cipher cipher = Cipher.getInstance(kg.getAlgorithm());
cipher.init(Cipher.UNWRAP_MODE,secretKey);
byte[] bytes = cipher.unwrap(bytes,"DES",Cipher.SECRET_KEY);
//加密 此处略去了实例化
cipher.init(Cipher.ENCRYPT_MODE,secretKey);
byte[] input = cipher.doFinal("DES DATA".getBytes());
//解密 此处略去了实例化
cipher.init(Cipher.DECRYPT_MODE,secretKey);
byte[] output = cipher.doFinal(input);
CipherInputStream和CipherOutputStream
CipherInputStream(解密)和CipherOutputStream(加密)同属Cipher类的拓展,统称为密钥流。
//解密
byte[] bytes = new BigInteger("-4299735180166341784").toByteArray(); //这个为加密时生成的密钥
DESKeySpec desKeySpec = new DESKeySpec(bytes);
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = skf.generateSecret(desKeySpec);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE,secretKey);
DataInputStream dataInputStream = new DataInputStream(new CipherInputStream(new FileInputStream("secret"),cipher));
System.out.println(dataInputStream.readUTF());
dataInputStream.close();
//加密
KeyGenerator kg = KeyGenerator.getInstance("DES");
SecretKey secretKey = kg.generateKey();
byte[] bytes = secretKey.getEncoded();
System.out.println(new BigInteger(bytes)); //打印出秘密密钥的二进制
Cipher cipher = Cipher.getInstance(kg.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE,secretKey);
CipherOutputStream cis = new CipherOutputStream(new FileOutputStream(new File("secret")),cipher);
DataOutputStream dataOutputStream = new DataOutputStream(cis);
dataOutputStream.writeUTF("1234567890"); //要加密的原文
dataOutputStream.flush();
dataOutputStream.close();
//原文为: 1234567890
//加密文件内容为: !?5谊鴿pr礷?
SealedObject
SealedObject能够用加密算法创建对象并保护其机密性。
//构造器
SealedObject(Serializable object,Cipher c)
//获取原始对象
getObject(Cipher c)
getObject(Key key)
getObject(Key key,String provider)
//返回此密封对象的算法
getAlogrithm()
代码演示
//对象加密
//代加密的对象
String input = "SealedObject";
KeyGenerator kg = KeyGenerator.getInstance("DES");
SecretKey sk = kg.generateKey();
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE,sk);
SealedObject sd = new SealedObject(input,cipher);
String
System.out.println(sd);
System.out.println(sd.getObject(sk));
//输出结果
javax.crypto.SealedObject@721e0f4f
SealedObject
自我推荐
这个是我的微信公众号,欢迎扫码关注,谢谢!