program story

왜 java.security.NoSuchProviderException 그런 공급자가 없습니까 : BC?

inputbox 2020. 11. 27. 08:05
반응형

왜 java.security.NoSuchProviderException 그런 공급자가 없습니까 : BC?


jar (bcprov-jdk16-145.jar)가 프로젝트 Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider())에 추가되고 클래스에 추가되었으며 BouncyCastleProvider.PROVIDER_NAME"BC"를 반환하지만 AesFileIo.writeFile ()은 여전히 java.security.NoSuchProviderException No such provider: BC. 어떤 아이디어?

import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class AesFileIo {

    private static final String AES_ALGORITHM = "AES/CTR/NoPadding";
    private static final String PROVIDER = BouncyCastleProvider.PROVIDER_NAME;
    private static final byte[] AES_KEY_128 = { // Hard coded for now
        78, -90, 42, 70, -5, 20, -114, 103,
        -99, -25, 76, 95, -85, 94, 57, 54};
    private static final byte[] IV = { // Hard coded for now
        -85, -67, -5, 88, 28, 49, 49, 85,
        114, 83, -40, 119, -65, 91, 76, 108};
    private static final SecretKeySpec secretKeySpec =
            new SecretKeySpec(AES_KEY_128, "AES");
    private static final IvParameterSpec ivSpec = new IvParameterSpec(IV);

    public void AesFileIo() {
        Security.addProvider(new org.bouncycastle.jce.provider
                .BouncyCastleProvider());
    }

    public void writeFile(String fileName, String theFile) {
        try {
            Cipher cipher = Cipher.getInstance(AES_ALGORITHM, PROVIDER);
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivSpec);
            byte[] encrypted = cipher.doFinal(theFile.getBytes());
            ObjectOutputStream os = new ObjectOutputStream(
                new FileOutputStream(fileName));
            os.write(encrypted);
            os.flush();
            os.close();
        } catch (Exception e) {
            StackTraceElement se = new Exception().getStackTrace()[0];
            System.err.println(se.getFileName() + " " + se.getLineNumber()
                    + " " + e);
        }
    }
}

나는 안드로이드 sdk에 익숙하지 않지만 이미 보안에 추가 된 공급자 android-sdk와 함께 제공되는 것 같습니다 BouncyCastle.

PC 환경에서해야 할 일은 수동으로 추가하기 만하면됩니다.

Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

policy파일에 대한 액세스 권한이 있으면 다음 과 같은 항목을 추가하십시오.

security.provider.5=org.bouncycastle.jce.provider.BouncyCastleProvider 

통지 .5는 이미 추가 제공의 일련 번호와 동일합니다.


security.provider. = org.bouncycastle.jce.provider.BouncyCastleProvider를 추가하여 java.security를 ​​편집하여 보안 공급자를 추가 할 수 있습니다.

또는 수업 상단에 줄을 추가하십시오.

Security.addProvider(new BouncyCastleProvider());

you can use below line to specify provider while specifying algorithms

Cipher cipher = Cipher.getInstance("AES", "SunJCE");

if you are using other provider like Bouncy Castle then

Cipher cipher =  Cipher.getInstance("AES", "BC");

You can add security provider by editing java.security with using following code with creating static block:

static {
    Security.addProvider(new BouncyCastleProvider());
}

If you are using maven project, then you will have to add dependency for BouncyCastleProvider as follows in pom.xml file of your project.

<dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-jdk15on</artifactId>
            <version>1.47</version>
</dependency>

If you are using normal java project, then you can add download bcprov-jdk15on-147.jar from the link given below and edit your classpath.

http://www.java2s.com/Code/Jar/b/Downloadbcprovextjdk15on147jar.htm


For those who are using web servers make sure that the bcprov-jdk16-145.jar has been installed in you servers lib, for weblogic had to put the jar in:

<weblogic_jdk_home>\jre\lib\ext

참고URL : https://stackoverflow.com/questions/3711754/why-java-security-nosuchproviderexception-no-such-provider-bc

반응형