パディングSwiftyRSA
付きの公開鍵で文字列を暗号化するために使用します。残念ながら、暗号化された文字列を Java で復号化したときにPKCS1
見つかりました。これまでのところ、文字列の暗号化/復号化にBadPadding: Encryption Error
Java を使用していることがわかりましたが、iOS/Swift にはありません。Swift と Java 間の暗号化/復号化に使用するアルゴリズムを教えてください。Mode
Mode
暗号化/復号化するための公開鍵と秘密鍵は次のとおりです
https://github.com/ppshein/Encrypt-Decrypt
迅速な暗号化
let publicKey = try PublicKey(pemNamed: "public")
let clear = try ClearMessage(string: inString, using: .utf8)
let encrypted = try clear.encrypted(with: publicKey, padding: .init(rawValue: 0))
let base64 = encrypted.data.base64EncodedString()
復号化するJava
public class CryptographyUsingKeystore {
private static final String ALGORITHM = "RSA";
public static byte[] encrypt(PublicKey publicKey, byte[] inputData)
throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.PUBLIC_KEY, publicKey);
byte[] encryptedBytes = cipher.doFinal(inputData);
return encryptedBytes;
}
public static byte[] decrypt(PrivateKey privateKey, byte[] inputData)
throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.PRIVATE_KEY, privateKey);
byte[] decryptedBytes = cipher.doFinal(inputData);
return decryptedBytes;
}
public static void main(String[] args) throws Exception {
KeyProvider keyProvider = new KeyProvider();
PublicKey publicKey = myKey.getPemPublicKey();
//PrivateKey privateKey = (PrivateKey) keyProvider.getPrivateKey();
byte[] encryptedData = encrypt(publicKey,
"Hello".getBytes());
System.out.println("Encrypted Key.... ");
System.out.println(new String(Base64.getEncoder().encode(encryptedData)));
byte[] decryptedData = decrypt(privateKey, encryptedData);
System.out.println("Decrypted key.... ");
System.out.println(new String(decryptedData));
}
}