「java生成enc」JAVA生成随机数

博主:adminadmin 2022-12-21 12:33:08 64

今天给各位分享java生成enc的知识,其中也会对JAVA生成随机数进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

des密钥如何生成 java

package Encrypt;

import java.security.*;

import javax.crypto.*;

import sun.misc.*;

/**

* 使用DES加密与解密,可对byte[],String类型进行加密与解密

* 密文可使用String,byte[]存储.

* 方法:

* void getKey(String strKey)从strKey的字条生成一个Key

* String getEncString(String strMing)对strMing进行加密,返回String密文

* String getDesString(String strMi)对strMin进行解密,返回String明文

* byte[] getEncCode(byte[] byteS)byte[]型的加密

* byte[] getDesCode(byte[] byteD)byte[]型的解密

*/

public class Encrypt{

private Key key;

private byte[] byteMi = null;

private byte[] byteMing = null;

private String strMi= "";

private String strM= "";

// 根据参数生成KEY

public void setKey(String strKey){

try{

KeyGenerator _generator = KeyGenerator.getInstance("DES");

_generator.init(new SecureRandom(strKey.getBytes()));

this.key = _generator.generateKey();

_generator=null;

}

catch(Exception e){

e.printStackTrace();

}

}

// 加密String明文输入,String密文输出

public void setEncString(String strMing){

BASE64Encoder base64en = new BASE64Encoder();

try {

this.byteMing = strMing.getBytes("UTF8");

this.byteMi = this.getEncCode(this.byteMing);

this.strMi = base64en.encode(this.byteMi);

}

catch(Exception e)

{

e.printStackTrace();

}

finally

{

this.byteMing = null;

this.byteMi = null;

}

}

//加密以byte[]明文输入,byte[]密文输出

private byte[] getEncCode(byte[] byteS){

byte[] byteFina = null;

Cipher cipher;

try

{

cipher = Cipher.getInstance("DES");

cipher.init(Cipher.ENCRYPT_MODE,key);

byteFina = cipher.doFinal(byteS);

}

catch(Exception e)

{

e.printStackTrace();

}

finally

{

cipher = null;

}

return byteFina;

}

// 解密:以String密文输入,String明文输出

public void setDesString(String strMi){

BASE64Decoder base64De = new BASE64Decoder();

try

{

this.byteMi = base64De.decodeBuffer(strMi);

this.byteMing = this.getDesCode(byteMi);

this.strM = new String(byteMing,"UTF8");

}

catch(Exception e)

{

e.printStackTrace();

}

finally

{

base64De = null;

byteMing = null;

byteMi = null;

}

}

// 解密以byte[]密文输入,以byte[]明文输出

private byte[] getDesCode(byte[] byteD){

Cipher cipher;

byte[] byteFina=null;

try{

cipher = Cipher.getInstance("DES");

cipher.init(Cipher.DECRYPT_MODE,key);

byteFina = cipher.doFinal(byteD);

}

catch(Exception e)

{

e.printStackTrace();

}

finally

{

cipher=null;

}

return byteFina;

}

//返回加密后的密文strMi

public String getStrMi()

{

return strMi;

}

//返回解密后的明文

public String getStrM()

{

return strM;

}

}

*注意:因为我用的时候是作为后台的BEAN来调用的,所以没有写main函数,也就不能直接运行了。必须加一个main()函数才能做为一个完成的JAVA程序使用。一个很简单的 main()函数就ok了,不用我写在这里了吧?呵呵!

在WEB前台调用这个BEAN文件时的语句:

jsp:useBean id="abc" scope="page" class="oaweb.Encrypt" /

//id="abc" 用来初始化一个对象

%

//明文加密:

String key = “06”; //初始化密钥。

abc.setKey(key); //调用set函数设置密钥。

abc.setEncString(re[i][j]);//将要加密的明文传送给Encrypt.java进行加密计算。

String Mi=abc.getStrMi(); //调用get函数获取加密后密文。

%

//变量Mi就是密文.

%

//密文解密:

abc. setDesString(String Mi); //将要解密的密文传送给Encrypt.java进行解密计算。

String M=abc. String getStrM(); //调用get函数获取解密后明文。

%

//变量M就是明文.

JAVA简单文件加密 求JAVA源代码

md5加密:

package com.ncs.pki.util;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

public class MD5Test {

private static MessageDigest digest = null;

public synchronized static final String hash(String data) {

if (digest == null) {

try {

digest = MessageDigest.getInstance("MD5");

} catch (NoSuchAlgorithmException nsae) {

System.err.println(

"Failed to load the MD5 MessageDigest. "

+ "Jive will be unable to function normally.");

nsae.printStackTrace();

}

}

// Now, compute hash.

digest.update(data.getBytes());

return encodeHex(digest.digest());

}

public static final String encodeHex(byte[] bytes) {

StringBuffer buf = new StringBuffer(bytes.length * 2);

int i;

for (i = 0; i bytes.length; i++) {

if (((int) bytes[i] 0xff) 0x10) {

buf.append("0");

}

buf.append(Long.toString((int) bytes[i] 0xff, 16));

}

return buf.toString();

}

public static String test(){

return null;

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println(MD5Test.hash("123456"));

}

}

3des加密:

package com.ncs.pki.util;

import java.security.Key;

import java.security.SecureRandom;

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;

public class DesEncrypt {

/**

*

* 使用DES加密与解密,可对byte[],String类型进行加密与解密 密文可使用String,byte[]存储.

*

* 方法: void getKey(String strKey)从strKey的字条生成一个Key

*

* String getEncString(String strMing)对strMing进行加密,返回String密文 String

* getDesString(String strMi)对strMin进行解密,返回String明文

*

*byte[] getEncCode(byte[] byteS)byte[]型的加密 byte[] getDesCode(byte[]

* byteD)byte[]型的解密

*/

Key key;

/**

* 根据参数生成KEY

*

* @param strKey

*/

public void getKey(String strKey) {

try {

KeyGenerator _generator = KeyGenerator.getInstance("DES");

_generator.init(new SecureRandom(strKey.getBytes()));

this.key = _generator.generateKey();

_generator = null;

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 加密String明文输入,String密文输出

*

* @param strMing

* @return

*/

public String getEncString(String strMing) {

byte[] byteMi = null;

byte[] byteMing = null;

String strMi = "";

BASE64Encoder base64en = new BASE64Encoder();

try {

byteMing = strMing.getBytes("UTF8");

byteMi = this.getEncCode(byteMing);

strMi = base64en.encode(byteMi);

} catch (Exception e) {

e.printStackTrace();

} finally {

base64en = null;

byteMing = null;

byteMi = null;

}

return strMi;

}

/**

* 解密 以String密文输入,String明文输出

*

* @param strMi

* @return

*/

public String getDesString(String strMi) {

BASE64Decoder base64De = new BASE64Decoder();

byte[] byteMing = null;

byte[] byteMi = null;

String strMing = "";

try {

byteMi = base64De.decodeBuffer(strMi);

byteMing = this.getDesCode(byteMi);

strMing = new String(byteMing, "UTF8");

} catch (Exception e) {

e.printStackTrace();

} finally {

base64De = null;

byteMing = null;

byteMi = null;

}

return strMing;

}

/**

* 加密以byte[]明文输入,byte[]密文输出

*

* @param byteS

* @return

*/

private byte[] getEncCode(byte[] byteS) {

byte[] byteFina = null;

Cipher cipher;

try {

cipher = Cipher.getInstance("DES");

cipher.init(Cipher.ENCRYPT_MODE, key);

byteFina = cipher.doFinal(byteS);

} catch (Exception e) {

e.printStackTrace();

} finally {

cipher = null;

}

return byteFina;

}

/**

* 解密以byte[]密文输入,以byte[]明文输出

*

* @param byteD

* @return

*/

private byte[] getDesCode(byte[] byteD) {

Cipher cipher;

byte[] byteFina = null;

try {

cipher = Cipher.getInstance("DES");

cipher.init(Cipher.DECRYPT_MODE, key);

byteFina = cipher.doFinal(byteD);

} catch (Exception e) {

e.printStackTrace();

} finally {

cipher = null;

}

return byteFina;

}

public static void main(String[] args) {

System.out.println("des demo");

DesEncrypt des = new DesEncrypt();// 实例化一个对像

des.getKey("MYKEY");// 生成密匙

System.out.println("key=MYKEY");

String strEnc = des.getEncString("111111");// 加密字符串,返回String的密文

System.out.println("密文=" + strEnc);

String strDes = des.getDesString(strEnc);// 把String 类型的密文解密

System.out.println("明文=" + strDes);

}

}

如何用Java实现URLEncode

用Java实现URLEncode的方法是引入java.net.URLEncoder包。

java.net.URLDecoder.decode(String s,String enc);

将application/x-www-form-urlencoded字符串转换成普通字符串。

java.net.URLEncoder.decode(String s,String enc);

将普通字符串转换成application/x-www-form-urlencoded字符串

URLEncoder类包含将字符串转换为application/x-www-form-urlencoded MIME 格式的静态方法。

web 设计者面临的众多难题之一便是怎样处理不同操作系统间的差异性。这些差异性能引起URL方面的问题:例如,一些操作系统允许文件名中含有空格符,有些又不允许。大多数操作系统不会认为文件名中含有符号“#”会有什么特殊含义;但是在一个URL中,符号“#”表示该文件名已经结束,后面会紧跟一个 fragment(部分)标识符。其他的特殊字符,非字母数字字符集,它们在URL或另一个操作系统上都有其特殊的含义,表述着相似的问题。为了解决这些问题,我们在URL中使用的字符就必须是一个ASCII字符集的固定字集中的元素,具体如下:

1.大写字母A-Z

2.小写字母a-z

3.数字 0-9

4.标点符 - _ . ! ~ * ' (和 ,)

诸如字符: / ? @ # ; $ + = 和 %也可以被使用,但是它们各有其特殊的用途,如果一个文件名包括了这些字符( / ? @ # ; $ + = %),这些字符和所有其他字符就应该被编码。

编码过程非常简单,任何字符只要不是ASCII码数字,字母,或者前面提到的标点符,它们都将被转换成字节形式,每个字节都写成这种形式:一个“%”后面跟着两位16进制的数值。空格是一个特殊情况,因为它们太平常了。它除了被编码成“%20”以外,还能编码为一个“+”。加号(+)本身被编码为%2B。当/ # = 和?作为名字的一部分来使用时,而不是作为URL部分之间的分隔符来使用时,它们都应该被编码。

WARNING这种策略在存在大量字符集的异构环境中效果不甚理想。例如:在U.S. Windows 系统中, é 被编码为 %E9. 在 U.S. Mac中被编码为%8E。这种不确定性的存在是现存的URI的一个明显的不足。所以在将来URI的规范当中应该通过国际资源标识符(IRIs)进行改善。

求一个用java编写的可逆的加密算法程序,自己写的小程序也行。

public class mySecurity {

private static KeyGenerator keygen ;

private static SecretKey secretKey;

private static Cipher cipher;

private static mySecurity security = null;

private mySecurity(){

}

public static mySecurity getInstance() throws Exception{

if(security == null){

security = new mySecurity();

keygen = KeyGenerator.getInstance("AES");

secretKey = keygen.generateKey();

cipher =Cipher.getInstance("AES");

}

return security;

}

//加密

public String encrypt(String str) throws Exception{

cipher.init(Cipher.ENCRYPT_MODE,secretKey);

byte [] src = str.getBytes(); byte [] enc = cipher.doFinal(src);

return parseByte2HexStr(enc); }

//解密

public String decrypt(String str) throws Exception{

cipher.init(Cipher.DECRYPT_MODE,secretKey);

byte[] enc = parseHexStr2Byte(str); byte [] dec = cipher.doFinal(enc);

return new String(dec); }

/**将16进制转换为二进制

* @param hexStr

* @return

*/

public static byte[] parseHexStr2Byte(String hexStr) {

if (hexStr.length() 1)

return null;

byte[] result = new byte[hexStr.length()/2];

for (int i = 0;i hexStr.length()/2; i++) {

int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);

int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);

result[i] = (byte) (high * 16 + low);

}

return result;

}

/**将二进制转换成16进制

* @param buf

* @return

*/

public static String parseByte2HexStr(byte buf[]) {

StringBuffer sb = new StringBuffer();

for (int i = 0; i buf.length; i++) {

String hex = Integer.toHexString(buf[i] 0xFF);

if (hex.length() == 1) {

hex = '0' + hex;

}

sb.append(hex.toUpperCase());

}

return sb.toString();

}

public static void main(String[] args) throws Exception{

String str = "abc haha 我";

String ss = mySecurity.getInstance().encrypt(str) ;

System.out.println(ss);

System.out.println(mySecurity.getInstance().decrypt(ss));

}

}

java生成enc的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于JAVA生成随机数、java生成enc的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-21,除非注明,否则均为首码项目网原创文章,转载请注明出处。