package test;//import com.borland.datastore.*;
//import com.borland.*;
import javax.crypto.*;
import javax.crypto.NoSuchPaddingException;
import java.security.Security;
import java.security.NoSuchAlgorithmException;public class Encrypt {
  String encrypt_type="DES";//DES/ECB/PKCS5Padding
  KeyGenerator kg = null;
  SecretKey key = null;
  public Encrypt() {
    Security.addProvider(              //jce注册
       new com.sun.crypto.provider.SunJCE());
  }
  public static void main(String[] args) {    Encrypt et = new Encrypt();
    et.initEncrtpt() ;
    byte[] data="1o1o99".getBytes() ;
    byte[] result=null;
    byte[] original =null;
    result=et.encrypt(data) ;
    original=et.unencrypt(result) ;
    System.out.println(data.length);
    System.out.println(result.length);
    System.out.println(original.length);
  }  public void setEncrypt_type(String type){
    this.encrypt_type =type;
  }  public void initEncrtpt(){
    try{
    kg = KeyGenerator.getInstance(encrypt_type);
    key = kg.generateKey();
     }catch(Exception e){
       System.out.println(e.toString());
    }
  }  public byte[] encrypt(byte[] data){
  byte[] result=null;
  try{
       Cipher cipher = Cipher.getInstance(encrypt_type);
       cipher.init(Cipher.ENCRYPT_MODE, key);
       result=cipher.doFinal(data);
      }catch(Exception e){
       System.out.println(e.toString()) ;
    }
  return result;
  }  public byte[] unencrypt(byte[] result){
  byte[] original=null;
  try{     Cipher cipher = Cipher.getInstance(encrypt_type);
     cipher.init(Cipher.DECRYPT_MODE, key);
     original = cipher.doFinal(result);
     }catch(Exception e){
       System.out.println(e.toString()) ;
    }
  return original;
  }
}我也是新手,刚做的,动态注册

解决方案 »

  1.   

    to heartlessbug(多情臭虫) ;我在哪里加入自己的密钥?
      

  2.   

    key = kg.generateKey();
    随机生成的,那就是自己的密钥。可以放到本地
    你说的密钥应该是权限的管理吧?
      

  3.   

    如果用随机产生的key加密一个字符串产生加密后的文本,该文本被存入数据库。
     
    问题1. 那当用户输入密码时如何与数据库里加密过的文本相比较?
      

  4.   

    其实我是这样考虑的,用随机产生的key加密文件后,将加密后的文件放到硬盘上,然后key放到数据库种,数据库同时建立一个关联表对应着文件和key,然后再客户端通过权限对文件进行管理,如果有权限,则在他解密文件时候给他key,否则就不给key