我们正面临数据加密解密的问题。在服务器端,我们有这样的解密 java 代码
public static String decrypt(byte[] data, PrivateKey base64PrivateKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return new String(cipher.doFinal(data));
}
对于客户端的加密,我们使用 jsencrypt。代码看起来像这样
encrypt(numInfo) {
this.readKeytext('assets/public.pem');
getKey = getKey.replace(/(^"|"$)/g, '');
if (cardNum.length > 117) {
} else {
this.$encrypt.setPublicKey(getKey);
this.cypherCardnum = this.$encrypt.encrypt(numInfo);
return this.cypherCardnum;
}
}
我们使用 RSA-3072 密钥生成技术来创建公钥和私钥。谁能建议我在 angularjs 中进行适合服务器端实现的加密的最佳方法?
更新:
样本:
原始数据:4000000000000002
私钥:
MIIG/AIBADANBgkqhkiG9w0BAQEFAASCBuYwggbiAgEAAoIBgQCj19SkjHjzHdIv
CSOWMvXLFfLj2wKRcgZakSVSmqB/K1yeXE/ZMGVv/cnZD0qNbg29PHUIm6DkUKoY
xg6nkqdyzMDAIpEb1dIhzDctIqqPYOnl2FNwUY6h5F98G7v0cEKKYdXpFRfYgrLZ
CmqDxIV7O5TDfJXMIDkXBzootPPxrWCkQ/FjbIN04SNeiwiKmO6exdPEYUklHR8H
BPQLeooSMVJ3kQYOQzV6HcXmSm7AEzx4tn4hcHy6yV5GXkXrcJxRUaRYYR5p/lG9
FgCTZVGRGuXYnxKGcmRzYZgB6vbdxjT7lUgaeZ+YR8EjrVOZ+ath+quNot9TBFXL
e1k2Ympdf6UwTckDWl+qovFyV7bxEWKBCVuECVatcMXlaBKaAGBdTc2WKXrUCnok
0bgTti7SscXwgZwb+Qa/XNes0PXPn/obpc9WfOQXLFi4Wd7ybRdHwck3NxB231VN
uinL9hk8EvFcWiSutTBZClBGSH3xxJ3yk5dWDqL0dVvOpW6dzU8CAwEAAQKCAYAz
EfeK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UVGkWGEeaf5RvRYAk2VR
kRrl2J8ShnJkc2GYAer23cY0+5VIGnmfmEfBI61TmfmrYfqrjaLfUwRVy3tZNmJq
XX+lME3JA1pfqqLxcle28RFigQlbhAlWrXDF5WgSmgBgXU3Nlil61Ap6JNG4E7Yu
0rHF8IGcG/kGv1zXrND1z5/6G6XPVnzkFyxYuFne8m0XR8HJNzcQdt9VTbopy/YZ
PBLxXFokrrUwWQpQRkh98cSd8pOXVg6i9HVbzqVunc1PAgMBAAE=
加密数据:
MtZahearfNk7awePM/B11fsx0DcUVnjWF9vrVkAG4g7wNNeZyMAN0F2JFO2SesfjAlv5THtycRh/5DZNBQDQDeWL2O0FEF4ByY/ZsBnJAA16n9iICK17ECuIAl2Yzlra2S+10uS5A7YFkzDRfQpwfv3S8FZ/srqT8kdTDHb5Rn+3XA18h39qyxvwL6+7WueTkE8bcRTKtaOfANspsxxa28NfwnR3yhyPQsETHWFklJChFE08ULWKf9ILT3j1+rME8ZNyUZ25B9VKJyaQ6Dk/u2M127SfQNvJf0ATlM/WRTIiVJ5Xx+k4sW9WsAGu2pQnrN43eGor5fbrkwnY7sNA12oLkGvP40UCsB2OOW1tNd0PiwAb3IN49xtPvKDFCjJiKBK8zz5DOmGvUdAIpeK2WoU6OeYIkouXlFWTRFQjbbS37vHWVz2IWB+4gyBGBGjOX3uWT3y+V5O729BWNf0j95je3EnX2fcEbXVi4FyHcrheTgSID/FLPqW/dVhO56yO
例外:
java.lang.IllegalArgumentException: Illegal base64 character 2e at
java.util.Base64$Decoder.decode0(Base64.java:714) at
java.util.Base64$Decoder.decode(Base64.java:526) at
com.ibsplc.ps.common.utils.RSAUtil.getPrivateKey(RSAUtil.java:63)
更新:
public static PublicKey getPublicKey(String base64PublicKey){
PublicKey publicKey = null;
try{
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(base64PublicKey.getBytes()));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
}
return publicKey;
}