javajce对称加密算法实例(jdk1.6)[size=medium]
package com.simon.security;
import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
pto.Cipher;
pto.KeyGenerator;
pto.SecretKey;
public class JceTest {
public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException{
KeyGenerator kg = Instance("DES");
// AES 要求密钥长度为128位、192位或256位。
kg.init(56);
// ⽣成秘密密钥
SecretKey secretKey = kg.generateKey();
byte[] key = Encoded();
System.out.println(secretKey);
System.out.println(key);
byte[] encode = getEncCode(new String("hello").getBytes(), secretKey);
byte[] decode = getDesCode(encode, secretKey);
System.out.println( encode );
System.out.println( new String(decode,"UTF-8") );
}
public static byte[] getEncCode(byte[] byteS, Key key) {
byte[] byteFina = null;
Cipher cipher;
try {
cipher = Instance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byteFina = cipher.doFinal(byteS);
} catch (Exception e) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
}
public static byte[] getDesCode(byte[] byteD , Key key) {
Cipher cipher;
byte[] byteFina = null;
try {
cipher = Instance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byteFina = cipher.doFinal(byteD);
} catch (Exception e) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
}
java加密方式有哪些}
[/size]
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论