package testNumber2;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
public class EncrypDES {
// KeyGenerator 提供对称密匙生成器的功能,支持各种算法
private KeyGenerator keygen;
// SecretKey 负责保存对称密匙
private SecretKey deskey;
// Cipher 负责完成加密或解密的结果
private Cipher c;
// 改字节数组负责保存加密的结果
private byte[] cipherByte;
public EncrypDES() throws NoSuchAlgorithmException, NoSuchPaddingException {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
keygen = KeyGenerator.getInstance("DES");
deskey = keygen.generateKey();
c = Cipher.getInstance("DES");
}
public byte[] Encrytor(String str) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
c.init(Cipher.ENCRYPT_MODE, deskey);
byte[] src = str.getBytes();
cipherByte = c.doFinal(src);
return cipherByte;
}
public byte[] Decryptor(byte[] buff) throws InvalidKeyException,IllegalBlockSizeException,BadPaddingException {
c.init(Cipher.DECRYPT_MODE, deskey);
cipherByte = c.doFinal(buff);
return cipherByte;
}
public static void main(String[] args) throws Exception {
EncrypDES de1 = new EncrypDES ();
String msg = "郭xx-搞笑相声全集";
byte[] encontent = de1.Encrytor(msg);
// byte[] decontent = de1.Decryptor(encontent);
System.out.println("明文是:"+msg);
String s = new String(encontent);
System.out.println("加密后:"+s);
System.out.println("aaaa "+s);
byte[] encontent2 = s.getBytes();
System.out.println("bbbb "+encontent2);
byte[] decontent2 = de1.Decryptor(encontent2);
// System.out.println("解密后:"+new String(decontent));
System.out.println("解密后:"+new String(decontent2));
}
}
用EDS给注册时候的密码加密保存数据库,登陆时输入密码跟取出数据库的加密码解码后比较,用这上面的方法哪里不对,请大神帮忙