当前位置:首页 » 编程语言 » javarsa加密

javarsa加密

发布时间: 2024-10-26 11:33:25

① 关于java中rsa的问题

【实例下载】本文介绍RSA2加密与解密,RSA2是RSA的加强版本,在密钥长度上采用2048, RSA2比RSA更安全,更可靠, 本人的另一篇文章RSA已经发表,有想了解的可以点开下面的RSA文章

② java ibm jdk rsa 怎么 加密

android和java webservice RSA处理的不同

1.andorid机器上生成的(密钥对由服务器在windows xp下生成并将公钥发给客户端保存)密码无法在服务器通过私钥解密。

2.为了测试,在服务器本地加解密正常,另外,在android上加解密也正常,但是在服务器中加密(使用相同公钥)后的密码同样无法在android系统解密(使用相同私钥)。
3.由于对RSA加密算法不了解,而且对Java RSA的加密过程也不清楚、谷歌一番,才了解到可能是加密过程中的填充字符长度不同,这跟加解密时指定的RSA算法有关系。
4. 比如,在A机中使用标准RSA通过公钥加密,然后在B系统中使用“RSA/ECB/NoPadding”使用私钥解密,结果可以解密,但是会发现解密后的原文前面带有很多特殊字符,这就是在加密前填充的空字符;如果在B系统中仍然使用标准的RSA算法解密,这在相同类型的JDK虚拟机环境下当然是完全一样的,关键是android系统使用的虚拟机(dalvik)跟SUN标准JDK是有所区别的,其中他们默认的RSA实现就不同。
5.更形象一点,在加密的时候加密的原文“abc”,直接使用“abc”.getBytes()方法获得的bytes长度可能只有3,但是系统却先把它放到一个512位的byte数组里,new byte[512],再进行加密。但是解密的时候使用的是“加密后的密码”.getBytes()来解密,解密后的原文自然就是512长度的数据,即是在“abc”之外另外填充了500多字节的其他空字符。

③ javarsa加密c#解密失败

系统bug。当软件javarsa的系统出现系统bug时,就会导致该软件在解密c井的程序的时候出现解密失败的情况,只需要将该软件卸载后重新安装该软件即可。

④ Java鐢熸垚RSA闱炲圭О鍨嫔姞瀵嗙殑鍏阍ュ拰绉侀挜

銆銆闱炲圭О鍨嫔姞瀵嗛潪甯搁傚悎澶氢釜瀹㈡埛绔鍜屾湇锷″櫒涔嬮棿镄勭桦瘑阃氲 瀹㈡埛绔浣跨敤钖屼竴涓鍏阍ュ皢鏄庢枃锷犲瘑 钥岃繖涓鍏阍ヤ笉鑳介嗗悜镄勮В瀵 瀵嗘枃鍙戦佸埌链嶅姟鍣ㄥ悗链夋湇锷″櫒绔鐢ㄧ侀挜瑙e瘑 杩欐牱灏卞仛鍒颁简鏄庢枃镄勫姞瀵嗕紶阃

銆銆闱炲圭О鍨嫔姞瀵嗕篃链夊畠鍏埚ぉ镄勭己镣 锷犲瘑 瑙e瘑阃熷害鎱㈠埗绾︿简瀹幂殑鍙戞尌 濡傛灉浣犳湁澶ч噺镄勬枃瀛楅渶瑕佸姞瀵嗕紶阃 寤鸿浣犻氲繃闱炲圭О鍨嫔姞瀵嗘潵鎶婂圭О鍨 瀵嗛挜 鍒嗗彂鍒板㈡埛绔 鍙婃椂镟存柊瀵圭О鍨 瀵嗛挜

銆銆import java io *;

銆銆import java security *;

銆銆import javax crypto *;

銆銆import javax crypto spec *;

銆銆/**

銆銆* <p>Title: RSA闱炲圭О鍨嫔姞瀵嗙殑鍏阍ュ拰绉侀挜</p>

銆銆* <p>Description: </p>

銆銆* <p>Copyright: Copyright (c) </p>

銆銆* <p>Company: </p>

銆銆* @author not attributable

銆銆* @version

銆銆*/

銆銆public class KeyRSA {

銆銆private KeyPairGenerator kpg = null;

銆銆private KeyPair kp = null;

銆銆private PublicKey public_key = null;

銆銆private PrivateKey private_key = null;

銆銆private FileOutputStream public_file_out = null;

銆銆private ObjectOutputStream public_object_out = null;

銆銆private FileOutputStream private_file_out = null;

銆銆private ObjectOutputStream private_object_out = null;

銆銆/**

銆銆* 鏋勯犲嚱鏁

銆銆* @param in 鎸囧畾瀵嗗宠闀垮害锛埚彇鍊艰寖锲 锝 锛

銆銆* @throws NoSuchAlgorithmException 寮傚父

銆銆*/

銆銆public KeyRSA(int in String address) throws NoSuchAlgorithmException FileNotFoundException IOException

銆銆{

銆銆kpg = KeyPairGenerator getInstance( RSA ); //鍒涘缓 瀵嗗宠瀵 鐢熸垚鍣

銆銆kpg initialize(in); //鎸囧畾瀵嗗宠闀垮害锛埚彇鍊艰寖锲 锝 锛

銆銆kp = kpg genKeyPair(); //鐢熸垚 瀵嗗宠瀵 鍏朵腑鍖呭惈镌涓涓鍏鍖椤拰涓涓绉佸宠镄勪俊鎭

銆銆public_key = kp getPublic(); //銮峰缑鍏鍖

銆銆private_key = kp getPrivate(); //銮峰缑绉佸宠

銆銆//淇濆瓨鍏鍖

銆銆public_file_out = new FileOutputStream(address + /public_key dat );

銆銆public_object_out = new ObjectOutputStream(public_file_out);

銆銆public_object_out writeObject(public_key);

銆銆//淇濆瓨绉佸宠

銆銆private_file_out = new FileOutputStream(address + /private_key dat );

銆銆private_object_out = new ObjectOutputStream(private_file_out);

銆銆private_object_out writeObject(private_key);

銆銆}

銆銆public static void main(String[] args) {

銆銆try {

銆銆System out println( 绉佸宠鍜屽叕鍖欎缭瀛桦埌C鐩树笅镄勬枃浠朵腑 );

銆銆new KeyRSA( c:/ );

銆銆}

銆銆catch (IOException ex) {

銆銆}

銆銆catch (NoSuchAlgorithmException ex) {

銆銆}

銆銆}

lishixin/Article/program/Java/hx/201311/26592

⑤ c#怎么调用java生成的RSA 公钥进行加密

.NET无法调用JAVA产生的RSA公钥,必须将RSA算法在.NET里面重写才行,在.NET里面RSA的公钥长度是128位的,但是你给出的JAVA公钥却是159位长度,非常的不标准,公钥长度不满足128的肯定无法给.NET使用。

这里最多帮做个对应解析,数据是肯定无法用的:
将java的RSA公钥最后四个字母AQAB分割开,用.NET的xml格式表示就是
<RSAKeyValue><Molus>
/qp
jLFfDCu3qytxf+/IoCYE

+Hf4hsEDUKV2kkhRJsnwwID</Molus><Exponent>AQAB</Exponent>
</RSAKeyValue>
这里的数据都是用的BASE64编码,你用BASE64解码后可以得到byte[],就可以看到密钥长度了,实际密钥要转换为BigInteger后才能参与RSA核心运算

⑥ 我想把java文件先加密然后打包,请高手指教怎么加密,有那种好的加密算法吗

RSA算法非常简单,概述如下:
找两素数p和q
取n=p*q
取t=(p-1)*(q-1)
取任何一个数e,要求满足e<t并且e与t互素(就是最大公因数为1)
取d*e%t==1

这样最终得到三个数: n d e

设消息为数M (M <n)
设c=(M**d)%n就得到了加密后的消息c
设m=(c**e)%n则 m == M,从而完成对c的解密。
注:**表示次方,上面两式中的d和e可以互换。

在对称加密中:
n d两个数构成公钥,可以告诉别人;
n e两个数构成私钥,e自己保留,不让任何人知道。
给别人发送的信息使用e加密,只要别人能用d解开就证明信息是由你发送的,构成了签名机制。
别人给你发送信息时使用d加密,这样只有拥有e的你能够对其解密。

rsa的安全性在于对于一个大数n,没有有效的方法能够将其分解
从而在已知n d的情况下无法获得e;同样在已知n e的情况下无法
求得d。

<二>实践

接下来我们来一个实践,看看实际的操作:
找两个素数:
p=47
q=59
这样
n=p*q=2773
t=(p-1)*(q-1)=2668
取e=63,满足e<t并且e和t互素
用perl简单穷举可以获得满主 e*d%t ==1的数d:
C:\Temp>perl -e "foreach $i (1..9999){ print($i),last if $i*63%2668==1 }"
847
即d=847

最终我们获得关键的
n=2773
d=847
e=63

取消息M=244我们看看

加密:

c=M**d%n = 244**847%2773
用perl的大数计算来算一下:
C:\Temp>perl -Mbigint -e "print 244**847%2773"
465
即用d对M加密后获得加密信息c=465

解密:

我们可以用e来对加密后的c进行解密,还原M:
m=c**e%n=465**63%2773 :
C:\Temp>perl -Mbigint -e "print 465**63%2773"
244
即用e对c解密后获得m=244 , 该值和原始信息M相等。

<三>字符串加密

把上面的过程集成一下我们就能实现一个对字符串加密解密的示例了。
每次取字符串中的一个字符的ascii值作为M进行计算,其输出为加密后16进制
的数的字符串形式,按3字节表示,如01F

代码如下:

#!/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->(); $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->(); $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

测试一下:
C:\Temp>perl rsa-test.pl
N=2773 D=847 E=63
原始串:RSA 娃哈哈哈~~~
加密串:
解密串:RSA 娃哈哈哈~~~

C:\Temp>perl rsa-test.pl 安全焦点(xfocus)
N=2773 D=847 E=63
原始串:安全焦点(xfocus)
加密串:
解密串:安全焦点(xfocus)

<四>提高

前面已经提到,rsa的安全来源于n足够大,我们测试中使用的n是非常小的,根本不能保障安全性,
我们可以通过RSAKit、RSATool之类的工具获得足够大的N 及D E。
通过工具,我们获得1024位的N及D E来测试一下:

n=EC3A85F5005D
4C2013433B383B
A50E114705D7E2
BC511951

d=0x10001

e=DD28C523C2995
47B77324E66AFF2
789BD782A592D2B
1965

设原始信息
M=

完成这么大数字的计算依赖于大数运算库,用perl来运算非常简单:

A) 用d对M进行加密如下:
c=M**d%n :
C:\Temp>perl -Mbigint -e " $x=Math::BigInt->bmodpow(0x11111111111122222222222233
333333333, 0x10001,
D55EDBC4F0
6E37108DD6
);print $x->as_hex"
b73d2576bd
47715caa6b
d59ea89b91
f1834580c3f6d90898

即用d对M加密后信息为:
c=b73d2576bd
47715caa6b
d59ea89b91
f1834580c3f6d90898

B) 用e对c进行解密如下:

m=c**e%n :
C:\Temp>perl -Mbigint -e " $x=Math::BigInt->bmodpow(0x17b287be418c69ecd7c39227ab
5aa1d99ef3
0cb4764414
, 0xE760A
3C29954C5D
7324E66AFF
2789BD782A
592D2B1965, CD15F90
4F017F9CCF
DD60438941
);print $x->as_hex"

(我的P4 1.6G的机器上计算了约5秒钟)

得到用e解密后的m= == M

C) RSA通常的实现
RSA简洁幽雅,但计算速度比较慢,通常加密中并不是直接使用RSA 来对所有的信息进行加密,
最常见的情况是随机产生一个对称加密的密钥,然后使用对称加密算法对信息加密,之后用
RSA对刚才的加密密钥进行加密。

最后需要说明的是,当前小于1024位的N已经被证明是不安全的
自己使用中不要使用小于1024位的RSA,最好使用2048位的。

----------------------------------------------------------

一个简单的RSA算法实现JAVA源代码:

filename:RSA.java

/*
* Created on Mar 3, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/

import java.math.BigInteger;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.StringTokenizer;

/**
* @author Steve
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class RSA {

/**
* BigInteger.ZERO
*/
private static final BigInteger ZERO = BigInteger.ZERO;

/**
* BigInteger.ONE
*/
private static final BigInteger ONE = BigInteger.ONE;

/**
* Pseudo BigInteger.TWO
*/
private static final BigInteger TWO = new BigInteger("2");

private BigInteger myKey;

private BigInteger myMod;

private int blockSize;

public RSA (BigInteger key, BigInteger n, int b) {
myKey = key;
myMod = n;
blockSize = b;
}

public void encodeFile (String filename) {
byte[] bytes = new byte[blockSize / 8 + 1];
byte[] temp;
int tempLen;
InputStream is = null;
FileWriter writer = null;
try {
is = new FileInputStream(filename);
writer = new FileWriter(filename + ".enc");
}
catch (FileNotFoundException e1){
System.out.println("File not found: " + filename);
}
catch (IOException e1){
System.out.println("File not found: " + filename + ".enc");
}

/**
* Write encoded message to 'filename'.enc
*/
try {
while ((tempLen = is.read(bytes, 1, blockSize / 8)) > 0) {
for (int i = tempLen + 1; i < bytes.length; ++i) {
bytes[i] = 0;
}
writer.write(encodeDecode(new BigInteger(bytes)) + " ");
}
}
catch (IOException e1) {
System.out.println("error writing to file");
}

/**
* Close input stream and file writer
*/
try {
is.close();
writer.close();
}
catch (IOException e1) {
System.out.println("Error closing file.");
}
}

public void decodeFile (String filename) {

FileReader reader = null;
OutputStream os = null;
try {
reader = new FileReader(filename);
os = new FileOutputStream(filename.replaceAll(".enc", ".dec"));
}
catch (FileNotFoundException e1) {
if (reader == null)
System.out.println("File not found: " + filename);
else
System.out.println("File not found: " + filename.replaceAll(".enc", "dec"));
}

BufferedReader br = new BufferedReader(reader);
int offset;
byte[] temp, toFile;
StringTokenizer st = null;
try {
while (br.ready()) {
st = new StringTokenizer(br.readLine());
while (st.hasMoreTokens()){
toFile = encodeDecode(new BigInteger(st.nextToken())).toByteArray();
System.out.println(toFile.length + " x " + (blockSize / 8));

if (toFile[0] == 0 && toFile.length != (blockSize / 8)) {
temp = new byte[blockSize / 8];
offset = temp.length - toFile.length;
for (int i = toFile.length - 1; (i <= 0) && ((i + offset) <= 0); --i) {
temp[i + offset] = toFile[i];
}
toFile = temp;
}

/*if (toFile.length != ((blockSize / 8) + 1)){
temp = new byte[(blockSize / 8) + 1];
System.out.println(toFile.length + " x " + temp.length);
for (int i = 1; i < temp.length; i++) {
temp[i] = toFile[i - 1];
}
toFile = temp;
}
else
System.out.println(toFile.length + " " + ((blockSize / 8) + 1));*/
os.write(toFile);
}
}
}
catch (IOException e1) {
System.out.println("Something went wrong");
}

/**
* close data streams
*/
try {
os.close();
reader.close();
}
catch (IOException e1) {
System.out.println("Error closing file.");
}
}

/**
* Performs <tt>base</tt>^<sup><tt>pow</tt></sup> within the molar
* domain of <tt>mod</tt>.
*
* @param base the base to be raised
* @param pow the power to which the base will be raisded
* @param mod the molar domain over which to perform this operation
* @return <tt>base</tt>^<sup><tt>pow</tt></sup> within the molar
* domain of <tt>mod</tt>.
*/
public BigInteger encodeDecode(BigInteger base) {
BigInteger a = ONE;
BigInteger s = base;
BigInteger n = myKey;

while (!n.equals(ZERO)) {
if(!n.mod(TWO).equals(ZERO))
a = a.multiply(s).mod(myMod);

s = s.pow(2).mod(myMod);
n = n.divide(TWO);
}

return a;
}

}

在这里提供两个版本的RSA算法JAVA实现的代码下载:

1. 来自于 http://www.javafr.com/code.aspx?ID=27020 的RSA算法实现源代码包:
http://zeal.newmenbase.net/attachment/JavaFR_RSA_Source.rar

2. 来自于 http://www.ferrara.linux.it/Members/lucabariani/RSA/implementazioneRsa/ 的实现:
http://zeal.newmenbase.net/attachment/sorgentiJava.tar.gz - 源代码包
http://zeal.newmenbase.net/attachment/algoritmoRSA.jar - 编译好的jar包

另外关于RSA算法的php实现请参见文章:
php下的RSA算法实现

关于使用VB实现RSA算法的源代码下载(此程序采用了psc1算法来实现快速的RSA加密):
http://zeal.newmenbase.net/attachment/vb_PSC1_RSA.rar

RSA加密的JavaScript实现: http://www.ohdave.com/rsa/
参考资料:http://www.lenovonet.com/proct/showarticle.asp?id=118

热点内容
查看linux所有用户 发布:2024-10-26 14:11:15 浏览:15
useraddftp 发布:2024-10-26 13:59:21 浏览:621
安卓免费私密相册哪个好用 发布:2024-10-26 13:41:20 浏览:639
湛江移动服务器ip 发布:2024-10-26 13:12:17 浏览:634
找回dns服务器地址 发布:2024-10-26 12:48:23 浏览:240
王者皮肤系统脚本 发布:2024-10-26 12:46:44 浏览:508
电脑配置8g怎么查看 发布:2024-10-26 12:40:58 浏览:811
布朗编程 发布:2024-10-26 12:35:27 浏览:307
php平滑重启 发布:2024-10-26 12:25:33 浏览:947
竹压缩板材 发布:2024-10-26 12:21:32 浏览:754