大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

rsa加密的java代码,js rsa加密 java解密

有一段用java实现rsa加解密的程序看不懂,希望高手帮我做下注释,详细些,谢谢

//引入文件

为东宝等地区用户提供了全套网页设计制作服务,及东宝网站建设行业解决方案。主营业务为做网站、成都网站制作、东宝网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

import java.security.*;

import javax.crypto.*;

/**

* RSACryptography

* RSACryptography use the privated key to encrypt the plain text and decrypt

* the cipher text with the public key

*/

public class RSACryptography {

Cipher cipher;

/**

构造函数,就是你每次new这个对象RSACryptography 时候就会执行里面的方法

返回一个Cipher对象(其实他就是用来加密解密的)

*/

public RSACryptography() {

try {

cipher = Cipher.getInstance("RSA");//返回一个cipher对象,该类

//应该是单例的

} catch (NoSuchAlgorithmException e) {//抛出异常,没什么说的

e.printStackTrace();

} catch (NoSuchPaddingException e) {

e.printStackTrace();

}

}

/**

好了,重点来了,你需要加密解密的就调用这个方法encrypt_decrypt(),传入一个byte[]的类型值byteInput,,就是你要加密的东西,在传入一个key,这个key 就像钥匙一样,你根据这个key进行加密,也可以根据这个key进行解密的,boolean 类型的 crypto,如果true就是加密,false就是解密

*/

public byte[] encrypt_decrypt(byte[] byteInput, Key key, boolean crypto) {

try {

if(crypto){

cipher.init(Cipher.ENCRYPT_MODE,key);//加密前初始化

}else{

cipher.init(Cipher.DECRYPT_MODE,key);//解密前初始化

}

byte[] cipherByte = cipher.doFinal(byteInput);//进行加密或解密

return cipherByte;//返回你的加密或者解密值类型为byte[]

} catch (InvalidKeyException e) {//抛出异常

e.printStackTrace();

} catch (IllegalBlockSizeException e) {

e.printStackTrace();

} catch (BadPaddingException e) {

e.printStackTrace();

}

return null;

}

}

给一个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加密算法

代码如下:main方法用于测试的,不是算法本身。

import java.security.KeyPair;

import java.security.KeyPairGenerator;

import java.security.PrivateKey;

import java.security.PublicKey;

import java.security.SecureRandom;

import javax.crypto.Cipher;

public class RSACrypto

{

private final static String RSA = "RSA";

public static PublicKey uk;

public static PrivateKey rk;

public static void generateKey() throws Exception

{

KeyPairGenerator gen = KeyPairGenerator.getInstance(RSA);

gen.initialize(512, new SecureRandom());

KeyPair keyPair = gen.generateKeyPair();

uk = keyPair.getPublic();

rk = keyPair.getPrivate();

}

private static byte[] encrypt(String text, PublicKey pubRSA) throws Exception

{

Cipher cipher = Cipher.getInstance(RSA);

cipher.init(Cipher.ENCRYPT_MODE, pubRSA);

return cipher.doFinal(text.getBytes());

}

public final static String encrypt(String text)

{

try {

return byte2hex(encrypt(text, uk));

}

catch(Exception e)

{

e.printStackTrace();

}

return null;

}

public final static String decrypt(String data)

{

try{

return new String(decrypt(hex2byte(data.getBytes())));

}

catch (Exception e)

{

e.printStackTrace();

}

return null;

}

private static byte[] decrypt(byte[] src) throws Exception

{

Cipher cipher = Cipher.getInstance(RSA);

cipher.init(Cipher.DECRYPT_MODE, rk);

return cipher.doFinal(src);

}

public static String byte2hex(byte[] b)

{

String hs = "";

String stmp = "";

for (int n = 0; n b.length; n ++)

{

stmp = Integer.toHexString(b[n] 0xFF);

if (stmp.length() == 1)

hs += ("0" + stmp);

else

hs += stmp;

}

return hs.toUpperCase();

}

public static byte[] hex2byte(byte[] b)

{

if ((b.length % 2) != 0)

throw new IllegalArgumentException("长度不是偶数");

byte[] b2 = new byte[b.length / 2];

for (int n = 0; n b.length; n += 2)

{

String item = new String(b, n, 2);

b2[n/2] = (byte)Integer.parseInt(item, 16);

}

return b2;

}

//just for test

public static void main(String args[])

{

try

{

RSACrypto.generateKey();

String cipherText = RSACrypto.encrypt("asdfghjh");

System.out.println(cipherText);

String plainText = RSACrypto.decrypt(cipherText);

System.out.println(plainText);

}

catch(Exception e)

{

e.printStackTrace();

}

}

}


当前名称:rsa加密的java代码,js rsa加密 java解密
文章起源:http://dzwzjz.com/article/dsispop.html
在线咨询
服务热线
服务热线:028-86922220
TOP