import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPublicKeySpec;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
/**
* RSA NO Padding加密
* @author JiangZhongke
*
*/
public class RSAUtil {
/**
* 获取SequryData 待加密数据
*
* @param password 密码
* @param validate 有效期
* @param cvn2 CVN2/CVV
* @return SequryData
*/
public static String setSecuryData(String password, String validate,
String cvn2) {
StringBuffer cod = new StringBuffer("010");
cod.append(password.length());
cod.append(password);
for (int i = 0; i < 14 - password.length(); i++) {
cod.append("F");
}
cod.append("02");
if (null == validate || "null".equals(validate) || "".equals(validate))
cod.append("00000000");
else
cod.append(printHexString(validate.getBytes()));
cod.append("03");
if (null == cvn2 || "null".equals(cvn2) || "".equals(cvn2))
cod.append("000000");
else
cod.append(printHexString(cvn2.getBytes()));
return getSecuryDataEncod(cod.toString());
}
/**
* 获取加密数据
*
* @param encod
* @return
*/
private static String getSecuryDataEncod(String encod) {
String publicKey = "AF1B6BDA67852BD049AE93663DE54A26315DF5B851F0829D2A11A72B21264C7C6E085F914873A97A6169C9C9D50187F4D4EDA8D930032BDACB6CF24A811B93A4ED962E3D5F84AA2FA7323F65FD1DC4A3AA5A32A3C5983CB794A6F82FF2E03EFD6BBB31A528BE4CDA138AD30564B8C442D37E9D264BD0FD8FC6F778DB3ADCC579";
int publicExponent = 65537;
return encryptByPublicKey(encod, publicKey, publicExponent);
}
private static String encryptByPublicKey(String encod, String publicKey,
int publicExponent) {
byte[] enChallengeCode = null;
// 转BCD
byte[] byteEncod = ByteUtil.lengthToBcd(encod);
PublicKey pubKey = getPublicKey(publicKey, publicExponent);
// 明文待加密数据
byte[] challengeCode = new byte[128];
for (int i = 0; i <= 17; i++) {
challengeCode[i] = byteEncod[i];
}
// 注意加密时使用的是no padding方式需要对分组数据的剩余部分添加00进行填充。
for (int i = 18; i < 128; i++) {
challengeCode[i] = 0x00;
}
enChallengeCode = encryptByPublicKey(challengeCode, pubKey);
return Base64.getBASE64Byte(enChallengeCode);
}
/**
*
* @param modulus
* 公钥的hexstring
* @param publicExponent
* exponent 为65537
*
* @return
*/
private static PublicKey getPublicKey(String modulus, int publicExponent) {
BigInteger bigIntegerModulus = new BigInteger(modulus, 16);
RSAPublicKeySpec rsaSpec = new RSAPublicKeySpec(bigIntegerModulus,
BigInteger.valueOf(publicExponent));
PublicKey pKey = null;
try {
KeyFactory kf = KeyFactory.getInstance("RSA");
pKey = kf.generatePublic(rsaSpec);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeySpecException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return pKey;
}
/**
*
* @param data
* @param publicKey
* @return
*/
private static byte[] encryptByPublicKey(byte[] data, PublicKey publicKey) {
byte[] enBytes = {};
try {
//KeyFactory keyFactory = KeyFactory.getInstance("RSA");
// Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
enBytes = cipher.doFinal(data);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return enBytes;
}
/**
* byte->HexString
*
* @param b
* @return
*/
private static String printHexString(byte[] b) {
String a = "";
for (int i = 0; i < b.length; i++) {
String hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
a = a + hex;
}
return a;
}
public static void main(String[] args) {
String encod = setSecuryData("123456", null, null);
System.out.println(encod);
}
}
RSANoPadding加密
最新推荐文章于 2022-01-26 15:42:18 发布