Android Base64 编码和 Apache codec 解码
Android Base64 Encoding and Apache codec decoding
我们使用以下方法使用 ANDROID Base64.NO_CLOSE
对字符串进行编码
public static String encrypt(String inputString, byte[] keyBytes) {
Calendar cal = Calendar.getInstance();
int mDay = cal.get(Calendar.DAY_OF_MONTH);
// System.out.println("Day of month :::" + mDay);
String encryptedString = "";
Key publicKey = null;
try {
Random generator = new Random(mDay);
int num = (generator.nextInt()) % 100;
String salt = "WEER563784" + num;
inputString += salt;
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
publicKey = keyFactory.generatePublic(publicKeySpec);
} catch (Exception e) {
System.out.println("Exception rsaEncrypt::::::::::::::::: "
+ e.getMessage());
e.printStackTrace();
}
// Encode the original data with RSA public key
byte[] encodedBytes = null;
try {
Cipher c = Cipher.getInstance("RSA");
c.init(Cipher.ENCRYPT_MODE, publicKey);
encodedBytes = c.doFinal(inputString.getBytes());
encryptedString = Base64.encodeToString(encodedBytes,
Base64.NO_CLOSE);
System.out.println(encryptedString);
} catch (Exception e) {
System.out.println("Exception rsaEncrypt::::::::::::::::: "
+ e.getMessage());
e.printStackTrace();
}
return encryptedString;
}
正在使用以下方法在 Android 应用外部解密生成的加密字符串
public static String decrypt(String inputString, byte[] keyBytes) {
String resultStr = null;
Calendar cal = Calendar.getInstance();
int mDay = cal.get(Calendar.DAY_OF_MONTH);
Random generator = new Random(mDay);
int num = (generator.nextInt()) % 100;
String salt = "qqq" + num;
PrivateKey privateKey = null;
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(keyBytes);
privateKey = keyFactory.generatePrivate(privateKeySpec);
} catch (Exception e) {
System.out.println("Exception privateKey::::::::::::::::: "
+ e.getMessage());
}
byte[] decodedBytes = null;
try {
Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding");
// Cipher c = Cipher.getInstance("RSA");
c.init(Cipher.DECRYPT_MODE, privateKey);
// decodedBytes = c.doFinal(Base64.decodeBase64(inputString));
decodedBytes = c.doFinal(Base64InputStream());
} catch (Exception e) {
System.out.println("Exception privateKey1::::::::::::::::: "
+ e.getMessage());
e.printStackTrace();
}
if (decodedBytes != null) {
resultStr = new String(decodedBytes);
System.out.println("resultStr:::" + resultStr + ":::::");
resultStr = resultStr.replace(salt, "");
}
return resultStr;
}
我们遇到以下异常
javax.crypto.BadPaddingException: Decryption error
at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:380)
at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:291)
at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:365)
at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:391)
at javax.crypto.Cipher.doFinal(Cipher.java:2087)
at RSAEncryption.decrypt(RSAEncryption.java:41)
at RSAEncryption.main(RSAEncryption.java:108)
所以问题是
1) 是否可以在 Android 之外使用 ANDROID Base64.NO_CLOSE 解密加密的字符串,我的意思是直接在 IDE 中?
2)在post的其中一个中,我发现使用ANDROIDBase64.NO_WRAP加密的字符串可以在Androidenv之外解密,这样理解正确吗?
非常感谢您的提前帮助。
此致,
阿米特
问题已解决...
解密时需要用到RSA/ECB/NoPadding
基本上,当我们在 Android 中使用 RSA 加密值时,在单独的独立设备中解密 Java env 应该在密码中使用 RSA/ECB/NoPadding。
Android加密代码:
public static String encrypt(String inputString, byte[] keyBytes) {
Calendar cal = Calendar.getInstance();
int mDay = cal.get(Calendar.DAY_OF_MONTH);
// System.out.println("Day of month :::" + mDay);
String encryptedString = "";
Key publicKey = null;
try {
Random generator = new Random(mDay);
int num = (generator.nextInt()) % 100;
String salt = "MNSadm563784" + num;
//inputString += salt;
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
publicKey = keyFactory.generatePublic(publicKeySpec);
} catch (Exception e) {
System.out.println("Exception rsaEncrypt::::::::::::::::: "
+ e.getMessage());
e.printStackTrace();
}
// Encode the original data with RSA public key
byte[] encodedBytes = null;
try {
Cipher c = Cipher.getInstance("RSA");
c.init(Cipher.ENCRYPT_MODE, publicKey);
encodedBytes = c.doFinal(inputString.getBytes());
encryptedString = Base64.encodeToString(encodedBytes,
Base64.NO_CLOSE);
System.out.print("-----??"+encryptedString+"??-------");
} catch (Exception e) {
System.out.println("Exception rsaEncrypt::::::::::::::::: "
+ e.getMessage());
e.printStackTrace();
}
return encryptedString;
}
服务器解密码:
public static String decrypt(String inputString, byte[] keyBytes) {
String resultStr = null;
// Calendar cal = Calendar.getInstance();
// int mDay = cal.get(Calendar.DAY_OF_MONTH);
// Random generator = new Random(mDay);
// int num = (generator.nextInt()) % 100;
// String salt = "MNSadm563784" + num;
PrivateKey privateKey = null;
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(
keyBytes);
privateKey = keyFactory.generatePrivate(privateKeySpec);
} catch (Exception e) {
System.out.println("Exception privateKey::::::::::::::::: "
+ e.getMessage());
e.printStackTrace();
}
byte[] decodedBytes = null;
try {
Cipher c = Cipher.getInstance("RSA/ECB/NoPadding");
// Cipher c = Cipher.getInstance("RSA");
c.init(Cipher.DECRYPT_MODE, privateKey);
decodedBytes = c.doFinal(Base64.decodeBase64(inputString));
// decodedBytes = c.doFinal(Base64InputStream());
} catch (Exception e) {
System.out.println("Exception privateKey1::::::::::::::::: "
+ e.getMessage());
e.printStackTrace();
}
if (decodedBytes != null) {
resultStr = new String(decodedBytes);
System.out.println("resultStr:::" + resultStr + ":::::");
// resultStr = resultStr.replace(salt, "");
}
return resultStr;
}
我们使用以下方法使用 ANDROID Base64.NO_CLOSE
对字符串进行编码 public static String encrypt(String inputString, byte[] keyBytes) {
Calendar cal = Calendar.getInstance();
int mDay = cal.get(Calendar.DAY_OF_MONTH);
// System.out.println("Day of month :::" + mDay);
String encryptedString = "";
Key publicKey = null;
try {
Random generator = new Random(mDay);
int num = (generator.nextInt()) % 100;
String salt = "WEER563784" + num;
inputString += salt;
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
publicKey = keyFactory.generatePublic(publicKeySpec);
} catch (Exception e) {
System.out.println("Exception rsaEncrypt::::::::::::::::: "
+ e.getMessage());
e.printStackTrace();
}
// Encode the original data with RSA public key
byte[] encodedBytes = null;
try {
Cipher c = Cipher.getInstance("RSA");
c.init(Cipher.ENCRYPT_MODE, publicKey);
encodedBytes = c.doFinal(inputString.getBytes());
encryptedString = Base64.encodeToString(encodedBytes,
Base64.NO_CLOSE);
System.out.println(encryptedString);
} catch (Exception e) {
System.out.println("Exception rsaEncrypt::::::::::::::::: "
+ e.getMessage());
e.printStackTrace();
}
return encryptedString;
}
正在使用以下方法在 Android 应用外部解密生成的加密字符串
public static String decrypt(String inputString, byte[] keyBytes) {
String resultStr = null;
Calendar cal = Calendar.getInstance();
int mDay = cal.get(Calendar.DAY_OF_MONTH);
Random generator = new Random(mDay);
int num = (generator.nextInt()) % 100;
String salt = "qqq" + num;
PrivateKey privateKey = null;
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(keyBytes);
privateKey = keyFactory.generatePrivate(privateKeySpec);
} catch (Exception e) {
System.out.println("Exception privateKey::::::::::::::::: "
+ e.getMessage());
}
byte[] decodedBytes = null;
try {
Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding");
// Cipher c = Cipher.getInstance("RSA");
c.init(Cipher.DECRYPT_MODE, privateKey);
// decodedBytes = c.doFinal(Base64.decodeBase64(inputString));
decodedBytes = c.doFinal(Base64InputStream());
} catch (Exception e) {
System.out.println("Exception privateKey1::::::::::::::::: "
+ e.getMessage());
e.printStackTrace();
}
if (decodedBytes != null) {
resultStr = new String(decodedBytes);
System.out.println("resultStr:::" + resultStr + ":::::");
resultStr = resultStr.replace(salt, "");
}
return resultStr;
}
我们遇到以下异常
javax.crypto.BadPaddingException: Decryption error
at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:380)
at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:291)
at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:365)
at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:391)
at javax.crypto.Cipher.doFinal(Cipher.java:2087)
at RSAEncryption.decrypt(RSAEncryption.java:41)
at RSAEncryption.main(RSAEncryption.java:108)
所以问题是
1) 是否可以在 Android 之外使用 ANDROID Base64.NO_CLOSE 解密加密的字符串,我的意思是直接在 IDE 中?
2)在post的其中一个中,我发现使用ANDROIDBase64.NO_WRAP加密的字符串可以在Androidenv之外解密,这样理解正确吗?
非常感谢您的提前帮助。
此致, 阿米特
问题已解决...
解密时需要用到RSA/ECB/NoPadding
基本上,当我们在 Android 中使用 RSA 加密值时,在单独的独立设备中解密 Java env 应该在密码中使用 RSA/ECB/NoPadding。
Android加密代码:
public static String encrypt(String inputString, byte[] keyBytes) {
Calendar cal = Calendar.getInstance();
int mDay = cal.get(Calendar.DAY_OF_MONTH);
// System.out.println("Day of month :::" + mDay);
String encryptedString = "";
Key publicKey = null;
try {
Random generator = new Random(mDay);
int num = (generator.nextInt()) % 100;
String salt = "MNSadm563784" + num;
//inputString += salt;
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
publicKey = keyFactory.generatePublic(publicKeySpec);
} catch (Exception e) {
System.out.println("Exception rsaEncrypt::::::::::::::::: "
+ e.getMessage());
e.printStackTrace();
}
// Encode the original data with RSA public key
byte[] encodedBytes = null;
try {
Cipher c = Cipher.getInstance("RSA");
c.init(Cipher.ENCRYPT_MODE, publicKey);
encodedBytes = c.doFinal(inputString.getBytes());
encryptedString = Base64.encodeToString(encodedBytes,
Base64.NO_CLOSE);
System.out.print("-----??"+encryptedString+"??-------");
} catch (Exception e) {
System.out.println("Exception rsaEncrypt::::::::::::::::: "
+ e.getMessage());
e.printStackTrace();
}
return encryptedString;
}
服务器解密码:
public static String decrypt(String inputString, byte[] keyBytes) {
String resultStr = null;
// Calendar cal = Calendar.getInstance();
// int mDay = cal.get(Calendar.DAY_OF_MONTH);
// Random generator = new Random(mDay);
// int num = (generator.nextInt()) % 100;
// String salt = "MNSadm563784" + num;
PrivateKey privateKey = null;
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(
keyBytes);
privateKey = keyFactory.generatePrivate(privateKeySpec);
} catch (Exception e) {
System.out.println("Exception privateKey::::::::::::::::: "
+ e.getMessage());
e.printStackTrace();
}
byte[] decodedBytes = null;
try {
Cipher c = Cipher.getInstance("RSA/ECB/NoPadding");
// Cipher c = Cipher.getInstance("RSA");
c.init(Cipher.DECRYPT_MODE, privateKey);
decodedBytes = c.doFinal(Base64.decodeBase64(inputString));
// decodedBytes = c.doFinal(Base64InputStream());
} catch (Exception e) {
System.out.println("Exception privateKey1::::::::::::::::: "
+ e.getMessage());
e.printStackTrace();
}
if (decodedBytes != null) {
resultStr = new String(decodedBytes);
System.out.println("resultStr:::" + resultStr + ":::::");
// resultStr = resultStr.replace(salt, "");
}
return resultStr;
}