Mega Code Archive

 
Categories / Java Tutorial / Security
 

Get a Provider

import java.security.Key; import java.security.Provider; import java.security.SecureRandom; import java.security.Security; import javax.crypto.Cipher; public final class MainClass {   String providerName = "Rot13Provider";   String algorithmName = "ROT13";   public static void main(String[] args) throws Exception {     Provider p = Security.getProvider("Rot13Provider");     System.out.println("Provider name: " + p.getName());     System.out.println("Provider version: " + p.getVersion());     System.out.println("Provider information: " + p.getInfo());     Cipher cipher = Cipher.getInstance("ROT13", "Rot13Provider");     System.out.println("Cipher: " + cipher.getAlgorithm());     String testString = "This is a test!";     cipher.init(Cipher.ENCRYPT_MODE, (Key) null, new SecureRandom());     byte[] b1 = cipher.doFinal(testString.getBytes());     cipher.init(Cipher.DECRYPT_MODE, (Key) null, new SecureRandom());     byte[] b2 = cipher.doFinal(b1);     System.out.println("Decrypted data as a String: " + new String(b2));   } }