package com.hai.JavaDemo;
import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.security.*;
public class Main {
//原文长度必须keySize/8长度,noPadding
private final static String transformation = "RSA/ECB/NoPadding";//ok
//加密分块大小<=keySize/8-11;解密分块大小keySize/8
private final static String transformation2 = "RSA/ECB/PKCS1Padding";//ok
//加密分块大小<=keySize/8-42;解密分块大小keySize/8
private final static String transformation3 = "RSA/ECB/OAEPWithSHA-1AndMGF1Padding";//ok
//加密分块大小<=keySize/8-58;解密分块大小keySize/8
private final static String transformation4 = "RSA/ECB/OAEPWithSHA-224AndMGF1Padding";//IllegalBlockSizeException
//加密分块大小<=keySize/8-66;解密分块大小keySize/8
private final static String transformation5 = "RSA/ECB/OAEPWithSHA-256AndMGF1Padding";//IllegalBlockSizeException
//加密分块大小<=keySize/8-98;解密分块大小keySize/8
private final static String transformation6 = "RSA/ECB/OAEPWithSHA-384AndMGF1Padding";//IllegalBlockSizeException
//加密分块大小<=keySize/8-130;解密分块大小keySize/8,建议KEY_SIZE=2048
private final static String transformation7 = "RSA/ECB/OAEPWithSHA-512AndMGF1Padding";//IllegalBlockSizeException
//加密分块大小<=keySize/8-42;解密分块大小keySize/8
private final static String transformation8 = "RSA/ECB/OAEPPadding";//ok
private static String useTransFormation = transformation5;
//rsa keysize一般是1024或2048
public static final int KEY_SIZE = 2048;
// private static String src = "1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678";
private static String src = "123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345";
public static void main(String[] args) {
KeyPair keyPair = null;
try {
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(KEY_SIZE);
keyPair = generator.genKeyPair();
System.out.println("getAlgorithm:" + keyPair.getPublic().getAlgorithm());
System.out.println("getFormat:" + keyPair.getPublic().getFormat());
System.out.println("getEncoded:" + keyPair.getPublic().getEncoded().length);
System.out.println("getPriAlgorithm:" + keyPair.getPrivate().getAlgorithm());
System.out.println("getPriFormat:" + keyPair.getPrivate().getFormat());
System.out.println("getPriEncoded:" + keyPair.getPrivate().getEncoded().length);
byte[] encrypt = encrypt(keyPair, useTransFormation);
decrypt(keyPair, useTransFormation, encrypt);
} catch (Exception e) {
e.printStackTrace();
try {
byte[] encrypt = encryptMulti(keyPair, useTransFormation, getTrunkLength(useTransFormation, true));
decryptMulti(keyPair, useTransFormation, KEY_SIZE / 8, encrypt);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
static int getTrunkLength(String tf, boolean encrypt) {
if (encrypt) {
switch (tf) {
case transformation:
return KEY_SIZE;
case transformation2:
return KEY_SIZE / 8 - 11;
case transformation3:
return KEY_SIZE / 8 - 42;
case transformation4:
return KEY_SIZE / 8 - 58;
case transformation5:
return KEY_SIZE / 8 - 66;
case transformation6:
return KEY_SIZE / 8 - 98;
case transformation7:
return KEY_SIZE / 8 - 130;
case transformation8:
return KEY_SIZE / 8 - 42;
}
} else return KEY_SIZE / 8;
return KEY_SIZE;
}
static byte[] encrypt(KeyPair keyPair, String transformation) throws Exception {
Cipher cipher = Cipher.getInstance(transformation);
cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic());
byte[] encrypt = cipher.doFinal(src.getBytes());
System.out.println("encryptLen:" + encrypt.length);
return encrypt;
}
static void decrypt(KeyPair keyPair, String transformation, byte[] encrypt) throws Exception {
Cipher cipher = Cipher.getInstance(transformation);
cipher.init(Cipher.DECRYPT_MODE, keyPair.getPrivate());
byte[] decrypt = cipher.doFinal(encrypt);
System.out.println("decrypt :" + new String(decrypt));
System.out.println("decrypt == src:" + new String(decrypt).equals(src));
}
static byte[] encryptMulti(KeyPair keyPair, String transformation, int trunkLenth) throws Exception {
System.out.println("encryptMulti trunkLenth:" + trunkLenth);
Cipher cipher = Cipher.getInstance(transformation);
cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic());
byte[] srcBytes = src.getBytes();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
if (srcBytes.length <= trunkLenth) {
byte[] encryBytes = cipher.doFinal(srcBytes);
System.out.println("srcBytes.length <= trunkLenth encryptLen:" + encryBytes.length);
bos.writeBytes(encryBytes);
} else {
int offset = 0;
while (srcBytes.length - offset > 0) {
int trunk = (srcBytes.length - offset) > trunkLenth ? trunkLenth : srcBytes.length - offset;
byte[] bytes = cipher.doFinal(srcBytes, offset, trunk);
System.out.println("while encryptLen:" + bytes.length);
bos.writeBytes(bytes);
offset += trunkLenth;
}
}
byte[] out = bos.toByteArray();
System.out.println("encryptEndLen:" + out.length);
return out;
}
static void decryptMulti(KeyPair keyPair, String transformation, int trunkLenth, byte[] encrypt) throws Exception {
System.out.println("decryptMulti trunkLenth:" + trunkLenth);
Cipher cipher = Cipher.getInstance(transformation);
cipher.init(Cipher.DECRYPT_MODE, keyPair.getPrivate());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
if (encrypt.length <= trunkLenth) {
byte[] encryBytes = cipher.doFinal(encrypt);
System.out.println("srcBytes.length <= trunkLenth decryptLen:" + encryBytes.length);
bos.writeBytes(encryBytes);
} else {
int offset = 0;
while (encrypt.length - offset > 0) {
int trunk = (encrypt.length - offset) > trunkLenth ? trunkLenth : encrypt.length - offset;
byte[] bytes = cipher.doFinal(encrypt, offset, trunk);
System.out.println("while decryptLen:" + bytes.length);
bos.writeBytes(bytes);
offset += trunkLenth;
}
}
byte[] out = bos.toByteArray();
System.out.println("decrypt :" + new String(out));
System.out.println("decrypt == src:" + new String(out).equals(src));
}
}
JAVA RSA加解密demo
最新推荐文章于 2023-11-27 15:21:50 发布