怎么写一个自己的安全管理器,它判断用户的密码是否正确,如果正确就可以读写文件。大侠们帮忙

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【lovemeone】截止到2008-07-23 00:17:34的历史汇总数据(不包括此帖):
    发帖的总数量:2                        发帖的总分数:150                      每贴平均分数:75                       
    回帖的总数量:1                        得分贴总数量:0                        回帖的得分率:0%                       
    结贴的总数量:2                        结贴的总分数:150                      
    无满意结贴数:0                        无满意结贴分:0                        
    未结的帖子数:0                        未结的总分数:0                        
    结贴的百分比:100.00%               结分的百分比:100.00%                  
    无满意结贴率:0.00  %               无满意结分率:0.00  %                  
    敬礼!
      

  2.   

    import java.io.*;
    public class MySecurityManager extends SecurityManager
    {
        String password;
        MySecurityManager(String password)
        {
        super();
        this.password = password;
        }
        private boolean VerifyPassword()
        {
        BufferedReader in= new BufferedReader
        (new InputStreamReader(System.in));
        String inputString;
        System.out.println("Input Password?");
        try
           {
           inputString = in.readLine();
           if (inputString.equals(password))
           return true;
           else
           return false;
           }catch (IOException e)
           {
           return false;
           }
           }
           public void checkRead(String filename)
           {
           if (!VerifyPassword())
           throw new SecurityException("Dont read!");
           else
           System.out.println("checkRead(String filename)");
           }
           public void checkWrite(String filename)
           {
               if (!VerifyPassword())
               throw new SecurityException("Dont write!");
               else
               System.out.println("checkWrite(String filename)");
           }
    }17.怎么实现数字签名啊??
    import java.io.*;
    import java.security.*;
    public class SignFile
    {
        public static void main(String[] args)throws Exception
        {
            if (args.length != 1)
            {
                System.out.println("Usage: SignFile nameOfFileToSign");
                System.exit(-1);
            }
            /* Generate a key keyPair */
            KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA", "SUN");
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
            keyGen.initialize(1024, random);
            KeyPair keyPair = keyGen.generateKeyPair();
            PrivateKey priKey = keyPair.getPrivate();
            PublicKey pubKey = keyPair.getPublic();
            //print keyGen and key info
            System.out.println("keygen algo= "+keyGen.getAlgorithm());
            System.out.println("keygen provider= "+keyGen.getProvider());
            System.out.println("key algo= "+pubKey.getAlgorithm());
            System.out.println("key format= "+pubKey.getFormat());
            // Create a Signature object and initialize it
            Signature signObject = Signature.getInstance("SHA1withDSA", "SUN");
            signObject.initSign(priKey);
            /* supply data to to sign the data */
            DataInputStream in=new DataInputStream
            (new FileInputStream(args[0]));
            byte[] data = new byte[1024];
            in.read(data);
            signObject.update(data);
            in.close();
            /* sign data(create signature) */
            byte[] signature = signObject.sign();
            /* Save the signature in a file */
            FileOutputStream signatureFile = new FileOutputStream("signature.txt");
            signatureFile.write(signature);
            signatureFile.close();
            /* Save the public key in a file */
            byte[] key = pubKey.getEncoded();
            FileOutputStream pubKeyFile = new FileOutputStream("publicKey.txt");
            pubKeyFile.write(key);
            pubKeyFile.close();
       }
    }