DES是一种对称加密方法,这里演示其ECB加密
范例一
try
{
// 创建key
String password = "12345678";
byte[] b = password.getBytes();
DESKeySpec dks = new DESKeySpec(b);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance( "DES" );
SecretKey key = keyFactory.generateSecret( dks );
// 创建cipher
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
// 加密,对于DES来说应用调用update( ),而不是doFinal( )
String plain = "shaofa00";
byte [] input = plain.getBytes();
byte [] output = cipher.update(plain.getBytes());
}
catch(Exception e)
{
}
范例二 (推荐)
try
{
String password = "12345678";
byte[] b = password.getBytes();
SecretKeySpec ks = new SecretKeySpec(b, "DES");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, ks);
String plain = "shaofa00";
byte [] input = plain.getBytes();
byte [] output = cipher.update(plain.getBytes());
System.out.println("haha");
}
catch(Exception e)
{
}