Advertisement

java的rsa加密_Java实现RSA加密算法

阅读量:

RSA是一种非对称加密算法,一般采用公钥加密,私钥解密的形式

生成密钥对

为了生成公钥和私钥对,在本系统中将使用KeyPairGenerator类的一个实例,并基于RSA算法来生成对象。

KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");

在初始化过程中,在确保安全性的同时,在初始化过程中,在确保安全性的同时

keyPairGen.initialize(1024);

通过调用keyPairsObj来创建密钥对,并将其存储于名为keyPairs的类中的变量中

在keyPair对象内取得公钥与私键RSAPublicKeypublic-key = $(RSAPUBLICKEY) keyPairs.getPublic();

RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();

该函数用于编码(encode)输入的数据

if (publicKey != null) {

try {

// Cipher负责完成加密或解密工作,基于RSA

Cipher cipher = Cipher.getInstance(ALGORITHM);

// 根据公钥,对Cipher对象进行初始化

cipher.init(Cipher.ENCRYPT_MODE, publicKey);

// 加密,结果保存进resultBytes,并返回

byte[] resultBytes = cipher.doFinal(srcBytes);

return resultBytes;

} catch (Exception e) {

e.printStackTrace();

}

}

return null;

}

解码(decrypt)接受一个RSAPublicKey publicKey和一个字节数组srcBytes,并返回一个加密的byte数组

if (publicKey != null) {

try {

// Cipher负责完成加密或解密工作,基于RSA

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

// 根据公钥,对Cipher对象进行初始化

cipher.init(Cipher.ENCRYPT_MODE, publicKey);

// 加密,结果保存进resultBytes,并返回

byte[] resultBytes = cipher.doFinal(srcBytes);

return resultBytes;

} catch (Exception e) {

e.printStackTrace();

}

}

return null;

}

完整代码import java.security.*;

import java.security.interfaces.*;

import javax.crypto.*;

public class RSA {

private RSAPublicKey publicKey;

private RSAPrivateKey privateKey;

private byte[] resultBytes;

private final static String ALGORITHM = "RSA";

public RSA() {

try {

String message = "你好,我很喜欢加密算法";

System.out.println("明文是:" + message);

// 生成公钥和私钥对,基于RSA算法生成对象

KeyPairGeneratorInterface keyPairGen = KeyPairGenerator.getInstance(RSAAlgorithm);

// 初始化密钥对生成器,密钥大小为1024位

keyPairGen.initialize(1024);

// 生成一个密钥对,保存在keyPair中

KeyPair keyPair = keyPairGen.generateKeyPair();

// 得到公钥和私钥

publicKey = (RSAPublicKey) keyPair.getPublic();

privateKey = (RSAPrivateKey) keyPair.getPrivate();

// 用公钥加密

byte[] srcBytes = message.getBytes();

resultBytes = RSA.encrypt(publicKey, srcBytes);

String result = new String(resultBytes);

System.out.println("用公钥加密后密文是:" + result);

} catch (Exception e) {

e.printStackTrace();

}

}

protected static byte[] encrypt(RSAPublicKey publicKey, byte[] srcBytes) {

if (publicKey != null) {

try {

// Cipher负责完成加密或解密工作,基于RSA

Cipher cipher = Cipher.getInstance(ALGORITHM);

// 根据公钥,对Cipher对象进行初始化

cipher.init(Cipher.ENCRYPT_MODE, publicKey);

// 加密,结果保存进resultBytes,并返回

byte[] resultBytes = cipher.doFinal(srcBytes);

return resultBytes;

} catch (Exception e) {

e.printStackTrace();

}

}

return null;

}

protected byte[] decrypt(RSAPrivateKey privateKey, byte[] encBytes) {

if (privateKey != null) {

try {

Cipher cipher = Cipher.getInstance(ALGORITHM);

// 根据私钥对Cipher对象进行初始化

cipher.init(Cipher.DECRYPT_MODE, privateKey);

// 解密并将结果保存进resultBytes

byte[] decBytes = cipher.doFinal(encBytes);

return decBytes;

} catch (Exception e) {

e.printStackTrace();

}

}

return null;

}

public static void main(String[] args) {

long rsaStart = System.currentTimeMillis();

RSA rsa = new RSA();

byte[] decBytes = rsa.decrypt(rsa.privateKey, rsa.resultBytes);

String dec = new String(decBytes);

long rsaEnd = System.currentTimeMillis();

System.out.println("用私钥解密后的结果是:" + dec);

System.out.println("共计用时:" + (rsaEnd - rsaStart));

}

}

运行结果见下面的GIF:

9bbc1942bab13874796549a601d517bc.gif

全部评论 (0)

还没有任何评论哟~