我試圖使用我自己的Java中的RSA實現來加密和解密字符串,並最終生成一個包含明文的文件。我試着在SO和Web上嘗試過無數的例子,其中人們使用內置的Java RSA函數或者它們自己的算法實現,但是不能讓它們中的任何一個工作,因爲解密後的文本永遠不會匹配原始字符串。使用自己的RSA實現加密純文本
我不知道如果我在文本的編碼/解碼方面做錯了什麼,嘗試在java.xml.*
中使用默認的UTF-8和Base64編碼器,但沒有運氣。我沒有使用任何填充,我不知道它是否有必要爲它工作,並且即時通訊只是使用任意鍵長度,這試圖改變大小,並沒有使事情工作。這只是我自己的一項練習,沒有人會受到這個或任何東西的保護。但是我不知道是什麼問題,所以這裏是我的代碼,試圖加密/解密簡單的字符串:
BigInteger ONE = new BigInteger("1");
SecureRandom rand = new SecureRandom();
BigInteger d;
BigInteger e;
BigInteger n;
BigInteger p = BigInteger.probablePrime(10, rand); // 10 is arbitrary, have tried different numbers
BigInteger q = BigInteger.probablePrime(10, rand);
BigInteger phi = (p.subtract(ONE)).multiply(q.subtract(ONE));
n = p.multiply(q); //10 bits * 10 bits = ??? bits for key length
e = new BigInteger("65537"); //public key exponent
d = e.modInverse(phi); //private key exponent
String string = "this is a test";
byte[] bytes = string.getBytes();
BigInteger plainText = new BigInteger(bytes);
BigInteger cipherText = plainText.modPow(e, n);
BigInteger originalMessage = cipherText.modPow(d, e);
System.out.println(string.getBytes());
System.out.println(cipherText);
System.out.println(originalMessage);
輸出有不同的價值觀對所有三件事情,每次我跑我的程序: 但他們總是在相同的相對形式:
[[email protected]
157529
24312
'新的Sting(byte [])'構造函數和'String.getBytes()'方法使用默認的字符編碼,因系統而異。您需要明確指定編碼。 (如有疑問,請使用UTF-8。) – ntoskrnl