diff --git a/src/main/java/RSACryptors.java b/src/main/java/RSACryptors.java index 884be6b..fe8aaba 100644 --- a/src/main/java/RSACryptors.java +++ b/src/main/java/RSACryptors.java @@ -1,6 +1,9 @@ import javax.crypto.Cipher; +import javax.crypto.spec.OAEPParameterSpec; +import javax.crypto.spec.PSource; import java.security.*; import java.security.spec.InvalidKeySpecException; +import java.security.spec.MGF1ParameterSpec; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; @@ -11,9 +14,10 @@ import java.security.spec.X509EncodedKeySpec; public class RSACryptors { //key length - public static final int KEY_LENGTH = 4096; + public static final int KEY_LENGTH = 2048; //main family of rsa public static final String RSA = "RSA"; + public static final String RSA_OAEP = "RSA/ECB/OAEPPadding"; /** * generate a 2048 bit RSA key @@ -45,8 +49,9 @@ public class RSACryptors { public static byte[] encryptRSA(byte[] plain, PublicKey publicKey) { byte[] enc = null; try { - Cipher cipher = Cipher.getInstance(RSA); - cipher.init(Cipher.ENCRYPT_MODE, publicKey); + Cipher cipher = Cipher.getInstance(RSA_OAEP); + OAEPParameterSpec oaepParams = new OAEPParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT); + cipher.init(Cipher.ENCRYPT_MODE, publicKey, oaepParams); enc = cipher.doFinal(plain); } //no need to catch 4 different exceptions @@ -68,8 +73,9 @@ public class RSACryptors { public static byte[] decryptRSA(byte[] enc, PrivateKey privateKey) { byte[] plain = null; try { - Cipher cipher = Cipher.getInstance(RSA); - cipher.init(Cipher.DECRYPT_MODE, privateKey); + Cipher cipher = Cipher.getInstance(RSA_OAEP); + OAEPParameterSpec oaepParams = new OAEPParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT); + cipher.init(Cipher.DECRYPT_MODE, privateKey, oaepParams); plain = cipher.doFinal(enc); } //no need to catch 4 different exceptions @@ -83,15 +89,40 @@ public class RSACryptors { public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException { final StringBuffer log = new StringBuffer(); -// KeyPair pair = RSACryptors.generateRSAKey(); -// System.out.println(new String(Base64.encode(pair.getPrivate().getEncoded()))); -// System.out.println("\n\n"); -// System.out.println(new String(Base64.encode(pair.getPublic().getEncoded()))); - final String priKey = "MIIJQgIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQC0deqTP1T8yOAvGu8UXxx2ERDeofF6RHuqkJ1dkbaCU59m/dH1L5+tNEKWNyqZIbfF/o5/UTjRA/rP2oQ2jA9IFtbBTQVMf1VDE3mnCZMzU3ATX7sWU0ej2WuHpgBV5DdroyUWVZGmpcvF1PnCLWIH2rLFA0SEoAspth8vqVrsY7w/nPOsUmwB10W+KRKhJa1UhGBUPjtaq2Y3vXnDkwqaT326K5DpjFF4wEWoYshmkkAtBetU0tPCkbqkNLGBj7qZiHdcr3wCOroa28kpYRgmD6nezVdkWcVdRNZr/HHxdtAHNtxH+6CAfU36kVd7GdYW5DUnoHXt4q2hZ3M0zl9UP4ZZk5MClwu4W9ALb6jLxY5YBcKkLuAXs94lESELhEJnarcTPoMfhtr6YvVuJBOajHHylV2J+8JQ18NI+EITJxh/JeWrPtQGH/dYS9v0MqgXFMu67UAOOot67+8e1D2Iq5bMX/Six1uwVCMW/CRpPOfM5qi/H89GkjSG4h2B79E+6W0Y+bOnTNoSSgjQf3AhpGudhKmsidR/6h2F1RhgTMMU4i3KZmU66if3dJi3GstmQdkve2vE+YTc+qGr91RHBvN05WrNDoz6c5UPOewzckGZiMt57fnWtCnZc/d8WHzx6nFG9Wf1Cy1SJa3p7SG8ETb30aHiE328pxatuknVfQIDAQABAoICADrV4wfk8xmDiIvzFRWoOMhJoh1j4S4C2+rtQVE7a6ysBOF4SZbN0xqOWlNNYycP9jGoX98kpQiIaqKP1OFP4r+XKqdVLDAXp725wMTz+zkVsAmxAXcy6b2C6msMdTmLsYH3a+UPOvCBR6GVYWSslTSIWhafjRxMbbdIuHw6k/lUPxDiZBO/v5U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4Yfbcb15hAclaSzKvMFERmocsLTdsS0Zl6QwC/e+hjyijan01A7OUQ=="; - final String pubKey = "MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAtHXqkz9U/MjgLxrvFF8cdhEQ3qHxekR7qpCdXZG2glOfZv3R9S+frTRCljcqmSG3xf6Of1E40QP6z9qENowPSBbWwU0FTH9VQxN5pwmTM1NwE1+7FlNHo9lrh6YAVeQ3a6MlFlWRpqXLxdT5wi1iB9qyxQNEhKALKbYfL6la7GO8P5zzrFJsAddFvikSoSWtVIRgVD47WqtmN715w5MKmk99uiuQ6YxReMBFqGLIZpJALQXrVNLTwpG6pDSxgY+6mYh3XK98Ajq6GtvJKWEYJg+p3s1XZFnFXUTWa/xx8XbQBzbcR/uggH1N+pFXexnWFuQ1J6B17eKtoWdzNM5fVD+GWZOTApcLuFvQC2+oy8WOWAXCpC7gF7PeJREhC4RCZ2q3Ez6DH4ba+mL1biQTmoxx8pVdifvCUNfDSPhCEycYfyXlqz7UBh/3WEvb9DKoFxTLuu1ADjqLeu/vHtQ9iKuWzF/0osdbsFQjFvwkaTznzOaovx/PRpI0huIdge/RPultGPmzp0zaEkoI0H9wIaRrnYSprInUf+odhdUYYEzDFOItymZlOuon93SYtxrLZkHZL3trxPmE3Pqhq/dURwbzdOVqzQ6M+nOVDznsM3JBmYjLee351rQp2XP3fFh88epxRvVn9QstUiWt6e0hvBE299Gh4hN9vKcWrbpJ1X0CAwEAAQ=="; - final X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(Base64.decode(pubKey)); - final PKCS8EncodedKeySpec priSpec = new PKCS8EncodedKeySpec(Base64.decode(priKey)); + KeyPair pair = RSACryptors.generateRSAKey(); + System.out.println(new String(Base64.encode(pair.getPrivate().getEncoded()))); + System.out.println("\n\n"); + System.out.println(new String(Base64.encode(pair.getPublic().getEncoded()))); +// final String priKey = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDIo9xEC9owvyos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cDZqovK+6kmFT1m1FNMdRuCdidyYVw69P2bYF9jjCAPmqGXrdkTZjwWwgOC5B4yeR1IYoUYqOeQwclSscaftU8ms3I4mqzhPzB7oRyv3jNDp2uxcSRVSO4UafiK1/f7MAA8T5f833z+ZD35Ne3mm3C2Fz4bM8kD3vDrAoGAENfY13eVvdPGt/SfnXVHU+zIDdo5Stsiv5tbmzsUgWRd0N57787pLZqStGYTztHvZ2Y1Exv19YnNj2uf+dGi2nig+Wynxh2rFK3fGTdUA3SgjsqgM2+8ckgpBeodtV37N8kIMD0eqPoaf+ErWw/90L9o0e9k5LRp8PVLXMewO38="; + final String pubKey="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1R2W+sxZsG1yEgUeICMlGiLD7C21BzSspFgUuOgrMNpECqMtXDZapLXYYRh/1CqorUasiPYNgqBY9Ggtx1+KwVQ0QxQ7j7Lo9fT4hBpmhiwNBOhlr8GepYbfRAKROn+X3KdysvXezwP6ikRqHInrAuOIuQwMHmuu8QYhSIc2LksraDDTBoRUuoltlTTsWX4VusbM3+M8oAbpcNUl43dDuBj4sNaKyPrhEMjYA8WrAsvi2jmMPYw0mjSkWViDgNZlVXpXsSxav8vJ19+gOjFm1XCJbfq5Bm9uaG72Co01vzs9YUWrjMEFVJ1E37CfiPnPCydevOPapTY1MlsFqfWluwIDAQAB"; + + final byte[] pubkeydata = Base64.decode(pubKey); + final byte[] prikeydata = Base64.decode(priKey); + log.append(" pub {"); + for (int j = 0; j < pubkeydata.length; j++) { + if (j != 0 && j % 16 == 0) { + log.append("\n"); + } + log.append(String.valueOf(pubkeydata[j]) + ","); + } + log.append(" }\n"); + + + log.append(" prikey {"); + for (int j = 0; j < prikeydata.length; j++) { + if (j != 0 && j % 16 == 0) { + log.append("\n"); + } + log.append(String.valueOf(prikeydata[j]) + ","); + } + log.append(" }\n"); + + final X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(pubkeydata); + final PKCS8EncodedKeySpec priSpec = new PKCS8EncodedKeySpec(prikeydata); final KeyFactory kf = KeyFactory.getInstance("RSA"); + final PublicKey pubKeyObj=kf.generatePublic(pubSpec); final PrivateKey priKeyObj=kf.generatePrivate(priSpec); final String source="encryptRSA"; @@ -101,12 +132,16 @@ public class RSACryptors { final byte[] encrypted=RSACryptors.encryptRSA(source.getBytes(),pubKeyObj); - + log.append('\n'); + log.append(new String(Base64.encode(encrypted))); + log.append('\n'); +// log.append(" {"); for (int j = 0; j < encrypted.length; j++) { log.append(String.valueOf(encrypted[j]) + ","); } log.append(" }\n"); +// final byte[] encrypted=Base64.decode("MEogpqN82NLup5AtwaGb2bB810yQ0W3LlT5mXhoysVnk9unLeIEQ1Ocmx3dP0HQcLwisVhy8EBm4XSpCx9IS6Xp1sRsyHV0mMIsDKtDRqkUs5rJtMm8ZkWst+AowGKCI+HYuzIUYqriPjtBibdIY703EUnoVNILxK1/YChpJF6ksvMsBQPQPgcwtM3KLkHXNPmxWp8foHzdeUFqbUbgO8e+2FuSQ0v2j4x75elLLcrh4SEtGLl36ra0h5/4g6Q5NiCsk8bokUP6L2eZFISbqzoKS2NJ9veDBjUzLeb+8d2PDgCAnlc8CzX9IBJottGGdM4YicdWrJBNbaj0gSC2czg=="); final byte[] decrypted=RSACryptors.decryptRSA(encrypted,priKeyObj); log.append(" trans : "); log.append(new String(decrypted));