'비밀번호 암호화'에 해당되는 글 1건

  1. 2008.09.01 Encryption, Decryption
Encryption, Decryption
 
       
         /**
         * Encrypt local and inherited fields with the password.
         */
        public void encrypt(String password) throws Exception {

                if (password == null || password.length() == 0)
                        return;

                // generate the key with password
                KeyGenerator generator = KeyGenerator.getInstance("DES");
                generator.init(new SecureRandom(password.getBytes()));
                Key key = generator.generateKey();

                Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
                cipher.init(Cipher.ENCRYPT_MODE, key);

               // do encryption

                // this is for padding
                BASE64Encoder encoder = new BASE64Encoder();
                program = encoder.encode(cipher.doFinal(program.getBytes()));
               

        } 

        /** 
         * Decrypt the local and inherited feilds with the password.
         */
        public void decrypt(String password) throws Exception {

                if (password == null || password.length() == 0)
                        return; 
                 
                // generate the key with password
                KeyGenerator generator = KeyGenerator.getInstance("DES");
                generator.init(new SecureRandom(password.getBytes()));
                Key key = generator.generateKey();

                Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
                cipher.init(Cipher.DECRYPT_MODE, key);

                 // do encryption
                // this is for padding
                BASE64Decoder decoder = newBASE64Decoder();
               program = new String(cipher.doFinal(decoder.decodeBuffer(program)));
        } 
Posted by [czar]
,