Java安全之保证消息的机密性

package com.demo.s;

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
/**
 *
 * 私钥加密的强度取决于密码算法和密钥的长度。
 * 如果算法比较好,那么攻击它的唯一方法就是使用尝试每个可能密钥的蛮力攻击,
 * 它平均要尝试(1/2)*2*n 次,其中n 是密钥的位数。
 *
 *无填充
 * ·PKCS5
 *·OAEP
 *·SSL3
 *
 *给定密码
 * ·ECB(电子密码本(Electronic Code Book))
  ·CBC(密码块链接(Cipher Block Chaining))
  ·CFB(密码反馈方式(Cipher Feedback Mode))
  ·OFB(输出反馈方式(Output Feedback Mode))
  ·PCBC(填充密码块链接(Propagating Cipher Block Chaining))

 */
public class PrivateExample {
 public static void main(String[] args) throws Exception {

  if (args.length != 1) {
   System.err.println("Usage: java PrivateExample text");
   System.exit(1);
  }
  byte[] plainText = args[0].getBytes("UTF8");
  System.out.println("/nStart generating DES key");
  KeyGenerator keyGen = KeyGenerator.getInstance("DES");
  keyGen.init(56);
  Key key = keyGen.generateKey();
  System.out.println("Finish generating DES key");
  Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
  System.out.println("/n" + cipher.getProvider().getInfo());
  System.out.println("/nStart encryption");
  cipher.init(Cipher.ENCRYPT_MODE, key);
  byte[] cipherText = cipher.doFinal(plainText);
  System.out.println("Finish encryption: ");
  System.out.println(new String(cipherText, "UTF8"));
  System.out.println("/nStart decryption");
  cipher.init(Cipher.DECRYPT_MODE, key);
  byte[] newPlainText = cipher.doFinal(cipherText);
  System.out.println("Finish decryption: ");
  System.out.println(new String(newPlainText, "UTF8"));
 }

}
 

你可能感兴趣的:(java,exception,算法,String,byte,encryption)