「java密钥合成」java rsa密钥生成

博主:adminadmin 2022-12-01 15:12:06 131

本篇文章给大家谈谈java密钥合成,以及java rsa密钥生成对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

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 如果随机生成秘钥对并获取

好像是,..这个你可以将公钥私钥放到一个MAP中 在这个MAP中初始化随即产生器然后生成密钥对 我也刚接触 还没看懂... //生成密钥

public static MapString,Object initKey(String seed) throws Exception{

KeyPairGenerator keygen = KeyPairGenerator.getInstance(ALGORITHM);

//初始化随机产生器

SecureRandom sr = new SecureRandom();

sr.setSeed(seed.getBytes());

keygen.initialize(KEY_SIZE,sr);

KeyPair keys = keygen.genKeyPair();

DSAPublicKey publicKey = (DSAPublicKey) keys.getPublic();

DSAPrivateKey privateKey = (DSAPrivateKey) keys.getPrivate();

MapString,Object map = new HashMapString,Object(2);

map.put(PUBLIC_KEY, publicKey);

map.put(PRIVATE_KEY, privateKey);

return map;

}

如何用java实现128位密钥的RSA算法

 import javax.crypto.Cipher;

 import sun.misc.BASE64Decoder;

 import sun.misc.BASE64Encoder;

 import java.io.FileInputStream;

 import java.io.FileOutputStream;

 import java.io.ObjectInputStream;

 import java.io.ObjectOutputStream;

 import java.security.Key;

 import java.security.KeyPair;

 import java.security.KeyPairGenerator;

import java.security.SecureRandom;

 public class RSA_Encrypt {

 /** 指定加密算法为DESede */

 private static String ALGORITHM = "RSA";

 /** 指定key的大小 */

 private static int KEYSIZE = 128;

 /** 指定公钥存放文件 */

 private static String PUBLIC_KEY_FILE = "PublicKey";

 /** 指定私钥存放文件 */

 private static String PRIVATE_KEY_FILE = "PrivateKey";

// private static String PUBLIC_KEY_FILE = "D://PublicKey.a";

// private static String PRIVATE_KEY_FILE = "D://PrivateKey.a";

 

 

 /**

 * 生成密钥对

 */

 private static void generateKeyPair() throws Exception{

   /** RSA算法要求有一个可信任的随机数源 */

    SecureRandom sr = new SecureRandom();

    /** 为RSA算法创建一个KeyPairGenerator对象 */

    KeyPairGenerator kpg = KeyPairGenerator.getInstance(ALGORITHM);

   /** 利用上面的随机数据源初始化这个KeyPairGenerator对象 */

    kpg.initialize(KEYSIZE, sr);

    /** 生成密匙对 */

    KeyPair kp = kpg.generateKeyPair();

    /** 得到公钥 */

    Key publicKey = kp.getPublic();

    /** 得到私钥 */

    Key privateKey = kp.getPrivate();

    /** 用对象流将生成的密钥写入文件 */

    ObjectOutputStream oos1 = new ObjectOutputStream(new FileOutputStream(PUBLIC_KEY_FILE));

    ObjectOutputStream oos2 = new ObjectOutputStream(new FileOutputStream(PRIVATE_KEY_FILE));

    oos1.writeObject(publicKey);

    oos2.writeObject(privateKey);

    /** 清空缓存,关闭文件输出流 */

    oos1.close();

    oos2.close();

 }

 /**

 * 加密方法

 * source: 源数据

 */

 public static String encrypt(String source) throws Exception{

    generateKeyPair();

    /** 将文件中的公钥对象读出 */

    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(PUBLIC_KEY_FILE));

    Key key = (Key) ois.readObject();

    ois.close();

    /** 得到Cipher对象来实现对源数据的RSA加密 */

    Cipher cipher = Cipher.getInstance(ALGORITHM);

    cipher.init(Cipher.ENCRYPT_MODE, key);

    byte[] b = source.getBytes();

    /** 执行加密操作 */

    byte[] b1 = cipher.doFinal(b);

    BASE64Encoder encoder = new BASE64Encoder();

    return encoder.encode(b1);

 }

 /**

 * 解密算法

 * cryptograph:密文

 */

 public static String decrypt(String cryptograph) throws Exception{

    /** 将文件中的私钥对象读出 */

    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(PRIVATE_KEY_FILE));

    Key key = (Key) ois.readObject();

    /** 得到Cipher对象对已用公钥加密的数据进行RSA解密 */

    Cipher cipher = Cipher.getInstance(ALGORITHM);

    cipher.init(Cipher.DECRYPT_MODE, key);

    BASE64Decoder decoder = new BASE64Decoder();

    byte[] b1 = decoder.decodeBuffer(cryptograph);

    /** 执行解密操作 */

    byte[] b = cipher.doFinal(b1);

    return new String(b);

 }

  

 public static void main(String[] args) {

  try {

   String source = "Hello World!";//要加密的字符串

   String cryptograph = encrypt(source);

   System.out.println(cryptograph);

   

   String target = decrypt(cryptograph);//解密密文

   System.out.println(target);

  } catch (Exception e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }//生成的密文

 }

}

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

The End

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