java公钥加密_JAVA公钥加密,私钥解密,该怎么解决

展开全部

今天有空研究了下32313133353236313431303231363533e58685e5aeb931333337373665大家都在喊的AES加密!还以为是什么深奥的东西呢!终于了解了,心中释然了!跟大家一起分享下吧!DES其实就是:数据加密标准 英文的缩写!就是个加密的标注而已,AES就是 高级加密标准 英文的缩写咯,大家都叫缩写叫惯了,搞得我们这些没接触的人一头雾水!心里还真憋屈的慌呢!

这是在搜集资料的时候拿了个例子练手,不过有个问题就是,把这代码放到文本里用CMD运行的时候出现了乱码情况!所幸的是注释,不影响效果!但是,程序要真遇到这样的情况的话,就得转码了,因为文本的编码是GBK的,而我所要粘贴的代码的编码是UTF-8

[html] view plaincopy

import java.util.*;

import java.io.*;

public class Test

{

private String encodeResult;//编码后字串

private String decodeResult;//解码后字串

public Test()

{

}

//编码设置

public void setEncodeResult(String encodeResult)

{

char[] src = encodeResult.toCharArray();//将待编码字串拆分成字符数组

StringBuilder sb = new StringBuilder();//保存编码后字符

//将待编码字串拆分成字符数组

for(int i = 0; i< src.length; i++)

{

if(Character.isDigit(src[i]))

{

if(i != src.length-1)

{//满足条件3

char[] temp = new char[Character.getNumericValue(src[i])+1];

Arrays.fill(temp,src[i+1]);

sb.append(temp);

sb.append("_");

}

else

{//满足条件2

sb.append(src[i]);

}

}

else if(src[i] == '_')//满足条件5

{

sb.append("\\UL");

sb.append("_");

}

else if(i == src.length-1)//满足条件1,且到了字串结尾

{

sb.append(src[i]);

}

else//满足条件1,且未到字串结尾

{

sb.append(src[i]);

sb.append("_");

}

}

this.encodeResult = new String(sb);//创建返回编码后字串

}

//获得编码后结果

public String getEncodeResult()

{

return encodeResult;

}

//解码设置

public void setDecodeResult(String encodeResult)

{

String[] temp = encodeResult.split("_");

StringBuilder sb = new StringBuilder();

for(int i = 0; i< temp.length; i++)

{

if(temp[i].equals("\\UL"))

sb.append("_");

else if(temp[i].length()>1)

sb.append(temp[i].length()-1);

else

sb.append(temp[i]);

}

this.decodeResult = new String(sb);

}

//获得解码后结果

public String getDecodeResult()

{

return decodeResult;

}

public static void main(String[] args)

{

System.out.println("请输入待编码字符串(以回车键结束):"); //此处存在一个乱码问题,在文本文档中的编码是GBK而它的编码是UTF-8,cmd不识别!

String source = "";

try

{

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

source = br.readLine();

}

catch (IOException e)

{

e.printStackTrace();

}

Test e = new Test();

e.setEncodeResult(source);

System.out.println("编码后结果:"+e.getEncodeResult());

e.setDecodeResult(e.getEncodeResult());

System.out.println("解码后结果:"+e.getDecodeResult());

}

}

[html] view plaincopy

请输入待编码字符串(以回车键结束):

abcdc123

编码后结果:a_b_c_d_c_22_333_3

解码后结果:abcdc123

【最简单的加密】

1.简单的概念

明文:加密前的信息

密文:机密后的信息

算法:加密或解密的算法

密钥:算法使用的钥匙

例子:

将123456每位数字都加 1 后得到234567,

其中123456就是明文,

234567就是密文,

加密密钥就是1,

加密算法是每位加

[html] view plaincopy

import java.security.SecureRandom;

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;

/**

* 编码工具类

* 1.将byte[]转为各种进制的字符串

* 2.base 64 encode

* 3.base 64 decode

* 4.获取byte[]的md5值

* 5.获取字符串md5值

* 6.结合base64实现md5加密

* 7.AES加密

* 8.AES加密为base 64 code

* 9.AES解密

* 10.将base 64 code AES解密

* @author uikoo9

* @version 0.0.7.20140601

*/

public class Test {

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

String content = "我爱你,祖国";

System.out.println("加密前:" + content);

String key = "123456";

System.out.println("加密密钥和解密密钥:" + key);

String encrypt = aesEncrypt(content, key);

System.out.println("加密后:" + encrypt);

String decrypt = aesDecrypt(encrypt, key);

System.out.println("解密后:" + decrypt);

}

/**

* AES加密为base 64 code

* @param content 待加密的内容

* @param encryptKey 加密密钥

* @return 加密后的base 64 code

* @throws Exception

*/

public static String aesEncrypt(String content, String encryptKey) throws Exception {

return base64Encode(aesEncryptToBytes(content, encryptKey));

}

/**

* AES加密

* @param content 待加密的内容

* @param encryptKey 加密密钥

* @return 加密后的byte[]

* @throws Exception

*/

public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {

KeyGenerator kgen = KeyGenerator.getInstance("AES");

kgen.init(128, new SecureRandom(encryptKey.getBytes()));

Cipher cipher = Cipher.getInstance("AES");

cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));

return cipher.doFinal(content.getBytes("utf-8"));

}

/**

* base 64 encode

* @param bytes 待编码的byte[]

* @return 编码后的base 64 code

*/

public static String base64Encode(byte[] bytes){

return new BASE64Encoder().encode(bytes);

}

/**

* 将base 64 code AES解密

* @param encryptStr 待解密的base 64 code

* @param decryptKey 解密密钥

* @return 解密后的string

* @throws Exception

*/

public static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {

return aesDecryptByBytes(base64Decode(encryptStr), decryptKey);

}

/**

* AES解密

* @param encryptBytes 待解密的byte[]

* @param decryptKey 解密密钥

* @return 解密后的String

* @throws Exception

*/

public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {

KeyGenerator kgen = KeyGenerator.getInstance("AES");

kgen.init(128, new SecureRandom(decryptKey.getBytes()));

Cipher cipher = Cipher.getInstance("AES");

cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));

byte[] decryptBytes = cipher.doFinal(encryptBytes);

return new String(decryptBytes);

}

/**

* base 64 decode

* @param base64Code 待解码的base 64 code

* @return 解码后的byte[]

* @throws Exception

*/

public static byte[] base64Decode(String base64Code) throws Exception{

return new BASE64Decoder().decodeBuffer(base64Code);

}

}

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
公钥加密私钥解密是一种非对称加密算法,常用于保护数据的安全性。在Java,可以使用Java Cryptography Extension(JCE)提供的API实现公钥加密私钥解密。 下面是一个简单的示例代码: ```java import java.security.*; import javax.crypto.*; import java.util.*; public class RSAExample { public static void main(String[] args) throws Exception { // 生成RSA密钥对 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(2048); KeyPair keyPair = keyPairGenerator.generateKeyPair(); // 获取公钥私钥 PublicKey publicKey = keyPair.getPublic(); PrivateKey privateKey = keyPair.getPrivate(); // 使用公钥加密数据 Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] encryptedData = cipher.doFinal("Hello World!".getBytes()); // 使用私钥解密数据 cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] decryptedData = cipher.doFinal(encryptedData); // 输出结果 System.out.println("原始数据:" + "Hello World!"); System.out.println("加密后数据:" + Base64.getEncoder().encodeToString(encryptedData)); System.out.println("解密后数据:" + new String(decryptedData)); } } ``` 在上述代码,首先使用`KeyPairGenerator`生成2048位的RSA密钥对,然后获取公钥私钥。接着,使用公钥进行加密私钥进行解密,最后输出原始数据、加密后的数据和解密后的数据。 需要注意的是,在实际应用公钥通常是公开的,而私钥应该严格保密,不应该被泄露。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值