快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

rsa的java代码,Java rs

给一个java简单随机生成rsa公钥私钥的算法代码

#!/usr/bin/perl -w

成都创新互联专注于下城网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供下城营销型网站建设,下城网站制作、下城网页设计、下城网站官网定制、微信小程序开发服务,打造下城网络公司原创品牌,更为您提供下城网站排名全网营销落地服务。

#RSA 计算过程学习程序编写的测试程序

#watercloud 2003-8-12

#

use strict;

use Math::BigInt;

my %RSA_CORE = (n=2773,e=63,d=847); #p=47,q=59

my $N=new Math::BigInt($RSA_CORE{n});

my $E=new Math::BigInt($RSA_CORE{e});

my $D=new Math::BigInt($RSA_CORE{d});

print "N=$N D=$D E=$E\n";

sub RSA_ENCRYPT

{

my $r_mess = shift @_;

my ($c,$i,$M,$C,$cmess);

for($i=0;$i length($$r_mess);$i++)

{

$c=ord(substr($$r_mess,$i,1));

$M=Math::BigInt-new($c);

$C=$M-copy(); $C-bmodpow($D,$N);

$c=sprintf "%03X",$C;

$cmess.=$c;

}

return \$cmess;

}

sub RSA_DECRYPT

{

my $r_mess = shift @_;

my ($c,$i,$M,$C,$dmess);

for($i=0;$i length($$r_mess);$i+=3)

{

$c=substr($$r_mess,$i,3);

$c=hex($c);

$M=Math::BigInt-new($c);

$C=$M-copy(); $C-bmodpow($E,$N);

$c=chr($C);

$dmess.=$c;

}

return \$dmess;

}

my $mess="RSA 娃哈哈哈~~~";

$mess=$ARGV[0] if @ARGV = 1;

print "原始串:",$mess,"\n";

my $r_cmess = RSA_ENCRYPT(\$mess);

print "加密串:",$$r_cmess,"\n";

my $r_dmess = RSA_DECRYPT($r_cmess);

print "解密串:",$$r_dmess,"\n";

#EOF

Java 第三方公钥 RSA加密求助

下面是RSA加密代码。

/**

* RSA算法,实现数据的加密解密。

* @author ShaoJiang

*

*/

public class RSAUtil {

private static Cipher cipher;

static{

try {

cipher = Cipher.getInstance("RSA");

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

} catch (NoSuchPaddingException e) {

e.printStackTrace();

}

}

/**

* 生成密钥对

* @param filePath 生成密钥的路径

* @return

*/

public static MapString,String generateKeyPair(String filePath){

try {

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

// 密钥位数

keyPairGen.initialize(1024);

// 密钥对

KeyPair keyPair = keyPairGen.generateKeyPair();

// 公钥

PublicKey publicKey = (RSAPublicKey) keyPair.getPublic();

// 私钥

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

//得到公钥字符串

String publicKeyString = getKeyString(publicKey);

//得到私钥字符串

String privateKeyString = getKeyString(privateKey);

//将密钥对写入到文件 想学习更多加我q u N 前面是二五七 中间是014后面是001

FileWriter pubfw = new FileWriter(filePath+"/publicKey.keystore");

FileWriter prifw = new FileWriter(filePath+"/privateKey.keystore");

BufferedWriter pubbw = new BufferedWriter(pubfw);

BufferedWriter pribw = new BufferedWriter(prifw);

pubbw.write(publicKeyString);

pribw.write(privateKeyString);

pubbw.flush();

pubbw.close();

pubfw.close();

pribw.flush();

pribw.close();

prifw.close();

//将生成的密钥对返回

MapString,String map = new HashMapString,String();

map.put("publicKey",publicKeyString);

map.put("privateKey",privateKeyString);

return map;

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

/**

* 得到公钥

*

* @param key

* 密钥字符串(经过base64编码)

* @throws Exception

*/

public static PublicKey getPublicKey(String key) throws Exception {

byte[] keyBytes;

keyBytes = (new BASE64Decoder()).decodeBuffer(key);

X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);

KeyFactory keyFactory = KeyFactory.getInstance("RSA");

PublicKey publicKey = keyFactory.generatePublic(keySpec);

return publicKey;

}

/**

* 得到私钥

*

* @param key

* 密钥字符串(经过base64编码)

* @throws Exception

*/

public static PrivateKey getPrivateKey(String key) throws Exception {

byte[] keyBytes;

keyBytes = (new BASE64Decoder()).decodeBuffer(key);

PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);

KeyFactory keyFactory = KeyFactory.getInstance("RSA");

PrivateKey privateKey = keyFactory.generatePrivate(keySpec);

return privateKey;

}

/**

* 得到密钥字符串(经过base64编码)

*

* @return

*/

public static String getKeyString(Key key) throws Exception {

byte[] keyBytes = key.getEncoded();

String s = (new BASE64Encoder()).encode(keyBytes);

return s;

}

/**

* 使用公钥对明文进行加密,返回BASE64编码的字符串

* @param publicKey

* @param plainText

* @return

*/

public static String encrypt(PublicKey publicKey,String plainText){

try {

cipher.init(Cipher.ENCRYPT_MODE, publicKey);

byte[] enBytes = cipher.doFinal(plainText.getBytes());

return (new BASE64Encoder()).encode(enBytes);

} catch (InvalidKeyException e) {

e.printStackTrace();

} catch (IllegalBlockSizeException e) {

e.printStackTrace();

} catch (BadPaddingException e) {

e.printStackTrace();

}

return null;

}

/**

* 使用keystore对明文进行加密

* @param publicKeystore 公钥文件路径

* @param plainText 明文

* @return

*/

public static String encrypt(String publicKeystore,String plainText){

try {

FileReader fr = new FileReader(publicKeystore);

BufferedReader br = new BufferedReader(fr);

String publicKeyString="";

String str;

while((str=br.readLine())!=null){

publicKeyString+=str;

}

br.close();

fr.close();

cipher.init(Cipher.ENCRYPT_MODE,getPublicKey(publicKeyString));

byte[] enBytes = cipher.doFinal(plainText.getBytes());

return (new BASE64Encoder()).encode(enBytes);

} catch (InvalidKeyException e) {

e.printStackTrace();

} catch (IllegalBlockSizeException e) {

e.printStackTrace();

} catch (BadPaddingException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

/**

* 使用私钥对明文密文进行解密

* @param privateKey

* @param enStr

* @return

*/

public static String decrypt(PrivateKey privateKey,String enStr){

try {

cipher.init(Cipher.DECRYPT_MODE, privateKey);

byte[] deBytes = cipher.doFinal((new BASE64Decoder()).decodeBuffer(enStr));

return new String(deBytes);

} catch (InvalidKeyException e) {

e.printStackTrace();

} catch (IllegalBlockSizeException e) {

e.printStackTrace();

} catch (BadPaddingException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return null;

}

/**

* 使用keystore对密文进行解密

* @param privateKeystore 私钥路径

* @param enStr 密文

* @return

*/

public static String decrypt(String privateKeystore,String enStr){

try {

FileReader fr = new FileReader(privateKeystore);

BufferedReader br = new BufferedReader(fr);

String privateKeyString="";

String str;

while((str=br.readLine())!=null){

privateKeyString+=str;

}

br.close();

fr.close();

cipher.init(Cipher.DECRYPT_MODE, getPrivateKey(privateKeyString));

byte[] deBytes = cipher.doFinal((new BASE64Decoder()).decodeBuffer(enStr));

return new String(deBytes);

} catch (InvalidKeyException e) {

e.printStackTrace();

} catch (IllegalBlockSizeException e) {

e.printStackTrace();

} catch (BadPaddingException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

}

求RSA算法JAVA实现源代码(带界面的)

import javax.crypto.Cipher;

import java.security.*;

import java.security.spec.RSAPublicKeySpec;

import java.security.spec.RSAPrivateKeySpec;

import java.security.spec.InvalidKeySpecException;

import java.security.interfaces.RSAPrivateKey;

import java.security.interfaces.RSAPublicKey;

import java.io.*;

import java.math.BigInteger;

/**

* RSA 工具类。提供加密,解密,生成密钥对等方法。

* 需要到下载bcprov-jdk14-123.jar。

* @author xiaoyusong

* mail: xiaoyusong@etang.com

* msn:xiao_yu_song@hotmail.com

* @since 2004-5-20

*

*/

public class RSAUtil {

/**

* 生成密钥对

* @return KeyPair

* @throws EncryptException

*/

public static KeyPair generateKeyPair() throws EncryptException {

try {

KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA",

new org.bouncycastle.jce.provider.BouncyCastleProvider());

final int KEY_SIZE = 1024;//没什么好说的了,这个值关系到块加密的大小,可以更改,但是不要太大,否则效率会低

keyPairGen.initialize(KEY_SIZE, new SecureRandom());

KeyPair keyPair = keyPairGen.genKeyPair();

return keyPair;

} catch (Exception e) {

throw new EncryptException(e.getMessage());

}

}

/**

* 生成公钥

* @param modulus

* @param publicExponent

* @return RSAPublicKey

* @throws EncryptException

*/

public static RSAPublicKey generateRSAPublicKey(byte[] modulus, byte[] publicExponent) throws EncryptException {

KeyFactory keyFac = null;

try {

keyFac = KeyFactory.getInstance("RSA", new org.bouncycastle.jce.provider.BouncyCastleProvider());

} catch (NoSuchAlgorithmException ex) {

throw new EncryptException(ex.getMessage());

}

RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(modulus), new BigInteger(publicExponent));

try {

return (RSAPublicKey) keyFac.generatePublic(pubKeySpec);

} catch (InvalidKeySpecException ex) {

throw new EncryptException(ex.getMessage());

}

}

/**

* 生成私钥

* @param modulus

* @param privateExponent

* @return RSAPrivateKey

* @throws EncryptException

*/

public static RSAPrivateKey generateRSAPrivateKey(byte[] modulus, byte[] privateExponent) throws EncryptException {

KeyFactory keyFac = null;

try {

keyFac = KeyFactory.getInstance("RSA", new org.bouncycastle.jce.provider.BouncyCastleProvider());

} catch (NoSuchAlgorithmException ex) {

throw new EncryptException(ex.getMessage());

}

RSAPrivateKeySpec priKeySpec = new RSAPrivateKeySpec(new BigInteger(modulus), new BigInteger(privateExponent));

try {

return (RSAPrivateKey) keyFac.generatePrivate(priKeySpec);

} catch (InvalidKeySpecException ex) {

throw new EncryptException(ex.getMessage());

}

}

/**

* 加密

* @param key 加密的密钥

* @param data 待加密的明文数据

* @return 加密后的数据

* @throws EncryptException

*/

public static byte[] encrypt(Key key, byte[] data) throws EncryptException {

try {

Cipher cipher = Cipher.getInstance("RSA", new org.bouncycastle.jce.provider.BouncyCastleProvider());

cipher.init(Cipher.ENCRYPT_MODE, key);

int blockSize = cipher.getBlockSize();//获得加密块大小,如:加密前数据为128个byte,而key_size=1024 加密块大小为127 byte,加密后为128个byte;因此共有2个加密块,第一个127 byte第二个为1个byte

int outputSize = cipher.getOutputSize(data.length);//获得加密块加密后块大小

int leavedSize = data.length % blockSize;

int blocksSize = leavedSize != 0 ? data.length / blockSize + 1 : data.length / blockSize;

byte[] raw = new byte[outputSize * blocksSize];

int i = 0;

while (data.length - i * blockSize 0) {

if (data.length - i * blockSize blockSize)

cipher.doFinal(data, i * blockSize, blockSize, raw, i * outputSize);

else

cipher.doFinal(data, i * blockSize, data.length - i * blockSize, raw, i * outputSize);

//这里面doUpdate方法不可用,查看源代码后发现每次doUpdate后并没有什么实际动作除了把byte[]放到ByteArrayOutputStream中,而最后doFinal的时候才将所有的byte[]进行加密,可是到了此时加密块大小很可能已经超出了OutputSize所以只好用dofinal方法。

i++;

}

return raw;

} catch (Exception e) {

throw new EncryptException(e.getMessage());

}

}

/**

* 解密

* @param key 解密的密钥

* @param raw 已经加密的数据

* @return 解密后的明文

* @throws EncryptException

*/

public static byte[] decrypt(Key key, byte[] raw) throws EncryptException {

try {

Cipher cipher = Cipher.getInstance("RSA", new org.bouncycastle.jce.provider.BouncyCastleProvider());

cipher.init(cipher.DECRYPT_MODE, key);

int blockSize = cipher.getBlockSize();

ByteArrayOutputStream bout = new ByteArrayOutputStream(64);

int j = 0;

while (raw.length - j * blockSize 0) {

bout.write(cipher.doFinal(raw, j * blockSize, blockSize));

j++;

}

return bout.toByteArray();

} catch (Exception e) {

throw new EncryptException(e.getMessage());

}

}

/**

*

* @param args

* @throws Exception

*/

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

File file = new File("test.html");

FileInputStream in = new FileInputStream(file);

ByteArrayOutputStream bout = new ByteArrayOutputStream();

byte[] tmpbuf = new byte[1024];

int count = 0;

while ((count = in.read(tmpbuf)) != -1) {

bout.write(tmpbuf, 0, count);

tmpbuf = new byte[1024];

}

in.close();

byte[] orgData = bout.toByteArray();

KeyPair keyPair = RSAUtil.generateKeyPair();

RSAPublicKey pubKey = (RSAPublicKey) keyPair.getPublic();

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

byte[] pubModBytes = pubKey.getModulus().toByteArray();

byte[] pubPubExpBytes = pubKey.getPublicExponent().toByteArray();

byte[] priModBytes = priKey.getModulus().toByteArray();

byte[] priPriExpBytes = priKey.getPrivateExponent().toByteArray();

RSAPublicKey recoveryPubKey = RSAUtil.generateRSAPublicKey(pubModBytes,pubPubExpBytes);

RSAPrivateKey recoveryPriKey = RSAUtil.generateRSAPrivateKey(priModBytes,priPriExpBytes);

byte[] raw = RSAUtil.encrypt(priKey, orgData);

file = new File("encrypt_result.dat");

OutputStream out = new FileOutputStream(file);

out.write(raw);

out.close();

byte[] data = RSAUtil.decrypt(recoveryPubKey, raw);

file = new File("decrypt_result.html");

out = new FileOutputStream(file);

out.write(data);

out.flush();

out.close();

}

}

这个行吧

再参考这个吧


网站题目:rsa的java代码,Java rs
当前URL:http://6mz.cn/article/hdphoi.html

其他资讯