所以,我正在尝试使用Java的RSA库加密和解密声音文件。当我使用字符串进行操作时它没有问题,我研究了RSA加密,所以我知道我在做什么。问题必须是处理文件或BigIntegers。
加密文件中的字节,然后解密并将其放入新文件的代码:
File sound = new File("F://java/Projekt kryptering/ljudtest.wma");
FileReader fr = new FileReader(sound);
int fragSize = 200;
ArrayList<BigInteger> encryptedFragments = new ArrayList<BigInteger>((int)(sound.length()/fragSize)); //create an aproximation on number of BigIntegers required
byte byt;
byte[] buf = new byte[fragSize];
int bufPos = 0;
while((byt = (byte)fr.read()) != -1){
buf[bufPos++] = byt;
if(bufPos == fragSize){
BigInteger soundFrag = new BigInteger(buf);
System.out.println("unencrypted bigInt: " + soundFrag);
encryptedFragments.add(soundFrag.modPow(pub.getPublicExponent(), pub.getModulus()));
bufPos = 0;
}
}
fr.close();
File decryptedSound = new File("F://java/Projekt kryptering/ljudtest2.wma");
decryptedSound.createNewFile();
FileWriter fw = new FileWriter(decryptedSound);
for(BigInteger frag : encryptedFragments){
BigInteger decrypted = frag.modPow(priv.getPrivateExponent(), priv.getModulus());
System.out.println("decrypted frag " + decrypted);
byte[] frags = decrypted.toByteArray();
for(byte fragg : frags)
fw.write(fragg);
}
fw.close();
当我尝试这样做时会发生的结果是生成的文件只有276个字节(原始文件是22,3kB)。您还可以在代码中看到一些Sytem.outs,这里的结果来自:
unencrypted bigInt: <Very long number>
unencrypted bigInt: <Very long number 2>
unencrypted bigInt: 0
unencrypted bigInt: 0
unencrypted bigInt: 0
unencrypted bigInt: 0
unencrypted bigInt: 0
unencrypted bigInt: 0
unencrypted bigInt: 0
unencrypted bigInt: 0
unencrypted bigInt: 0
unencrypted bigInt: 0
unencrypted bigInt: 0
unencrypted bigInt: 0
unencrypted bigInt: 0
unencrypted bigInt: 0
unencrypted bigInt: 0
unencrypted bigInt: 0
unencrypted bigInt: 0
unencrypted bigInt: 0
unencrypted bigInt: 0
decrypted frag <Very long number 3>
decrypted frag <Very long number 4
decrypted frag 0
decrypted frag 0
decrypted frag 0
decrypted frag 0
decrypted frag 0
decrypted frag 0
decrypted frag 0
decrypted frag 0
decrypted frag 0
decrypted frag 0
decrypted frag 0
decrypted frag 0
decrypted frag 0
decrypted frag 0
decrypted frag 0
decrypted frag 0
decrypted frag 0
decrypted frag 0
decrypted frag 0