DES/CBC/PKCS5Padding密码

阅读更多

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));
  }
}
 

你可能感兴趣的:(C,C++,C#,Security)