admin 管理员组

文章数量: 887021


2024年1月12日发(作者:unix大作业文件系统)

(java)RSAECBPKCS1Padding算法加密和解密直接上代码:(有许多调试信息)[java]

01.

02.

03.

04.

05.

06.

07.

08.

09.

10.

11.

12.

13.

14.

15.

16.

17.

18.

19.

20.

21.

22.

23.

24.

25.

26.

27.

28.

29.

30.

31.

32.

33.

34.

35.

36.

37.

38.

39.

40.

41.

42.

43.

44.

45.

46.

47.

48.

49.

50.

51.

52.

53.

54.

55.

56.

57.

58.

59.

60.

61.

62.

63.

64.

65.

66.

67.

68.

69.

70.

71.

72.

73.

import .*;

import ;

import ty.*;

import .*;

import 64;

import ;

public class TestEncryp {

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

// TODO Auto-generated method stub

PrivateKey pri=getPriKey("/Users/cqx/bh_pkcs8_rsa_private_key_","RSA");

PublicKey pub=getPubKey("/Users/cqx/rsa_public_key_","RSA");

n("hahhahah11");

String str="我是需要传递的字符串";

byte[] estr=encrypt(es(),pub,2048, 11,"RSA/ECB/PKCS1Padding");

n(new String(estr));

n("hahhahah12");

byte[] dstr=decrypt(estr, pri, 2048, 11, "RSA/ECB/PKCS1Padding");

n(new String(dstr));

}

public static byte[] decrypt(byte[] encryptedBytes, PrivateKey privateKey, int keyLength,

int keyByteSize = keyLength / 8;

int decryptBlockSize = keyByteSize - reserveSize;

int nBlock = / keyByteSize;

ByteArrayOutputStream outbuf = null;

try {

Cipher cipher = tance(cipherAlgorithm);

(T_MODE, privateKey);

outbuf = new ByteArrayOutputStream(nBlock * decryptBlockSize);

for (int offset = 0; offset < ; offset += keyByteSize) {

int inputLen = - offset;

if (inputLen > keyByteSize) {

inputLen = keyByteSize;

}

byte[] decryptedBlock = l(encryptedBytes, offset, inputLen);

(decryptedBlock);

}

();

return Array();

} catch (Exception e) {

throw new Exception("DEENCRYPT ERROR:", e);

} finally {

try{

if(outbuf != null){

();

}

}catch (Exception e){

outbuf = null;

throw new Exception("CLOSE ByteArrayOutputStream ERROR:", e);

}

}

}

public static byte[] encrypt(byte[] plainBytes, PublicKey publicKey, int keyLength, int reserveSize, String cipherAlgorithm)

int keyByteSize = keyLength / 8;

int encryptBlockSize = keyByteSize - reserveSize;

int nBlock = / encryptBlockSize;

if (( % encryptBlockSize) != 0) {

nBlock += 1;

}

ByteArrayOutputStream outbuf = null;

try {

Cipher cipher = tance(cipherAlgorithm);

(T_MODE, publicKey);

outbuf = new ByteArrayOutputStream(nBlock * keyByteSize);

for (int offset = 0; offset < ; offset += encryptBlockSize) {

int inputLen = - offset;

if (inputLen > encryptBlockSize) {

inputLen = encryptBlockSize;

73.

74.

75.

76.

77.

78.

79.

80.

81.

82.

83.

84.

85.

86.

87.

88.

89.

90.

91.

92.

93.

94.

95.

96.

97.

98.

99.

100.

101.

102.

103.

104.

105.

106.

107.

108.

109.

110.

111.

112.

113.

114.

115.

116.

117.

118.

119.

120.

121.

122.

123.

124.

125.

126.

127.

128.

129.

130.

131.

132.

133.

134.

135.

136.

137.

138.

139.

140.

141.

142.

143.

144.

145.

146.

147.

148.

149.

150.

151.

152.

153.

inputLen = encryptBlockSize;

}

byte[] encryptedBlock = l(plainBytes, offset, inputLen);

(encryptedBlock);

}

();

return Array();

} catch (Exception e) {

throw new Exception("ENCRYPT ERROR:", e);

} finally {

try{

if(outbuf != null){

();

}

}catch (Exception e){

outbuf = null;

throw new Exception("CLOSE ByteArrayOutputStream ERROR:", e);

}

}

}

public static PrivateKey getPriKey(String privateKeyPath,String keyAlgorithm){

PrivateKey privateKey = null;

InputStream inputStream = null;

try {

if(inputStream==null){

n("hahhah1!");

}

inputStream = new FileInputStream(privateKeyPath);

n("hahhah2!");

privateKey = getPrivateKey(inputStream,keyAlgorithm);

n("hahhah3!");

} catch (Exception e) {

n("加载私钥出错!");

} finally {

if (inputStream != null){

try {

();

}catch (Exception e){

n("加载私钥,关闭流时出错!");

}

}

}

return privateKey;

}

public static PublicKey getPubKey(String publicKeyPath,String keyAlgorithm){

PublicKey publicKey = null;

InputStream inputStream = null;

try {

n("hahhahah8");

inputStream = new FileInputStream(publicKeyPath);

n("hahhahah9");

publicKey = getPublicKey(inputStream,keyAlgorithm);

n("hahhahah10");

} catch (Exception e) {

n("加载公钥出错!");

} finally {

if (inputStream != null){

try {

();

}catch (Exception e){

n("加载公钥,关闭流时出错!");

}

}

}

return publicKey;

}

public static PublicKey getPublicKey(InputStream inputStream, String keyAlgorithm) throws try {

BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));

StringBuilder sb = new StringBuilder();

String readLine = null;

while ((readLine = ne()) != null) {

if ((0) == '-') {

continue;

} else {

(readLine);

('r');

}

}

X509EncodedKeySpec pubX509 = new X509EncodedKeySpec(decodeBase64(ng()));


本文标签: 加载 出错 关闭