?? privateexample.java
字號:
package javasecurity;
import java.security.*;
import javax.crypto.*;
public class PrivateExample
{
public static void main(String[] args) throws Exception
{
// 檢查輸入?yún)?shù),得到明文
if (args.length != 1)
{
System.err.println("Usage: java PrivateExample text");
System.exit(1);
}
byte[] plainText = args[0].getBytes("UTF8");
// 得到DES私鑰
System.out.println("\nStart generating DES key");
KeyGenerator keyGen = KeyGenerator.getInstance("DES");
keyGen.init(56);
Key key = keyGen.generateKey();
System.out.println("Finish generating DES key");
// 得到DES cipher 對象,并打印出算法的提供者
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
System.out.println("\n" + cipher.getProvider().getInfo());
// 使用密鑰對明文進(jìn)行加密
System.out.println("\nStart encryption");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] cipherText = cipher.doFinal(plainText);
System.out.println("Finish encryption: ");
System.out.println(new String(cipherText, "UTF8"));
// 使用同一把密鑰對密文進(jìn)行解密
System.out.println("\nStart decryption");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] newPlainText = cipher.doFinal(cipherText);
System.out.println("Finish decryption: ");
System.out.println(new String(newPlainText, "UTF8"));
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -