Commit 70f336e8 authored by yanzg's avatar yanzg

修改实体位置

parent 03bc58c5
...@@ -7,6 +7,7 @@ import org.slf4j.LoggerFactory; ...@@ -7,6 +7,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.util.Base64Utils; import org.springframework.util.Base64Utils;
import javax.crypto.Cipher; import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.security.*; import java.security.*;
import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey; import java.security.interfaces.RSAPublicKey;
...@@ -27,6 +28,7 @@ public final class RsaHelper { ...@@ -27,6 +28,7 @@ public final class RsaHelper {
private static final String ALGORITHM_SIGN = "MD5withRSA"; private static final String ALGORITHM_SIGN = "MD5withRSA";
private static final int KEYPAIR_LEN = 1024; private static final int KEYPAIR_LEN = 1024;
public static final int MAX_ENCRYPT_BLOCK = 64;
private RsaHelper() { private RsaHelper() {
super(); super();
...@@ -100,10 +102,30 @@ public final class RsaHelper { ...@@ -100,10 +102,30 @@ public final class RsaHelper {
PublicKey publicKey = getPublicKey(publicKeyStr); PublicKey publicKey = getPublicKey(publicKeyStr);
Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm()); Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, publicKey); cipher.init(Cipher.ENCRYPT_MODE, publicKey);
cipher.update(source.getBytes(SystemContants.UTF8));
byte[] bytes = cipher.doFinal(); byte[] bytesFrom = source.getBytes(SystemContants.UTF8);
String target = encodeBase64(bytes); ByteArrayOutputStream out = new ByteArrayOutputStream();
return target;
int inputLen = bytesFrom.length;
int offSet = 0;
byte[] cache;
int i = 0;
// 对数据分段加密
while (inputLen - offSet > 0) {
if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
cache = cipher.doFinal(bytesFrom, offSet, MAX_ENCRYPT_BLOCK);
} else {
cache = cipher.doFinal(bytesFrom, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * MAX_ENCRYPT_BLOCK;
}
byte[] encryptedData = out.toByteArray();
out.close();
return encodeBase64(encryptedData);
} catch (Exception ex) { } catch (Exception ex) {
throw new RuntimeException(ex); throw new RuntimeException(ex);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment