📄 demojce.java
字号:
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class DemoJCE{
public static void main(String args[]){
try{
System.out.println("Getting Key generator...");
KeyGenerator kgen = KeyGenerator.getInstance("Blowfish");
System.out.println("Generating key ... ");
SecretKey secretKey= kgen.generateKey();
byte[] bytes = secretKey.getEncoded();
SecretKeySpec specKey = new SecretKeySpec(bytes,"Blowfish");
//
//Create the cipher object.
//
System.out.println("Creating cipher ... ");
Cipher cipher = Cipher.getInstance("Blowfish");
System.out.println("Encryptin ... ");
cipher.init(Cipher.ENCRYPT_MODE,specKey);
String target = "Encrypt this buddy.";
byte[] encrypted = cipher.doFinal(target.getBytes());
System.out.println("beford: "+ target);
System.out.println("after:"+new String(encrypted));
//Decrypt
cipher.init(Cipher.DECRYPT_MODE,specKey);
byte[] decrypted=cipher.doFinal(encrypted);
System.out.println("\nafter decrypt:"+new String(decrypted));
}catch(Exception ex){
System.out.println("Exception caught:"+ ex);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -