当我运行以下代码时,解密长度与原始长度不同。所以无法读取音频。
我收到此错误:javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input stream
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1119)
at MainClass.main(MainClass.java:119)
我的代码在这里:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
public class MainClass {
public static final int SIZE = 1024;
private BigInteger p, q, n, totient, e, d;
private Random rnd = new Random();
public MainClass() {
p = new BigInteger(1024, 10, new Random());
do {
q = new BigInteger(1024, 10, new Random());
} while (p.equals(q));
n = p.multiply(q);
totient = (p.subtract(BigInteger.ONE)).multiply(q
.subtract(BigInteger.ONE));
// System.out.println(p+"\n"+q+"\n"+n+"\n"+totient);
}
public BigInteger getMod() {
d = e.modInverse(totient);
// System.out.println("d="+d);
return d;
}
public BigInteger getE() {
do {
e = new BigInteger(SIZE, rnd);
} while (!e.gcd(totient).equals(BigInteger.ONE)
|| !(e.compareTo(totient) < 0));
return e;
}
public String encrypt(String msg) {
BigInteger ciphertext = new BigInteger(msg.getBytes());
String cipher = (ciphertext).modPow(getE(), getN()).toString();
return cipher;
}
public String decrypt(String msg) {
byte[] decryptMsg = (new BigInteger(msg)).modPow(getMod(), getN())
.toByteArray();
String plain = new String(decryptMsg);
return plain;
}
public BigInteger getN() {
return n;
}
public static void main(String[] args) {
try {
List<String> ori = new ArrayList<String>();
List<String> st1 = new ArrayList<String>();
List<String> st2 = new ArrayList<String>();
String s2;
MainClass mc = new MainClass();
// StringHelper sh = new StringHelper();
byte[] input;
final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
// final AudioFileFormat.Type [] types =
// AudioSystem.getAudioFileTypes();
final String PATH = "C:\\Users\\Rahul\\Desktop\\adios.wav";
final AudioInputStream audioInputStream = AudioSystem
.getAudioInputStream(new File(PATH));
AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE,
byteOut);
audioInputStream.close();
byteOut.close();
input = ((ByteArrayOutputStream) byteOut).toByteArray();
System.out.println(input.length);
input.toString();
String s = new String(input);
System.out.println("Original " + s.length() + "\n");
int i = 0;
for (i = 0; i + 255 < s.length(); i = i + 256) {
ori.add(s.substring(i, i + 255));
}
ori.add(s.substring(i - 256, s.length() - 1));
// System.out.println(s.length());
for (i = 0; i < ori.size(); i++) {
st1.add(mc.encrypt(ori.get(i)));
}
// String s1 = mc.encrypt(s.substring(0, 500));
// System.out.println("Cipher Text");
// System.out.println(s1.length()+ "\n"+s1);
for (i = 0; i < st1.size(); i++) {
st2.add(mc.decrypt(st1.get(i)));
}
// String s2 = mc.decrypt(s1);
StringBuffer sb = new StringBuffer();
for (i = 0; i < st2.size(); i++) {
sb.append(st2.get(i));
}
s2 = sb.toString();
System.out.println("Decrypt" + s2.length() + "\n");
byte[] output = s2.getBytes();
ByteArrayInputStream oInstream = new ByteArrayInputStream(output);
AudioInputStream oAIS = AudioSystem.getAudioInputStream(oInstream);
AudioSystem.write(oAIS, AudioFileFormat.Type.WAVE, new File(
"E:\\decrypted.wav"));
oAIS.close();
oInstream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
有什么帮助吗??