背景:java后台使用RSA能正确解析出密文
同理安卓也可以,但是安卓生成的密文在java后台确不能解析,具体方法如下:
需下载一个包:bcprov-jdk15on-159.jar
同时java后台:
public static final String PADDING = "RSA/NONE/NoPadding";
安卓后台为
public static final String PADDING = "RSAECB/NoPadding";
具体实现代码如下:
import java.io.FileNotFoundException; import java.io.IOException; import java.security.Key; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.PrivateKey; import java.security.PublicKey; import java.security.Security; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.security.spec.InvalidKeySpecException; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import javax.crypto.Cipher; import org.apache.commons.codec.binary.Base64; /** * @author JavaDigest * */ public class EncryptionUtil { /** * String to hold name of the encryption algorithm. */ public static final String ALGORITHM = "RSA"