Problem:
java.security.NoSuchProviderException: no such provider: BC
Solution:
1. Download Bouncy Castle JAR, e.g.: bcprov-jdk15on-157.jar
2. In the code, add:
... and Thoughts
Problem:
java.security.NoSuchProviderException: no such provider: BC
Solution:
1. Download Bouncy Castle JAR, e.g.: bcprov-jdk15on-157.jar
2. In the code, add:
Instead of:
> java -Xmx256M ...
We can:
> set JAVA_TOOL_OPTIONS=-Xmx256M
> java ...
JAVA_TOOL_OPTIONS may not work with jvm.dll. For that we can try _JAVA_OPTIONS. e.g.
> set _JAVA_OPTIONS=-Xmx256M
For a C++ library, we can use command ldd to list the dependencies. There isn't such a official tool for the Java. However, if the source code is available, we can build the JAR using javac with the verbose mode turned on. The location of all the dependent JARs will be listed during the build.
Create an ObjectMapper object and let Spring Boot use our ObjectMapper object with the @Bean and @Primary annotations.
In our configuration bean:
1. Set the breakpoints.
2. Right click on the JUnit test Java class. On the popup menu, select Debug as... -> JUnit.
To install docker on fedora
$ sudo dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo
$ sudo dnf install docker-ce docker-ce-cli containerd.io
$ sudo systemctl start docker
$ sudo docker run hello-world
To build and run an image
First, create a Dockerfile under the current directory (see below).
$ sudo docker build -t myimagename:myimageversion .
$ sudo docker run -p 8888:8080 myimagename:myimageversionor
$ sudo docker run --net=host myimagename:myimageversion
Other commands
$ sudo docker image ls
$ sudo docker rmi image-hash
$ sudo docker ps -as
$ sudo docker rm container-hash
$ sudo docker run -it myimagename:myimageversion bash
Dockerfile of adding self CA and Bouncy Castle jar
FROM tomcat:9.0.44-jdk8
# Add myCA certificate
ADD myCA.crt /usr/local/share/ca-certificates/
RUN chmod 644 /usr/local/share/ca-certificates/myCA.crt && update-ca-certificates
# Add Bouncy Castle provider
RUN echo '' >> "/usr/local/openjdk-8/jre/lib/security/java.security"
RUN echo 'security.provider.10=org.bouncycastle.jce.provider.BouncyCastleProvider' >> "/usr/local/openjdk-8/jre/lib/security/java.security"
ADD bcprov-jdk15on.jar /usr/local/openjdk-8/jre/lib/ext/
# Add web app
ADD myapp.war /usr/local/tomcat/webapps/
EXPOSE 8080
CMD ["catalina.sh", "run"]
If "Install New Software" fails in Eclipse and it complains that the server's certificate is not trusted, that may be because the Java instance that Eclipse uses does not have the needed CA in its keystore.
Another symptom is that when you try to access Eclipse Marketplace in Eclipse (Help|Eclipse Marketplace...), exceptions are thrown complaining about the server certificate.
Usually Eclipse uses the Java in the system, and you can simple add the CA into the keystore following this post.
Sometimes Eclipse uses a different Java instance and to find out which one it is, you need to go to the installation of Eclipse and find this file eclipse.ini. Use a text editor to open it. Find the line -vm. Under it is the Java instance that Elipse uses.
Go to where the Java instance locates, and enter its lib/security/ directory. Follow this post to add the new CA to cacerts, e.g.:
$ keytool -import -alias CloudService -keystore cacerts -file "/path/to/CloudServiceRootCA.cer"
If Mockito is used to mock a Spring singleton bean in one test class, it may impact the second test class so that test cases in the 2nd test class could fail.
It appears that all test cases are successfuly if the 2 test classes are run independently, but when they are run together, the test cases in the 2nd test class would fail.
In that case, @DirtiesContext annotation can be used on the 1st test class. It indicates that the ApplicationContext associated with a test is dirty. After the test, the ApplicationContext would be closed and removed from the context cache. The test cases from the 2nd test class can be run independently from the 1st test class.
public class ExampleTest { public void returnMultiple() { // Return multiple strings String[] strArray = returnArray();
System.out.println("Result from returnArray():"); System.out.println(strArray[0]); System.out.println(strArray[1]); // Return boolean and string ResultObj resultObj = returnObj();
System.out.println("Result from returnObj():"); System.out.println(resultObj.boolResult); System.out.println(resultObj.strResult); } private String[] returnArray() { String[] results = new String[2]; results[0] = "result 1"; results[1] = "result 2"; return results; } public class ResultObj { public boolean boolResult; public String strResult; } private ResultObj returnObj() { ResultObj results = new ResultObj(); results.boolResult = true; results.strResult = "result string"; return results; } }
The first way is to return an Array of the same types. We can set each item of the array inside the method. And the caller and reach them by the array.
String s1 = "Hello World!"; byte[] sb = s1.getBytes(); String s2 = new String(sb); System.out.println(s1.equals(s2));
byte[] b1 = new byte[]{-100, -128, -1, 1, -10, 100, 33, -13, 32, 87}; String bs = new String(b1); byte[] b2 = bs.getBytes(); System.out.println(Arrays.equals(b1, b2));
byte[] b1 = new byte[] {-100, -128, -1, 1, -10, 100, 33, -13, 32, 87}; String bs = Base64.getEncoder().encodeToString(b1); byte[] b2 = Base64.getDecoder().decode(bs); System.out.println(Arrays.equals(b1, b2));
byte[] encrypt(byte[] plainText, String keySeed, byte[] iv) { if (plainText == null || iv == null) return null; try { SecretKey key = getSecretKey(keySeed); IvParameterSpec ivParm = new IvParameterSpec(iv); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(Cipher.ENCRYPT_MODE, key, ivParm); byte[] cipherText = cipher.doFinal(plainText); return cipherText; } catch (NoSuchAlgorithmException ex) { } catch (NoSuchPaddingException ex) { } catch (InvalidKeyException ex) { } catch (BadPaddingException ex) { } catch (IllegalBlockSizeException ex) { } catch (InvalidAlgorithmParameterException ex) { } return null; }
byte[] decrypt(byte[] cipherText, String keySeed, byte[] iv) { if (cipherText == null || iv == null) return null; try { SecretKey key = getSecretKey(keySeed); IvParameterSpec ivParm = new IvParameterSpec(iv); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(Cipher.DECRYPT_MODE, key, ivParm); byte[] plaintext = cipher.doFinal(cipherText); return plaintext; } catch (NoSuchAlgorithmException ex) { } catch (NoSuchPaddingException ex) { } catch (InvalidKeyException ex) { } catch (BadPaddingException ex) { } catch (IllegalBlockSizeException ex) { } catch (InvalidAlgorithmParameterException ex) { } return null; }
byte[] generateIv() { SecureRandom random = new SecureRandom(); byte bytes[] = new byte[16]; random.nextBytes(bytes); return bytes; }
SecretKey getSecretKey(String keySeed) throws NoSuchAlgorithmException { byte[] keySeedBytes = keySeed.getBytes(); MessageDigest md = MessageDigest.getInstance("SHA-256"); md.update(keySeedBytes); byte[] dgBytes = md.digest(); // generate a 32 bytes key SecretKey key = new SecretKeySpec(dgBytes, 0, dgBytes.length, "AES"); return key; }
void test() { byte[] b = new byte[] { 1, 2, 3, 10, 20, 30, 100}; String key = "mykey"; byte[] iv = generateIv(); byte[] enb = encrypt(b, key, iv); byte[] deb = decrypt(enb, key, iv); System.out.println(Arrays.equals(b, deb)); }
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class Object2Bytes { public static byte[] object2Bytes(Object obj) { ByteArrayOutputStream bos = null; ObjectOutputStream oos = null; try { bos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(bos); oos.writeObject(obj); oos.flush(); return bos.toByteArray(); } catch (IOException ex) { } finally { try { if (oos != null) oos.close(); if (bos != null) bos.close(); } catch (IOException ex) { } } return null; } public static Object bytes2Object(byte[] bt) { ByteArrayInputStream bis = null; ObjectInputStream ois = null; try { bis = new ByteArrayInputStream(bt); ois = new ObjectInputStream(bis); return ois.readObject(); } catch (IOException ex) { } catch (ClassNotFoundException ex) { } finally { try { if (ois != null) ois.close(); if (bis != null) bis.close(); } catch (IOException ex) { } } return null; } }
import org.junit.Test; import java.io.Serializable; public class Object2BytesTest implements Serializable { public class SimpleClass implements Serializable { private int i; public int getI() { return i; } public void setI(int i) { this.i = i; } } @Test public void test1() { SimpleClass sc = null; byte[] bt = null; sc = new SimpleClass(); sc.setI(10); bt = Object2Bytes.object2Bytes(sc); sc = (SimpleClass)Object2Bytes.bytes2Object(bt); System.out.println(sc.getI()); sc.setI(20); bt = Object2Bytes.object2Bytes(sc); sc = (SimpleClass)Object2Bytes.bytes2Object(bt); System.out.println(sc.getI()); } }