java 加密的示例

java 加密的示例,第1张

import javasecurity;

import javaxcrypto;

import javaio;

public class tCipher

{

public static void main(String[] args)

{

test_Cipher();

}

public static void test_Cipher()

{

try

{

//待加密的数据

String strToEnc = "Hello Java!";

byte[] plainText = strToEncgetBytes();

Systemoutprintln( "\n开始生成DES密钥" );

KeyGenerator keyGen = KeyGeneratorgetInstance("DES"); //初始化为DES算法

keyGeninit(56); //设置其密钥长度,56bits

Key key = keyGengenerateKey(); //生成密钥

Systemoutprintln( "生成DES密钥成功。" );

//打印出DES密钥

byte[] keyencode=keygetEncoded();

PrintHex(keyencode,keyencodelength);

//生成Cipher对象,设置算法为ECB模式的DES算法,补位填充模式为PKCS5

Cipher cipher = CiphergetInstance("DES/ECB/PKCS5Padding");

//打印Cipher对象密码服务提供者信息

Systemoutprintln( "\n" + ciphergetProvider()getInfo() );

// 加密

Systemoutprintln( "\n开始加密" );

cipherinit(CipherENCRYPT_MODE, key);//cipher对象初始化,设置为加密

byte[] cipherText = cipherdoFinal(plainText);//结束数据加密,输出密文

Systemoutprintln( "加密完成,密文为: " );

PrintHex(cipherText,cipherTextlength);//打印密文

// 使用相同的key解密数据

Systemoutprintln( "\n开始解密" );

cipherinit(CipherDECRYPT_MODE, key);

byte[] newPlainText = cipherdoFinal(cipherText);

Systemoutprintln( "解密完成 ,明文为:" );

//输出原文

Systemoutprintln( new String(newPlainText, "UTF8") );

}

catch (Exception e)

{

Systemoutprintln("加解密出错。");

}

}

public static void PrintHex(byte data[],int len)

{

int i;

int tmp;

String Tmp="";

for(i=0; i<len; i++)

{

if(i%16 == 0)

{

Systemoutprintln("");

//0x0000

if(i<0x10)

Tmp = "0x000";

if((i<0x100) && (i>=0x10))

Tmp = "0x00";

if((i>=0x100)&&(i<0x1000))

Tmp = "0x0";

if(i>=0x1000)

Tmp = "0x";

Systemoutprint(Tmp+IntegertoHexString(i)+"h: ");

}

tmp = data[i];

if(tmp < 0)

tmp = 256 + tmp;

if(tmp <0x10)

Systemoutprint("0"+IntegertoHexString(tmp) +" ");

else

Systemoutprint(IntegertoHexString(tmp) +" ");

}

Systemoutprintln("");

}

}

这个算法java SDK自带的额 参考代码如下:

/解密

 @param content  待解密内容

 @param password 解密密钥

 @return

/

public static byte[] decrypt(byte[] content, String password) {

try {

KeyGenerator kgen = KeyGeneratorgetInstance("AES");

kgeninit(128, new SecureRandom(passwordgetBytes()));

SecretKey secretKey = kgengenerateKey();

byte[] enCodeFormat = secretKeygetEncoded();

SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");

Cipher cipher = CiphergetInstance("AES");// 创建密码器

cipherinit(CipherDECRYPT_MODE, key);// 初始化

byte[] result = cipherdoFinal(content);

return result; // 加密

} catch (NoSuchAlgorithmException e) {

eprintStackTrace();

} catch (NoSuchPaddingException e) {

eprintStackTrace();

} catch (InvalidKeyException e) {

eprintStackTrace();

} catch (IllegalBlockSizeException e) {

eprintStackTrace();

} catch (BadPaddingException e) {

eprintStackTrace();

}

return null;

}

/

 加密

 @param content 需要加密的内容

 @param password  加密密码

 @return

/

public static byte[] encrypt(String content, String password) {

try {

KeyGenerator kgen = KeyGeneratorgetInstance("AES");

kgeninit(128, new SecureRandom(passwordgetBytes()));

SecretKey secretKey = kgengenerateKey();

byte[] enCodeFormat = secretKeygetEncoded();

SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");

Cipher cipher = CiphergetInstance("AES");// 创建密码器

byte[] byteContent = contentgetBytes("utf-8");

cipherinit(CipherENCRYPT_MODE, key);// 初始化

byte[] result = cipherdoFinal(byteContent);

return result; // 加密

} catch (NoSuchAlgorithmException e) {

eprintStackTrace();

} catch (NoSuchPaddingException e) {

eprintStackTrace();

} catch (InvalidKeyException e) {

eprintStackTrace();

} catch (UnsupportedEncodingException e) {

eprintStackTrace();

} catch (IllegalBlockSizeException e) {

eprintStackTrace();

} catch (BadPaddingException e) {

eprintStackTrace();

}

return null;

}

>

以上就是关于java 加密的示例全部的内容,包括:java 加密的示例、java实现ase加密解密、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/10165763.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存