一般加密原理是,由用户共有的公共密钥和传输数据的用户直接有的私有密钥组成。每次加密的时候,都是由一定算法随机生成一对密钥,用密钥加密所需加密的文件,用公钥加密密钥,然后将这些一起传输给目标用户,目标用户将数据处理后,又按照此密钥加密,然后传输回去。
这样的优点别人很难破解,网络上的数据无法定位,而且产生的密钥是随机的。
接下来这个例子就是java核心技术卷2高级特性安全性一章的最后一个例子。
import
java.io.
*
;
import
java.security.
*
;
import
javax.crypto.
*
;
import
javax.crypto.interfaces.
*
;
import
javax.crypto.spec.
*
;
public
class
RSATest
...
{
private
static
final
int
KEYSIZE
=
512
;
/** */
/**
*
@param
args
*/
public
static
void
main(String[] args)
...
{
//
TODO Auto-generated method stub
try
...
{
if
(args[
0
].equals(
"
-genkey
"
))
...
{
KeyPairGenerator keyPair
=
KeyPairGenerator.getInstance(
"
RSA
"
);
SecureRandom random
=
new
SecureRandom();
keyPair.initialize(KEYSIZE, random);
KeyPair keyP
=
keyPair.generateKeyPair();
ObjectOutputStream oos
=
new
ObjectOutputStream(
new
FileOutputStream(args[
1
]));
oos.writeObject(keyP.getPublic());
oos.close();
oos
=
new
ObjectOutputStream(
new
FileOutputStream(args[
2
]));
oos.writeObject(keyP.getPrivate());
oos.close();
}
else
if
(args[
0
].equals(
"
-encrypt
"
))
...
{
KeyGenerator keygen
=
KeyGenerator.getInstance(
"
AES
"
);
SecureRandom random
=
new
SecureRandom();
keygen.init(random);
SecretKey key
=
keygen.generateKey();
ObjectInputStream kin
=
new
ObjectInputStream(
new
FileInputStream(args[
3
]));
Key publicKey
=
(Key)kin.readObject();
kin.close();
Cipher cipher
=
Cipher.getInstance(
"
RSA
"
);
System.out.println(cipher.getAlgorithm());
cipher.init(Cipher.WRAP_MODE,publicKey);
byte
[] wrappedKey
=
cipher.wrap(key);
DataOutputStream out
=
new
DataOutputStream(
new
FileOutputStream(args[
2
]));
out.writeInt(wrappedKey.length);
out.write(wrappedKey);
InputStream in
=
new
FileInputStream(args[
1
]);
cipher
=
Cipher.getInstance(
"
AES
"
);
cipher.init(Cipher.ENCRYPT_MODE, key);
crypt(in,out,cipher);
in.close();
out.close();
}
else
...
{
DataInputStream in
=
new
DataInputStream(
new
FileInputStream(args[
1
]));
int
length
=
in.readInt();
byte
[]wrappedKey
=
new
byte
[length];
in.read(wrappedKey,
0
,length);
ObjectInputStream keyIn
=
new
ObjectInputStream(
new
FileInputStream(args[
3
]));
Key privateKey
=
(Key) keyIn.readObject();
keyIn.close();
Cipher cipher
=
Cipher.getInstance(
"
RSA
"
);
cipher.init(Cipher.UNWRAP_MODE, privateKey);
Key key
=
cipher.unwrap(wrappedKey,
"
AES
"
, Cipher.SECRET_KEY);
OutputStream out
=
new
FileOutputStream(args[
2
]);
cipher
=
Cipher.getInstance(
"
AES
"
);
cipher.init(Cipher.ENCRYPT_MODE, key);
crypt(in,out,cipher);
in.close();
out.close();
}
}
catch
(Exception e)
...
{
e.printStackTrace();
}
}
private
static
void
crypt(InputStream in, OutputStream out, Cipher cipher)
throws
IOException,GeneralSecurityException
...
{
//
TODO Auto-generated method stub
int
blockSize
=
cipher.getBlockSize();
int
outputSize
=
cipher.getOutputSize(blockSize);
byte
[] inBytes
=
new
byte
[blockSize];
byte
[] outputBytes
=
new
byte
[outputSize];
int
inLength
=
0
;
boolean
more
=
true
;
while
(more)
...
{
inLength
=
in.read(inBytes);
if
(inLength
==
blockSize)
...
{
int
outLength
=
cipher.update(inBytes,
0
,blockSize,outputBytes);
out.write(outputBytes,
0
, outLength);
}
else
more
=
false
;
}
if
(inLength
>
0
)
outputBytes
=
cipher.doFinal(inBytes,
0
, inLength);
else
outputBytes
=
cipher.doFinal();
out.write(outputBytes);
}
}
运行的时候,先在eclipse里面运行的参数里加上-genkey public.key private.key
生成两个文件,就是用于加密的公钥和私钥。
然后运行的参数里面加上-encrypt textFile encryptedFile public.key对第二个参数对应的文件加密,第三个参数对应的就是生成的加密过的文件。
最后运行-decrypt encryptedFile decryptedFile private.key对文件进行解密。
分享到:
相关推荐
Rsa算法 Rsa算法 Rsa算法 Rsa算法 Rsa算法 Rsa算法 Rsa算法 Rsa算法
java版的RSA算法,可以供参考使用。你可以试试哦
用java实现的RSA算法,如果有需要的朋友可以下载看看啊,
java实现RSA算法
RSA加密算法实现以及C#与java互通加解密,解决RSA算法在java与C#相互通用
模拟RSA算法进行加密,解密的过程.能对除中文外的任何字符进行加密解密.运行环境(JDK1.5.0)以上.
java语言,RSA算法写的文件加密加密系统
JAVA RSA 加密算法 RSA.java RSA公钥加解密,RSA私钥加解密,MD5withRSA 签名 MD5withRSA签名较验
浅析用Java编程实现RSA算法的过程.pdf
这是用JAVA编写的RSA算法,功能比较完整,不过注意支持的数字不能太大。
java平台实现rsa算法,自动进行加密解密输出密文
java利用大整数类实现的一个RSA加解密签名过程。
用JAVA语言实现RSA算法.kdh
用JAVA实现Rsa非对称加密算法 不算简单的算法 既可以加密又可以解密
用java实现的rsa加密算法,里面有详细的加密过程的实现
Java公钥加密算法RSA.rar可在NetBeans下直接运行 Java公钥加密算法RSA.rar可在NetBeans下直接运行
密码学中的RSA 算法的java代码实现,其中有模的重复平方计算法和中国剩余定理
对不起,之前这个RSA的实现代码,在上传时传的是空文档,现在新的在我的资源里,已经重新发布 因为资源不能删除,我也没办法啊!请各位不要下载这个……
包括RSA算法的两种语言实现,原理正确,可以正常运行,对应博客为:https://blog.csdn.net/qq_41112170/article/details/104904340
对不起,之前上传的那个RSA的实现代码,在上传时传的是空文档。现在纠正过来,sorry~