关注
Java安全——安全提供者

Java安全

安全提供者

在Java中,安全提供者(Security Provider)是一种实现了特定安全服务的软件模块。它提供了一系列的加密、解密、签名、验证和随机数生成等安全功能。安全提供者基础设施在Java中的作用是为开发人员提供一种扩展和替换标准安全功能的方式,以满足特定的安全需求。

Java的安全提供者基础设施是通过Java Cryptography Architecture(JCA)实现的。JCA定义了一组API和框架,用于在Java平台上实现各种安全服务。安全提供者是JCA的核心组件之一,它通过实现JCA规范中定义的接口,向应用程序提供安全功能。

安全提供者可以由Java平台提供的默认提供者,也可以是第三方开发的提供者。默认提供者包含在Java开发工具包(JDK)中,并提供了一些常见的加密算法和安全功能。第三方提供者则可以通过扩展JCA接口,实现自定义的加密算法和其他安全功能。

使用安全提供者,开发人员可以在应用程序中轻松地切换和配置不同的安全实现。例如,可以根据具体的安全需求选择不同的提供者,或者通过配置文件动态加载和替换提供者。这种灵活性使得Java应用程序能够适应不同的安全环境和要求。

总之,Java中的安全提供者基础设施允许开发人员使用标准或自定义的安全功能,以保护和加密数据,验证身份,以及执行其他与安全相关的操作。它为Java应用程序提供了一种可扩展和灵活的安全解决方案。

安全提供者体系结构

提供了两个概念的抽象:引擎和算法

  • 引擎

    • 安全提供者提供了操作,这些操作就是引擎的抽象
  • 算法

    • 算法如何具体执行,引擎的不同算法的实现
  • 消息摘要就是一个引擎,是程序员所能执行的一个操作

  • 消息摘要的操作是与具体计算算法无关的

安全提供者体系结构中的组件

Java安全提供者Provider体系结构中涉及到的主要组件包括以下几个方面:

  1. Provider类

Provider类是Java安全API提供商的实现类。它们实现了Java提供的标准接口,并提供了一组安全算法实现。每个Provider类都有一个唯一的名称,用于标识它们在Java运行时环境中的身份。开发人员可以通过API或者配置文件指定使用哪一个Provider类来实现相应的安全功能。

  1. Service类

Service类是Provider类的组成部分,它是实现特定算法的类。Service类提供了各种算法的实现,包括加密、签名、哈希等。Service类还提供了一组支持该算法的参数和属性。

  1. Algorithm类

Algorithm类是Java安全API提供的加密算法、哈希算法、签名算法等的抽象基类。它定义了与特定算法相关的所有方法和属性,以及与该算法相关的所有参数和属性。

  1. Key类

Key类是Java安全API提供的密钥的抽象基类。它定义了生成和管理密钥的所有方法和属性。开发人员可以使用Key类来创建、存储、和翻译密钥。

  1. Certificate类

Certificate类是Java安全API提供的证书的抽象基类。它定义了数字证书的结构和内容。开发人员可以使用Certificate类来创建、处理、验证数字证书。


In the architecture of the security provider (provider) in Java security, there are several key components:

  1. Provider: A provider is an implementation of the java.security.Provider class. It is responsible for providing specific security services, such as cryptographic algorithms, key management, secure random number generation, and more. Each provider has a unique name and can be registered and used by the Java security framework.
  2. Service: A service represents a specific security functionality provided by a provider. It is implemented as a subclass of the java.security.Provider.Service class. Examples of services include encryption algorithms, digital signature algorithms, key generators, and secure random number generators.
  3. Algorithm: An algorithm is a specific implementation of a cryptographic operation or security function. It is identified by a unique name and is provided by a service within a provider. For example, the “AES” algorithm is a specific implementation of the Advanced Encryption Standard.
  4. Key: A key is a piece of information used in cryptographic operations, such as encryption, decryption, signing, or verification. Keys can be generated, stored, and managed by the security provider. Examples of keys include symmetric encryption keys, asymmetric encryption keys (public and private keys), and digital signature keys.
  5. Secure Random: Secure random number generation is crucial for many security operations. The secure random component of the provider generates cryptographically strong random numbers that are suitable for use in key generation, nonces, initialization vectors, and other security-related purposes.

These components work together to provide a comprehensive security infrastructure in Java. The provider offers various services with specific algorithms, and keys can be generated and managed by the provider. The secure random component ensures the generation of secure random numbers for cryptographic operations. Developers can utilize these components to build secure applications and implement various security functionalities.

安全提供者的选择

在这里插入图片描述

在这里插入图片描述

设置和选择查看

Java安全提供者的选择可以在启动JVM时通过指定系统属性java.security.provider来设置,例如:

java -Djava.security.provider=SunJCE ...

可以使用以下命令查看当前使用的安全提供者:

java -Djava.security.debug=provider -version

如果需要更改安全提供者,可以在代码中使用Security.insertProviderAt(provider, position)方法将指定的安全提供者插入到安全提供者列表的指定位置。如果不指定位置,则默认为列表的末尾。另外,也可以在JRE安全配置文件中更改默认的安全提供者列表。

在选择安全提供者时,需要根据具体的安全需求来选择,例如对称加密算法、非对称加密算法、消息摘要算法等。可以参考Java官方文档或第三方安全提供者的文档来选择合适的安全提供者。同时,还需要考虑安全提供者的性能、安全性和可靠性等因素。

Simply put

To set and view the security provider selection in Java, you can use the following methods:

  1. Setting the Security Provider:

    • Programmatically: You can set the security provider programmatically using the Security class. Use the Security.addProvider(Provider provider) method to add a provider to the list of available providers. The provider added first will be the default provider.
    • Configuration File: You can also set the security provider by modifying the java.security configuration file located in the JRE_HOME/lib/security directory. In this file, providers are listed in the order of preference. You can change the order or add/remove providers as per your requirements.
  2. Viewing the Security Provider:

    • Programmatically: You can use the Security class to view the list of installed providers and their preference order. The Security.getProviders() method returns an array of installed providers in the JVM.
    • Command Line: You can use the java.security.properties system property to view the security provider configuration. Run the command java -Djava.security.properties=<path_to_file> -jar <your_jar_file> to specify a custom properties file that contains the security provider configuration.
  3. Changing the Security Provider:

    • Programmatically: To change the security provider at runtime, you can remove the existing provider using the Security.removeProvider(String name) method and then add the desired provider using Security.addProvider(Provider provider) .
    • Configuration File: To change the security provider permanently, modify the java.security configuration file and reorder the providers as needed.
  4. Selecting the Security Provider:

    • By Default: If you don’t explicitly set the security provider, the JVM uses the default provider configured in the java.security file. This default provider is typically the first provider listed in the file.
    • Programmatically: If you want to select a specific provider programmatically, you can use the Security.setProperty(String key, String value) method to set the security.provider property to the desired provider’s name. This will override the default provider selection.

Remember that the exact steps and methods may vary depending on the Java version and implementation you are using. It’s recommended to consult the official Java documentation or relevant resources for your specific Java version.

Provider 类

Java安全提供者(Security Provider)是Java Security框架的核心组成部分之一,用于提供和管理加密算法、公钥证书、密钥库等安全相关的服务。

Provider类是提供相关安全服务的实现类,每个Provider实现类都提供了多个算法的实现。在Java Security框架中,一个Provider类的对象可以包含多个同类型的算法实现(每个算法实现都实现了JCA架构规定的相应API)。例如,一个Provider对象可以同时包含多个MessageDigest算法的实现,每个实现具有不同的算法名称。当需要使用算法时,可以通过算法名称获取相应的算法实现。

使用Provider类创建Provider对象:

可以通过以下方式获取Provider对象:

  1. 根据名字获取:
Provider provider = Security.getProvider("BC");
  1. 根据类名获取:
Provider provider = Security.getProvider(BouncyCastleProvider.PROVIDER_NAME);

使用Provider类向Provider中添加算法实现:

  1. 在配置文件java.security中配置

可以通过修改jre/lib/security/java.security配置文件实现向Provider对象中添加算法实现。(以BC(BouncyCastle)Provider为例)

在java.security配置文件中添加如下配置:

security.provider.1=org.bouncycastle.jce.provider.BouncyCastleProvider
  1. 在代码中添加
Security.addProvider(new BouncyCastleProvider());

在代码中添加时,需要确保添加的Provider对象还没有添加到Provider列表中。还可以根据需求来指定提供者的优先级。

Provider类方法说明:

Provider类提供了许多方法,常用的方法如下:

  1. getName():

获取Provider对象的名字。

String name = provider.getName();
  1. getVersion():

获取Provider对象的版本号。

double version = provider.getVersion();
  1. getService():

通过类型和算法名获取提供者的服务。

Service service = provider.getService("Cipher","AES/CBC/PKCS5Padding");
  1. getServices():

获取Provider对象所有提供的服务。

Set<Service> services = provider.getServices();
  1. put():

向Provider对象中添加服务,该方法需要传入一个Service对象。

provider.put(new Service(provider,"Myalgo","MyAlgorithm",MyAlgorithm.class.getName(),null,null));

Provider类还提供了其他一些方法,如hashtable、枚举等等。具体可以查看Java API文档。

Security 类
  • 管理java程序中所要用到的提供者类。
  • 并且在安全提供者体系结构建立最后一个环节
看看安全提供者的保驾护航
public static void main(String[] args) {
        Provider[] providers = Security.getProviders();
        for(Provider provider : providers) {
            System.out.println("Provider: " + provider.getName() + " version " + provider.getVersion());
            for(Object service : provider.keySet()) {
                System.out.println("  Service: " + service);
            }
        }
    }
Security类和安全管理器

Security公共类调用要调用安全管理器方法,保证在不可信的类调用影响到虚拟机的时候能够进行干预处理。

在这里插入图片描述

引擎类体系结构

在这里插入图片描述

在这里插入图片描述

转载自CSDN-专业IT技术社区

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://blog.csdn.net/weixin_38233104/article/details/131522070

评论

赞0

评论列表

微信小程序
QQ小程序

关于作者

点赞数:0
关注数:0
粉丝:0
文章:0
关注标签:0
加入于:--