`

DES/CBC/PKCS5Padding密码

    博客分类:
  • Java
阅读更多

DES/CBC/PKCS5Padding 加密解密

 

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.IvParameterSpec;

public class MainClass {
  public static void main(String args[]) throws Exception {
    KeyGenerator kg = KeyGenerator.getInstance("DES");
    Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding");
    Key key = kg.generateKey();

    c.init(Cipher.ENCRYPT_MODE, key);
    byte input[] = "Stand and unfold yourself".getBytes();
    byte encrypted[] = c.doFinal(input);
    byte iv[] = c.getIV();

    IvParameterSpec dps = new IvParameterSpec(iv);
    c.init(Cipher.DECRYPT_MODE, key, dps);
    byte output[] = c.doFinal(encrypted);
    System.out.println(new String(output));
  }
}
 
分享到:
评论
1 楼 zerostar88 2011-05-10  
but DES now is deprecated, we usually use AES instead, if use CBC mode, it need IV param, which is 16 bytes

相关推荐

Global site tag (gtag.js) - Google Analytics