Commit 10448834 authored by yanzg's avatar yanzg

表结构修改

parent c53c65c3
...@@ -83,6 +83,20 @@ public final class RsaHelper { ...@@ -83,6 +83,20 @@ public final class RsaHelper {
* @throws Exception * @throws Exception
*/ */
public static void generatorKeyPair() { public static void generatorKeyPair() {
try {
RsaInfo rsaInfo = generatorRsa();
Log.info(RsaHelper.class, "生成的公钥:%s\n生成的私钥:%s", rsaInfo.getPublicKey(), rsaInfo.getPrivateKey());
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
/**
* 生成密钥对
*
* @throws Exception
*/
public static RsaInfo generatorRsa() {
try { try {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(ALGORITHM_RSA); KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(ALGORITHM_RSA);
keyPairGen.initialize(KEYPAIR_LEN); keyPairGen.initialize(KEYPAIR_LEN);
...@@ -90,12 +104,10 @@ public final class RsaHelper { ...@@ -90,12 +104,10 @@ public final class RsaHelper {
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic(); RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate(); RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
byte[] keyBs = rsaPublicKey.getEncoded(); String publicKey = encodeBase64(rsaPublicKey.getEncoded());
String publicKey = encodeBase64(keyBs); String privateKey = encodeBase64(rsaPrivateKey.getEncoded());
Log.info(RsaHelper.class, "生成的公钥:\t %s", publicKey);
keyBs = rsaPrivateKey.getEncoded(); return new RsaInfo(publicKey, privateKey);
String privateKey = encodeBase64(keyBs);
Log.info(RsaHelper.class, "生成的私钥:\t %s", privateKey);
} catch (Exception ex) { } catch (Exception ex) {
throw new RuntimeException(ex); throw new RuntimeException(ex);
} }
...@@ -316,4 +328,39 @@ public final class RsaHelper { ...@@ -316,4 +328,39 @@ public final class RsaHelper {
throw new RuntimeException(ex); throw new RuntimeException(ex);
} }
} }
public static class RsaInfo {
/**
* 公钥
*/
private String publicKey;
/**
* 私钥
*/
private String privateKey;
public RsaInfo() {
}
public RsaInfo(String publicKey, String privateKey) {
this.publicKey = publicKey;
this.privateKey = privateKey;
}
public String getPublicKey() {
return publicKey;
}
public void setPublicKey(String publicKey) {
this.publicKey = publicKey;
}
public String getPrivateKey() {
return privateKey;
}
public void setPrivateKey(String privateKey) {
this.privateKey = privateKey;
}
}
} }
\ No newline at end of file
package helper; package helper;
import com.yanzuoguang.util.helper.RsaHelper; import com.yanzuoguang.util.helper.RsaHelper;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
public class TestRsa { public class TestRsa {
...@@ -8,24 +9,52 @@ public class TestRsa { ...@@ -8,24 +9,52 @@ public class TestRsa {
@Test @Test
public void test() throws Exception { public void test() throws Exception {
RsaHelper.generatorKeyPair(); RsaHelper.generatorKeyPair();
}
@Test
public void testRsa() throws Exception {
RsaHelper.RsaInfo rsaInfo = RsaHelper.generatorRsa();
testRsaPublicEncode(rsaInfo);
testRsaPrivateEncode(rsaInfo);
} }
@Test @Test
public void rsa() { public void rsa() {
String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC3GdrIdT2lOblSalXgw4Bjnv7f2ybH+iv39QLUO4j1PeN0vacswQgmUq3KHLbxWeBqNTdsuR/G5zKv4e3raEYsAxrlHw/AJCJQOKTpYEPKLiVhNj7mqg1+I66BRwaG9AunN4bF6LfTj2wgFElVbFGbv1v8SEVDRYiUkwVsAo5JqQIDAQAB"; String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC3GdrIdT2lOblSalXgw4Bjnv7f2ybH+iv39QLUO4j1PeN0vacswQgmUq3KHLbxWeBqNTdsuR/G5zKv4e3raEYsAxrlHw/AJCJQOKTpYEPKLiVhNj7mqg1+I66BRwaG9AunN4bF6LfTj2wgFElVbFGbv1v8SEVDRYiUkwVsAo5JqQIDAQAB";
String privateKey = "MIICeAIBAD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pAgMBAAECgYBwJ18SsHWtEhmI+OdXgIjQ/J/j4Kn1jjCGdkZgV4NBrMH5TP3sdOSYDMa06TfJyCKlC6nCZ/al8BHlF/+S1VE9Mu8DRrbHAO4OJQyOEaQ64mePL6jYkhVbjr4q7kHTbsIWVu3sH9TJRa3n+lsEpEv90ri458+ofn++h+rlYKlLfQJBAPVWi1VanQlyAtGKmeA7GrxH+XYiqeq4J1suT1qUTsRwEM4RLUZcrsdGio2bVwbbyX3TB2vYdLvf1otdndKBr0MCQQC/Due/BN+B/hqOgMdW2RoHfycg/HVOp7CpsBikRUMegDQGPb6N+z/BIXkVBNHBADzaTcJh0DgSsoMRhlEdTWajAkEAq2V31/yDAytEbtGOqMmB5xG9ZNvYq2NWE2xqAdTkpnXIN75mS+bKL+vHNiDVDrSTsrSwVZaWDv7U6u5PKNZy8wJBAI/K/Af79uuy/vG5Yk2u37Q8sopU90TXWFKdwi4AIt/VxVHdLolVS0pjkumK0wLa1vHGDEHpoAoSaCrMWEghdNkCQQCUFZxSRZB8XiLtlNyT5s6UX89Ov3eZO1KmFWiXgXuZ3STr/8sDhyC9TN8uq8vK4lQiihlZQFw89LwogtJLAExc";
RsaHelper.RsaInfo rsaInfo = new RsaHelper.RsaInfo(publicKey, privateKey);
testRsaPublicEncode(rsaInfo);
testRsaPrivateEncode(rsaInfo);
}
private void testRsaPublicEncode(RsaHelper.RsaInfo rsaInfo) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 50; i++) {
sb.append((char) ('A' + i % 26));
}
String from = sb.toString();
String temp = RsaHelper.encryptionByPublicKey(from, rsaInfo.getPublicKey());
String result = RsaHelper.decryptionByPrivateKey(temp, rsaInfo.getPrivateKey());
System.out.println("原字段:" + from);
System.out.println("公钥加密后字段" + temp);
System.out.println("私钥解密后字段" + result);
Assert.assertEquals(from, result);
}
private void testRsaPrivateEncode(RsaHelper.RsaInfo rsaInfo) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (int i = 0; i < 50; i++) { for (int i = 0; i < 50; i++) {
sb.append((char) ('A' + i % 26)); sb.append((char) ('A' + i % 26));
} }
String from = sb.toString(); String from = sb.toString();
String temp = RsaHelper.encryptionByPublicKey(from, publicKey); String temp = RsaHelper.encryptionByPrivateKey(from, rsaInfo.getPrivateKey());
String result = RsaHelper.decryptionByPrivateKey(temp, privateKey); String result = RsaHelper.decryptionByPublicKey(temp, rsaInfo.getPublicKey());
System.out.println(from); System.out.println("原字段:" + from);
System.out.println(result); System.out.println("公钥加密后字段" + temp);
System.out.println(temp); System.out.println("私钥解密后字段" + result);
Assert.assertEquals(from, result);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment