Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java Security  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 用范例出错
lping9188



发贴: 0
积分: 0
于 2003-03-05 21:01 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我试着下面程序:
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
多谢!



作者 Re:用范例出错 [Re:lping9188]
menzy



版主


发贴: 754
积分: 113
于 2003-03-06 17:40 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
ok, your definition of variables are in "try" block, and it wouldn't work outside it, so the Java VM cannot find the definition of variables.


作者 Re:用范例出错 [Re:lping9188]
lping9188



发贴: 0
积分: 0
于 2003-03-08 23:05 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Thanks alot!



flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923