lping9188
发贴: 0
积分: 0
|
于 2003-03-05 21:01
我试着下面程序: import java.security.*; import javax.crypto.*;
class crypto1 { public static void main(String[] args) {
try { KeyGenerator keygen = KeyGenerator.getInstance("DES"); SecretKey desKey = keygen.generateKey(); } catch(NoSuchAlgorithmException e) {
System.out.println("Failed to generate key"); System.exit(1); }
Cipher desCipher;
// Create the cipher
try { desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); } catch(NoSuchAlgorithmException e) {
System.out.println("Failed to get instance"); System.exit(1); }
// Initialize the cipher for encryption
try { desCipher.init(Cipher.ENCRYPT_MODE,desKey); }
catch(InvalidKeyException e) {
System.out.println("Failed to initial cipher"); System.exit(1); }
// Our cleartext byte[] cleartext = "This is just an example".getBytes();
// Encrypt the cleartext try { byte[] ciphertext = desCipher.doFinal(cleartext); }
catch(IllegalBlockSizeException e) {
System.out.println("Failed to generate key"); System.exit(1); }
// Initialize the same cipher for decryption try {
desCipher.init(Cipher.DECRYPT_MODE,desKey); } catch(InvalidKeyException e) {
System.out.println("Failed to generate key"); System.exit(1); }
try { // Decrypt the ciphertext byte[] cleartext1 = desCipher.doFinal(ciphertext); } catch(IllegalBlockSizeException e) {
System.out.println("Failed to generate key"); System.exit(1); } System.out.println("success");
} }
编译结果为: C:\java\crypto1.java:42: cannot resolve symbol symbol : variable desKey location: class crypto1 desCipher.init(Cipher.ENCRYPT_MODE,desKey); ^ C:\java\crypto1.java:74: cannot resolve symbol symbol : variable desKey location: class crypto1 desCipher.init(Cipher.DECRYPT_MODE,desKey); ^ C:\java\crypto1.java:86: cannot resolve symbol symbol : variable ciphertext location: class crypto1 byte[] cleartext1 = desCipher.doFinal(ciphertext); ^ 3 errors 不知为何,我用的是jdk1.3.1,按照说明安装了jce 多谢!
|