[转载]java RSATest

在Java中使用加密的RSA算法



介绍

在这篇文章中,我会为您提供一个使用长串的RSA算法的方法。正如你知道,RSA算法是有限的117个字节,长字符串不能被加密或解密。然而,它可以分解成若干块的字节,然后进行加密或解密的内容。该算法用于非对称加密。对于非对称加密技术,您可以点击此链接。



技术性

在本文中,我在下面提供了加密和解密的长串完整的例子。如果您使用的密码类ie.doFinal(字节[] bytesString方法),它会抛出异常,它可以超过117字节的RSA加密。但在实际应用中,你可能不太相信的长度你想要的字符串进行加密或解密。在这种情况下,你必须打破字节,然后加密。请参阅

经过完整的例子。



完整的例子



类别名称:SecurityUtil.java



包com.dds.core.security;



进口java.security.KeyFactory;

进口java.security.KeyPair;

进口java.security.KeyPairGenerator;

进口java.security.PrivateKey;

进口java.security.PublicKey;

进口java.security.Security;

进口java.security.spec.EncodedKeySpec;

进口java.security.spec.PKCS8EncodedKeySpec;

进口java.security.spec.X509EncodedKeySpec;



进口javax.crypto.Cipher;



进口sun.misc.BASE64Decoder;

进口sun.misc.BASE64Encoder;



进口com.sun.crypto.provider.SunJCE;



/ **这是一个工具类,提供

*方便的方法的安全性。这

*类提供的方法,您可以

*加密和解密后的字符串

*超过117字节的RSA算法

*这是一个非对称的。

* @作者Debadatta米什拉(皮库)

*

* /

公共类SecurityUtil(

/ **

*类型的对象链接密钥)(@

* /

私人密钥对密钥;

/ **

*字符串变量,表示该算法

* /

私有静态最后字符串算法=“RSA的”;

/ **

* varibale的keysize

* /

私有静态最终诠释KEYSIZE = 1024;



/ **

*默认构造

* /

公共SecurityUtil()(

超级();

Security.addProvider(新SunJCE());





/ **

*此方法用于生成

*密钥对。

* /

公共无效invokeKeys()(

尝试(

KeyPairGenerator keypairGenerator = KeyPairGenerator

。使用getInstance(算法);

keypairGenerator.initialize(KEYSIZE);

密钥= keypairGenerator.generateKeyPair();

)赶上(例外五)(

e.printStackTrace();







/ **此方法用于获取字符串

*代表的公钥。

* @类型帕拉姆库马拉公钥公钥)(@链接

* @作为String返回公钥

* /

公共字符串getPublicKeyString(公钥公钥)(

返回新BASE64Encoder()。编码(publicKey.getEncoded());





/ **此方法用于获取字符串

*代表权的PrivateKey。

* @类型帕拉姆库马拉私钥链接的PrivateKey)(@

* @作为String返回的PrivateKey

* /

公共字符串getPrivateKeyString(的PrivateKey私钥)(

返回新BASE64Encoder()。编码(privateKey.getEncoded());





/ **此方法用于获取

*(@)联系的PrivateKey对象从

* String表示。

* @关键字符串类型帕拉姆库马拉

* @返回链接的PrivateKey)(@

* @抛出异常

* /

公众的PrivateKey getPrivateKeyFromString(字符串键)抛出异常(

的PrivateKey私钥=无效;

尝试(

KeyFactory keyFactory = KeyFactory.getInstance(算法);

EncodedKeySpec privateKeySpec =新PKCS8EncodedKeySpec(

新BASE64Decoder()。decodeBuffer(键));

私钥= keyFactory.generatePrivate(privateKeySpec);

)赶上(例外五)(

e.printStackTrace();



返回私钥;





/ **此方法用于获取链接公钥)(@

*从公共密钥字符串表示形式。

* @关键字符串类型帕拉姆库马拉

* @返回链接公钥)(@

* @抛出异常

* /

公共公钥getPublicKeyFromString(字符串键)抛出异常(

公钥公钥=无效;

尝试(

KeyFactory keyFactory = KeyFactory.getInstance(算法);

EncodedKeySpec publicKeySpec =新X509EncodedKeySpec(

新BASE64Decoder()。decodeBuffer(键));

公钥= keyFactory.generatePublic(publicKeySpec);

)赶上(例外五)(

e.printStackTrace();



返回公钥;





/ **此方法用于获取

*加密的内容从原来的

通过传递链路公钥*(@)的内容。

*这种方法非常有用,当字节更

*比117。

* @文本字符串类型帕拉姆库马拉

* @类型帕拉姆库马拉关键环节公钥)(@

* @返回值作为一个字符串加密

* @抛出异常

* /

公共字符串getEncryptedValue(字符串文本,公钥密钥)

抛出异常(

弦乐encryptedText;

尝试(

字节[] textBytes = text.getBytes(“UTF8的”);

密码加密= Cipher.getInstance(“RSA/ECB/PKCS1Padding”);

cipher.init(Cipher.ENCRYPT_MODE,键);



诠释textBytesChunkLen = 100;

诠释encryptedChunkNum =(textBytes.length - 1)/ textBytesChunkLen

+ 1;



/ / RSA的收益为100字节的文本输出128字节

诠释encryptedBytesChunkLen = 128;

诠释encryptedBytesLen = encryptedChunkNum * encryptedBytesChunkLen;

System.out.println(“加密字节长度-------”

+ encryptedBytesChunkLen);

/ /定义输出数组。

字节[] encryptedBytes =新字节[encryptedBytesLen];



诠释textBytesChunkIndex = 0;

诠释encryptedBytesChunkIndex = 0;



为(int i = 0;我

如果(我

encryptedBytesChunkIndex = encryptedBytesChunkIndex

+ cipher.doFinal(textBytes,textBytesChunkIndex,

textBytesChunkLen,encryptedBytes,

encryptedBytesChunkIndex);



textBytesChunkIndex = textBytesChunkIndex

+ textBytesChunkLen;

否则)(

cipher.doFinal(textBytes,textBytesChunkIndex,

textBytes.length - textBytesChunkIndex,

encryptedBytes,encryptedBytesChunkIndex);





encryptedText =新BASE64Encoder()。编码(encryptedBytes);

)赶上(例外五)(

扔情报;



返回encryptedText;





/ **此方法是用来解密的内容。

*这种方法非常有用的规模时,

*字节是117多人。

* @ String类型帕拉姆库马拉文字说明

*加密的内容。

* @类型帕拉姆库马拉关键环节的PrivateKey)(@

* @返回值作为一个字符串解密

* /

公共字符串getDecryptedValue(字符串文本,关键的PrivateKey)(

字符串结果=空;

尝试(

字节[] encryptedBytes =新BASE64Decoder()。decodeBuffer(文字);

密码加密= Cipher.getInstance(“RSA/ECB/PKCS1Padding”);

cipher.init(Cipher.DECRYPT_MODE,键);



诠释encryptedByteChunkLen = 128;

诠释encryptedChunkNum = encryptedBytes.length

/ encryptedByteChunkLen;

诠释decryptedByteLen = encryptedChunkNum * encryptedByteChunkLen;

字节[] decryptedBytes =新字节[decryptedByteLen];

诠释decryptedIndex = 0;

诠释encryptedIndex = 0;



为(int i = 0;我

如果(我

decryptedIndex = decryptedIndex

+ cipher.doFinal(encryptedBytes,encryptedIndex,

encryptedByteChunkLen,decryptedBytes,

decryptedIndex);

encryptedIndex = encryptedIndex + encryptedByteChunkLen;

否则)(

decryptedIndex = decryptedIndex

+ cipher.doFinal(encryptedBytes,encryptedIndex,

encryptedBytes.length - encryptedIndex,

decryptedBytes,decryptedIndex);





结果=新的字符串(decryptedBytes)。修剪();

)赶上(例外五)(

e.printStackTrace();



返回结果;





/ **此方法用于获取

*(@)链接公钥

* @返回链接公钥)(@

* /

公共公钥getPublicKey()(

返回keyPair.getPublic();





/ **此方法用于获取

*链接的PrivateKey的(@)

* @返回链接的PrivateKey)(@

* /

公众的PrivateKey getPrivateKey()(

返回keyPair.getPrivate();







上述类提供的私钥,公钥和String和String的加密解密了几个有用的方法。



请参考下列从属阶级上述类。



类别名称:KeyGenerator.java



包com.dds.core.security;



进口的java.io.File;

进口java.io.FileOutputStream;

进口java.io.OutputStream;

进口java.security.PrivateKey;

进口java.security.PublicKey;

进口java.util.Properties类型;



/ **这个类是用来产生

*私人和公共密钥和商店

*在文件里。

* @作者Debadatta米什拉(皮库)

*

* /

公共类KeyGenerator(

/ **此方法用于获取

*路径的关键目录

*私人和公共密钥文件

*储存。

* @返回路径的关键目录

* /

私有静态字符串getKeyFilePath()(

弦乐keyDirPath =空;

尝试(

keyDirPath = System.getProperty(“user.dir”)+ File.separator

+“键”;

文件keyDir =新的文件(keyDirPath);

如果(!keyDir.exists())

keyDir.mkdirs();

)赶上(例外五)(

e.printStackTrace();



返回keyDirPath;





/ **

*此方法用于生成

*私人和公共密钥。

* /

公共静态无效generateKeys()(

性能publicProp =新特性();

性能privateProp =新特性();

尝试(

OutputStream的pubOut =新FileOutputStream(getKeyFilePath()

+ File.separator +“public.key”);

OutputStream的priOut =新FileOutputStream(getKeyFilePath()

+ File.separator +“private.key”);



SecurityUtil secureUtil =新SecurityUtil();

secureUtil.invokeKeys();



公钥公钥= secureUtil.getPublicKey();

的PrivateKey私钥= secureUtil.getPrivateKey();



弦乐publicString = secureUtil.getPublicKeyString(公钥);

弦乐privateString = secureUtil.getPrivateKeyString(私钥);



publicProp.put(“键”,publicString);

publicProp.store(pubOut,“公共密钥信息”);



privateProp.put(“键”,privateString);

privateProp.store(priOut,“私人密钥信息”);

)赶上(例外五)(

e.printStackTrace();









上述类用于生成公钥和私钥。它可以生成并存储在不同的文件并呼吁Public.key和Private.key。请参阅上面的类的测试工具类。



类别名称:TestKeyGenerator



进口com.dds.core.security.KeyGenerator;



/ **这是一个testharness类

*为KeyGenerator类。

* @作者Debadatta米什拉(皮库)

*

* /

公共类TestKeyGenerator(

公共静态无效的主要(字串[] args)(

KeyGenerator.generateKeys();







如果你运行上面的类,你会发现在你的目录中您的应用程序文件夹的根路径的关键。在此文件夹中你会发现两个文件一个是民营的关键信息,另一个是公共密钥。

还有另外一个是用来获取私有密钥和公开密钥信息存储在文件中的类。



类别名称:KeyReader.java



包com.dds.core.security;



进口的java.io.File;

进口java.io.FileInputStream;

进口java.io.InputStream;

进口java.security.PublicKey;

进口java.util.Properties类型;



/ **这个类是用来读取

从文件*键。

* @作者Debadatta米什拉(皮库)

*

* /

公共类KeyReader(

/ **此方法用于获取

*公共密钥字符串值

*从文件中。

* @返回字符串)(@链接公钥

* /

公共静态字符串getPublicKeyString()(

弦乐publicString =空;

尝试(

属性道具=新特性();

弦乐publicKeyPath = System.getProperty(“user.dir”)

+ File.separator +“键”+ File.separator +“public.key”;

在新的InputStream = FileInputStream(publicKeyPath);

prop.load(中);

publicString = prop.getProperty(“键”);

)赶上(例外五)(

e.printStackTrace();



返回publicString;





/ **此方法用于获取

*字符串从文件的私钥。

* @返回字符串的私钥

* /

公共静态字符串getPrivateKeyString()(

弦乐publicString =空;

尝试(

属性道具=新特性();

弦乐publicKeyPath = System.getProperty(“user.dir”)

+ File.separator +“键”+ File.separator +“private.key”;

在新的InputStream = FileInputStream(publicKeyPath);

prop.load(中);

publicString = prop.getProperty(“键”);

)赶上(例外五)(

e.printStackTrace();



返回publicString;







这是一个实用类阅读从公共和私人密钥的文件。



现在,指的是测试工具类使得加密和解密字符串。



进口java.security.PrivateKey;

进口java.security.PublicKey;



进口com.dds.core.security.KeyReader;

进口com.dds.core.security.SecurityUtil;



/ **

*这是一个用于加密和解密测试工具类。

*

* @作者Debadatta米什拉(皮库)

*

* /

公共类TestEncryption(



公共静态无效的主要(字串[] args)(

弦乐privateKeyString = KeyReader.getPrivateKeyString();

SecurityUtil securityUtil =新SecurityUtil();

弦乐publicKeyString = KeyReader.getPublicKeyString();

尝试(

公钥公钥= securityUtil

。getPublicKeyFromString(publicKeyString);

的PrivateKey私钥= securityUtil

。getPrivateKeyFromString(privateKeyString);

弦乐originalValue =“提供了一些非常长的字符串”;



弦乐encryptedValue = securityUtil.getEncryptedValue(

originalValue,公钥);

System.out.println(“EncryptedValue -----”+ encryptedValue);

弦乐decryptedValue = securityUtil.getDecryptedValue(

encryptedValue,私钥);

System.out.println(“原始价值------”+ decryptedValue);



)赶上(例外五)(

e.printStackTrace();











这个测试工具类是用来加密和解密的长字符串的内容。您还可以使用加密和解密的文件相同的方法。首先,你必须为String读取一个文件的内容和方法,那么你可以申请加密。



结论

我希望你会喜欢我的这对RSA非对称加密技术的文章。对于非对称加密技术,请参阅链接http://www.articlesbase.com/information-technology-articles/asymmetric-cryptography-in-java-438155.html。如果您发现任何问题或错误,请随时给我的地址debadattamishra@aol.com邮件。本文仅是用于那些谁是新的Java开发。本文不承担任何商业意义。请提供我对这篇文章的反馈
          

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值