My linux world » Java – Encryption

Java - Encryption


Get list off all supported algorithms

List<String> algorithms = new ArrayList<>();		
 
Provider[] providers = Security.getProviders();
for (Provider provider : providers) {
  Set<Service> services = provider.getServices();
  for (Service s : services) {
    if ("MessageDigest".equals(s.getType())) {
      algorithms.add(s.getAlgorithm());		       
    }
  }
}
 
// algoritms contains list of all supported algorithms.

Encrypt string to md5

String algorithm="MD5";
// Initialize MessageDigest :
MessageDigest md = MessageDigest.getInstance(algorithm);
 
// add string bytes
md.update("Hello World".getBytes());
byte[] digest = md.digest();
 
// get encrypt value :
StringBuilder sb = new StringBuilder();
for (byte b : digest) {
  sb.append(String.format("%02x", b & 0xff));
}
String result = sb.toString();

Note : The result must be b10a8db164e0754105b7a99be72e3fe5


Copyright © 2024 My linux world - by Marc RABAHI
Design by Marc RABAHI and encelades.