资讯专栏INFORMATION COLUMN

javax.crypto.Cipher 源码学习笔记

余学文 / 1988人阅读

摘要:源码学习笔记该类是用来加密的引擎类,支持对称和非对称加密。函数创建对象操作其中方法是在中操作的方法,其他几个都使用执行。状态变化内部类内部类是用来解析中传入的字符串的。查询时,也会查询别名是否等于。知其然知其所以然。

javax.crypto.Cipher 源码学习笔记

该类是JCE用来加密的引擎类,支持对称和非对称加密。该类的介绍可以参考:[[译]JCA参考指南(二):核心类和接口](https://xshandow.gitee.io/201...

最近看了下OpenJDK中的Cipher源码,现在介绍一下Cipher的内部实现。

1.Cipher函数

创建Cipher对象:getInstance()

操作:init()、update()、doFinal()

GCM|CCM:updateAAD()

其中:getInstance()方法是在Cipher中操作的方法,其他几个都使用spi.engin**()执行。

状态变化:

2.内部类Transform

内部类Transform是用来解析getInstanse()中传入的transform字符串的。

setModePadding(CipherSpi spi)中通过spi.engineSetMode(mode|pad)设置,反馈模式和补丁方案。

判断Provider.service是否支持提供的mod和pad.

3.getInstanse()

getInstanse()传入的转换的格式如下:

“algorithm/mode/padding” or(标准名称)

“algorithm”

只传入transformation:

</>复制代码

  1. public static final Cipher getInstance(String transformation)
  2. throws NoSuchAlgorithmException, NoSuchPaddingException
  3. {
  4. //获取Transform列表,带alg
  5. List transforms = getTransforms(transformation);
  6. List cipherServices = new ArrayList(transforms.size());
  7. for (Iterator t = transforms.iterator(); t.hasNext(); ) {
  8. Transform transform = (Transform)t.next();
  9. //transform = alg + sufix(/[mode] + /[padding])
  10. //有四种形式:mode和padding是可选的
  11. cipherServices.add(new ServiceId("Cipher", transform.transform));
  12. }
  13. //获取支持Transform(alg+suffix)的List
  14. //所有的Provider全部遍历一遍
  15. List services = GetInstance.getServices(cipherServices);
  16. // make sure there is at least one service from a signed provider
  17. // and that it can use the specified mode and padding
  18. Iterator t = services.iterator();
  19. Exception failure = null;
  20. while (t.hasNext()) {
  21. Service s = (Service)t.next();
  22. if (JceSecurity.canUseProvider(s.getProvider()) == false) {
  23. continue;
  24. }
  25. //返回 transforms.suffix和算法的后缀一样的第一个Transform
  26. Transform tr = getTransform(s, transforms);
  27. if (tr == null) {
  28. // should never happen
  29. continue;
  30. }
  31. //是否支持
  32. int canuse = tr.supportsModePadding(s);
  33. if (canuse == S_NO) {
  34. // does not support mode or padding we need, ignore
  35. continue;
  36. }
  37. //找到第一个结束
  38. if (canuse == S_YES) {
  39. return new Cipher(null, s, t, transformation, transforms);
  40. } else { // S_MAYBE, try out if it works
  41. try {
  42. CipherSpi spi = (CipherSpi)s.newInstance(null);
  43. tr.setModePadding(spi);
  44. return new Cipher(spi, s, t, transformation, transforms);
  45. } catch (Exception e) {
  46. failure = e;
  47. }
  48. }
  49. }
  50. throw new NoSuchAlgorithmException
  51. ("Cannot find any provider supporting " + transformation, failure);
  52. }

另一种同时传入Provider:

</>复制代码

  1. public static final Cipher getInstance(String transformation,Provider provider)
  2. throws NoSuchAlgorithmException, NoSuchPaddingException
  3. {
  4. if (provider == null) {
  5. throw new IllegalArgumentException("Missing provider");
  6. }
  7. Exception failure = null;
  8. List transforms = getTransforms(transformation);
  9. boolean providerChecked = false;
  10. String paddingError = null;
  11. for (Iterator t = transforms.iterator(); t.hasNext();) {
  12. Transform tr = (Transform)t.next();
  13. //直接搜该provider是否支持
  14. Service s = provider.getService("Cipher", tr.transform);
  15. if (s == null) {
  16. continue;
  17. }
  18. if (providerChecked == false) {
  19. // for compatibility, first do the lookup and then verify
  20. // the provider. this makes the difference between a NSAE
  21. // and a SecurityException if the
  22. // provider does not support the algorithm.
  23. Exception ve = JceSecurity.getVerificationResult(provider);
  24. if (ve != null) {
  25. String msg = "JCE cannot authenticate the provider "
  26. + provider.getName();
  27. throw new SecurityException(msg, ve);
  28. }
  29. providerChecked = true;
  30. }
  31. if (tr.supportsMode(s) == S_NO) {
  32. continue;
  33. }
  34. if (tr.supportsPadding(s) == S_NO) {
  35. paddingError = tr.pad;
  36. continue;
  37. }
  38. try {
  39. CipherSpi spi = (CipherSpi)s.newInstance(null);
  40. tr.setModePadding(spi);
  41. Cipher cipher = new Cipher(spi, transformation);
  42. cipher.provider = s.getProvider();
  43. cipher.initCryptoPermission();
  44. return cipher;
  45. } catch (Exception e) {
  46. failure = e;
  47. }
  48. }
  49. // throw NoSuchPaddingException if the problem is with padding
  50. if (failure instanceof NoSuchPaddingException) {
  51. throw (NoSuchPaddingException)failure;
  52. }
  53. if (paddingError != null) {
  54. throw new NoSuchPaddingException
  55. ("Padding not supported: " + paddingError);
  56. }
  57. throw new NoSuchAlgorithmException
  58. ("No such algorithm: " + transformation, failure);
  59. }

</>复制代码

  1. 第一个getInstance(),将transform转换成内部Transform,遍历所有的Provider,查询到第一个支持transform的Service,然后new Cipher().

  2. 第二种getInstance(),将transform转换成内部Transform,直接通过provider.getService("Cipher", tr.transform)查询是否支持transform,然后new Cipher().

注意:

tr.transform是通过下面介绍的的函数获得的。

查询service时,也会查询别名是否等于tr.transform。

4.List getTransforms(String transformation)

</>复制代码

  1. /**
  2. * 获取Transform的List列表
  3. */
  4. private static List getTransforms(String transformation)
  5. throws NoSuchAlgorithmException {
  6. String[] parts = tokenizeTransformation(transformation);
  7. String alg = parts[0];
  8. String mode = parts[1];
  9. String pad = parts[2];
  10. if ((mode != null) && (mode.length() == 0)) {
  11. mode = null;
  12. }
  13. if ((pad != null) && (pad.length() == 0)) {
  14. pad = null;
  15. }
  16. //Transform 仅有alg
  17. if ((mode == null) && (pad == null)) {
  18. // DES
  19. Transform tr = new Transform(alg, "", null, null);
  20. return Collections.singletonList(tr);
  21. } else {
  22. // Transform = alg/mode/padding 的格式
  23. // if ((mode != null) && (pad != null)) {
  24. // DES/CBC/PKCS5Padding
  25. List list = new ArrayList(4);
  26. list.add(new Transform(alg, "/" + mode + "/" + pad, null, null));
  27. list.add(new Transform(alg, "/" + mode, null, pad));
  28. list.add(new Transform(alg, "//" + pad, mode, null));
  29. list.add(new Transform(alg, "", mode, pad));
  30. return list;
  31. }
  32. }

</>复制代码

  1. 如果transform的格式时“algorithm/mode/padding”,会输出4中形式的Transform,查询支持某种就会返回。
4.ini()

</>复制代码

  1. public final void init(int opmode, Key key, SecureRandom random)
  2. throws InvalidKeyException
  3. {
  4. initialized = false;
  5. checkOpmode(opmode);
  6. if (spi != null) {
  7. checkCryptoPerm(spi, key);
  8. spi.engineInit(opmode, key, random);
  9. } else {
  10. try {
  11. chooseProvider(I_KEY, opmode, key, null, null, random);
  12. } catch (InvalidAlgorithmParameterException e) {
  13. // should never occur
  14. throw new InvalidKeyException(e);
  15. }
  16. }
  17. initialized = true;
  18. this.opmode = opmode;
  19. }

</>复制代码

  1. 如上代码,执行ini()、update()..等操作时,其实是执行spi.engin**()

  2. 如果使用的mod需要传入IV,这使用,init(Mode,Key,IvParameterSpec)出入IV。

  3. GCMParameterSpec的时候IV必须每次都不同。

Transform标准名称

可以参考Java Cryptography Architecture Standard Algorithm Name Documentation for JDK 8

总结

看Cipher源码的原因是因为,在看BC的时候看到支持的transform列表中支持的是RSA/OAEP的加密模式,但是JCE中要求的传日格式是“algorithm/mode/padding” or(标准名称)“algorithm”,因此就产生了以问。

</>复制代码

  1. BC: Cipher.RSA -> org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi$NoPadding
  2. aliases: [RSA//RAW, RSA//NOPADDING]
  3. attributes: {SupportedKeyFormats=PKCS#8|X.509, SupportedKeyClasses=javax.crypto.interfaces.RSAPublicKey|javax.crypto.interfaces.RSAPrivateKey}
  4. BC: Cipher.RSA/RAW -> org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi$NoPadding
  5. BC: Cipher.RSA/PKCS1 -> org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi$PKCS1v1_5Padding
  6. aliases: [RSA//PKCS1PADDING]
  7. BC: Cipher.RSA/1 -> org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi$PKCS1v1_5Padding_PrivateOnly
  8. BC: Cipher.RSA/2 -> org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi$PKCS1v1_5Padding_PublicOnly
  9. BC: Cipher.RSA/OAEP -> org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi$OAEPPadding
  10. aliases: [RSA//OAEPPADDING]
  11. BC: Cipher.RSA/ISO9796-1 -> org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi$ISO9796d1Padding
  12. aliases: [RSA//ISO9796-1PADDING]

可以看出RSA/OAEP的别名正事RSA//OAEPPADING,所以也能够查到RSA//OAEPPADING对应的正是service中的RSA/OAEP。

知其然知其所以然。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/76821.html

相关文章

  • 慕课网_《Java实现对称加密》学习总结

    时间:2017年4月11日星期二说明:本文部分内容均来自慕课网。@慕课网:http://www.imooc.com教学示例源码:https://github.com/zccodere/s...个人学习源码:https://github.com/zccodere/s... 第一章:对称加密算法DES 1-1 JAVA对称加密算法DES 加密密钥=解密密钥 对称加密算法 初等 DES --3D...

    tomlingtm 评论0 收藏0
  • 慕课网_《Java实现非对称加密》学习总结

    摘要:时间年月日星期三说明本文部分内容均来自慕课网。秘密密钥,生成一个分组的秘密密钥。 时间:2017年4月12日星期三说明:本文部分内容均来自慕课网。@慕课网:http://www.imooc.com教学示例源码:https://github.com/zccodere/s...个人学习源码:https://github.com/zccodere/s... 第一章:概述 1-1 概述 非对称...

    dailybird 评论0 收藏0
  • Java加密算法笔记--DES算法实现

    摘要:加密算法笔记算法实现在使用中发现,经过加密的字符串如果要进行传输,需要使用进行编码,这样能保证加密信息的完整性,确保将来解密成功。 Java加密算法笔记--DES算法实现 在使用中发现,经过加密的字符串如果要进行传输,需要使用Base64进行编码,这样能保证加密信息的完整性,确保将来解密成功。 import java.security.SecureRandom; import java...

    BlackFlagBin 评论0 收藏0
  • 原创:微信小程序java实现AES解密并获取unionId

    摘要:来自微信小程序联盟如果大家使用小程序的同时还在使用公众号的话,可能会用到这种功能,由于公司业务需要,我们需要使用,具体使用方法,请参考微信开放平台的说明,但是在微信小程序的文档中只给出了部分语言实现的源码,竟然没有的,小程序的开发人员是有多 来自:微信小程序联盟 如果大家使用小程序的同时还在使用公众号的话,可能会用到unionId这种功能,由于公司业务需要,我们需要使用unionId,...

    jackzou 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<